This commit is contained in:
tamaina 2025-09-25 20:33:22 +09:00 committed by GitHub
commit e2044099f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 118 additions and 148 deletions

View File

@ -10,22 +10,40 @@ export type IImage = {
data: Buffer; data: Buffer;
ext: string | null; ext: string | null;
type: string; type: string;
filename?: string;
size?: number;
}; };
export type IImageStream = { export type IImageStream = {
data: Readable; data: Readable;
ext: string | null; ext: string | null;
type: string; type: string;
filename?: string;
size?: number;
}; };
export type IImageSharp = { export type IImageSharp = {
data: sharp.Sharp; data: sharp.Sharp;
ext: string | null; ext: string | null;
type: string; type: string;
filename?: string;
size?: number;
}; };
export type IImageStreamable = IImage | IImageStream | IImageSharp; export type IImageStreamable = IImage | IImageStream | IImageSharp;
export function getSizeFromIImage(image: IImageStreamable): number | undefined {
if ('size' in image) {
return image.size;
}
if (image.data instanceof Buffer) {
return image.data.length;
}
return;
}
export const webpDefault: sharp.WebpOptions = { export const webpDefault: sharp.WebpOptions = {
quality: 77, quality: 77,
alphaQuality: 95, alphaQuality: 95,

View File

@ -18,7 +18,7 @@ import { FILE_TYPE_BROWSERSAFE } from '@/const.js';
import { StatusError } from '@/misc/status-error.js'; import { StatusError } from '@/misc/status-error.js';
import type Logger from '@/logger.js'; import type Logger from '@/logger.js';
import { DownloadService } from '@/core/DownloadService.js'; import { DownloadService } from '@/core/DownloadService.js';
import { IImageStreamable, ImageProcessingService, webpDefault } from '@/core/ImageProcessingService.js'; import { getSizeFromIImage, type IImageStreamable, ImageProcessingService, webpDefault } from '@/core/ImageProcessingService.js';
import { VideoProcessingService } from '@/core/VideoProcessingService.js'; import { VideoProcessingService } from '@/core/VideoProcessingService.js';
import { InternalStorageService } from '@/core/InternalStorageService.js'; import { InternalStorageService } from '@/core/InternalStorageService.js';
import { contentDisposition } from '@/misc/content-disposition.js'; import { contentDisposition } from '@/misc/content-disposition.js';
@ -117,6 +117,49 @@ export class FileServerService {
return; return;
} }
@bindThis
private processFileAndConvertToIImage(file: Awaited<ReturnType<FileServerService['getStreamAndTypeFromUrl']>>, request: FastifyRequest, reply: FastifyReply): IImageStreamable {
if (typeof file !== 'object' || !file) {
throw new Error('Invalid file');
}
if (request.headers.range && 'file' in file && file.size > 0) {
this.logger.info(`Range request for file ${file.file.id} ${file.fileRole}: ${request.headers.range}`);
const range = request.headers.range as string;
const parts = range.replace(/bytes=/, '').split('-');
const start = parseInt(parts[0], 10);
let end = parts[1] ? parseInt(parts[1], 10) : file.size - 1;
if (end > file.size) {
end = file.size - 1;
}
const chunksize = end - start + 1;
reply.header('Content-Range', `bytes ${start}-${end}/${file.size}`);
reply.header('Accept-Ranges', 'bytes');
reply.code(206);
return {
data: fs.createReadStream(file.path, {
start,
end,
}),
ext: file.ext,
type: file.mime,
size: chunksize,
filename: file.filename,
};
}
return {
data: fs.createReadStream(file.path),
ext: file.ext,
type: file.mime,
size: file.size,
filename: file.filename,
};
}
@bindThis @bindThis
private async sendDriveFile(request: FastifyRequest<{ Params: { key: string; } }>, reply: FastifyReply) { private async sendDriveFile(request: FastifyRequest<{ Params: { key: string; } }>, reply: FastifyReply) {
const key = request.params.key; const key = request.params.key;
@ -135,9 +178,9 @@ export class FileServerService {
} }
try { try {
if (file.state === 'remote') { let image: IImageStreamable | null = null;
let image: IImageStreamable | null = null;
if (file.state === 'remote') {
if (file.fileRole === 'thumbnail') { if (file.fileRole === 'thumbnail') {
if (isMimeImage(file.mime, 'sharp-convertible-image-with-bmp')) { if (isMimeImage(file.mime, 'sharp-convertible-image-with-bmp')) {
reply.header('Cache-Control', 'max-age=31536000, immutable'); reply.header('Cache-Control', 'max-age=31536000, immutable');
@ -172,36 +215,7 @@ export class FileServerService {
} }
if (!image) { if (!image) {
if (request.headers.range && file.file.size > 0) { image = this.processFileAndConvertToIImage(file, request, reply);
const range = request.headers.range as string;
const parts = range.replace(/bytes=/, '').split('-');
const start = parseInt(parts[0], 10);
let end = parts[1] ? parseInt(parts[1], 10) : file.file.size - 1;
if (end > file.file.size) {
end = file.file.size - 1;
}
const chunksize = end - start + 1;
image = {
data: fs.createReadStream(file.path, {
start,
end,
}),
ext: file.ext,
type: file.mime,
};
reply.header('Content-Range', `bytes ${start}-${end}/${file.file.size}`);
reply.header('Accept-Ranges', 'bytes');
reply.header('Content-Length', chunksize);
reply.code(206);
} else {
image = {
data: fs.createReadStream(file.path),
ext: file.ext,
type: file.mime,
};
}
} }
if ('pipe' in image.data && typeof image.data.pipe === 'function') { if ('pipe' in image.data && typeof image.data.pipe === 'function') {
@ -212,78 +226,31 @@ export class FileServerService {
// image.dataがstreamでないなら直ちにcleanup // image.dataがstreamでないなら直ちにcleanup
file.cleanup(); file.cleanup();
} }
reply.header('Content-Type', FILE_TYPE_BROWSERSAFE.includes(image.type) ? image.type : 'application/octet-stream');
reply.header('Content-Length', file.file.size);
reply.header('Cache-Control', 'max-age=31536000, immutable');
reply.header('Content-Disposition',
contentDisposition(
'inline',
correctFilename(file.filename, image.ext),
),
);
return image.data;
}
if (file.fileRole !== 'original') {
const filename = rename(file.filename, {
suffix: file.fileRole === 'thumbnail' ? '-thumb' : '-web',
extname: file.ext ? `.${file.ext}` : '.unknown',
}).toString();
reply.header('Content-Type', FILE_TYPE_BROWSERSAFE.includes(file.mime) ? file.mime : 'application/octet-stream');
reply.header('Cache-Control', 'max-age=31536000, immutable');
reply.header('Content-Disposition', contentDisposition('inline', filename));
if (request.headers.range && file.file.size > 0) {
const range = request.headers.range as string;
const parts = range.replace(/bytes=/, '').split('-');
const start = parseInt(parts[0], 10);
let end = parts[1] ? parseInt(parts[1], 10) : file.file.size - 1;
if (end > file.file.size) {
end = file.file.size - 1;
}
const chunksize = end - start + 1;
const fileStream = fs.createReadStream(file.path, {
start,
end,
});
reply.header('Content-Range', `bytes ${start}-${end}/${file.file.size}`);
reply.header('Accept-Ranges', 'bytes');
reply.header('Content-Length', chunksize);
reply.code(206);
return fileStream;
}
return fs.createReadStream(file.path);
} else { } else {
reply.header('Content-Type', FILE_TYPE_BROWSERSAFE.includes(file.file.type) ? file.file.type : 'application/octet-stream'); if (file.fileRole !== 'original') {
reply.header('Content-Length', file.file.size); const filename = rename(file.filename, {
reply.header('Cache-Control', 'max-age=31536000, immutable'); suffix: file.fileRole === 'thumbnail' ? '-thumb' : '-web',
reply.header('Content-Disposition', contentDisposition('inline', file.filename)); extname: file.ext ? `.${file.ext}` : '.unknown',
}).toString();
if (request.headers.range && file.file.size > 0) { image = this.processFileAndConvertToIImage(file, request, reply);
const range = request.headers.range as string; image.filename = filename;
const parts = range.replace(/bytes=/, '').split('-'); } else {
const start = parseInt(parts[0], 10); image = this.processFileAndConvertToIImage(file, request, reply);
let end = parts[1] ? parseInt(parts[1], 10) : file.file.size - 1;
if (end > file.file.size) {
end = file.file.size - 1;
}
const chunksize = end - start + 1;
const fileStream = fs.createReadStream(file.path, {
start,
end,
});
reply.header('Content-Range', `bytes ${start}-${end}/${file.file.size}`);
reply.header('Accept-Ranges', 'bytes');
reply.header('Content-Length', chunksize);
reply.code(206);
return fileStream;
} }
return fs.createReadStream(file.path);
} }
reply.header('Content-Type', FILE_TYPE_BROWSERSAFE.includes(image.type) ? image.type : 'application/octet-stream');
const size = getSizeFromIImage(image);
if (size) reply.header('Content-Length', size);
reply.header('Cache-Control', 'max-age=31536000, immutable');
reply.header('Content-Disposition',
contentDisposition(
'inline',
correctFilename(image.filename ?? file.filename, image.ext),
),
);
return image.data;
} catch (e) { } catch (e) {
if ('cleanup' in file) file.cleanup(); if ('cleanup' in file) file.cleanup();
throw e; throw e;
@ -363,17 +330,31 @@ export class FileServerService {
data: fs.createReadStream(file.path), data: fs.createReadStream(file.path),
ext: file.ext, ext: file.ext,
type: file.mime, type: file.mime,
size: file.size,
}; };
} else { } else if (!('static' in request.query)) {
const data = (await sharpBmp(file.path, file.mime, { animated: !('static' in request.query) })) // animated
const data = (await sharpBmp(file.path, file.mime, { animated: true }))
.resize({ .resize({
height: 'emoji' in request.query ? 128 : 320, height: 'emoji' in request.query ? 128 : 320,
withoutEnlargement: true, withoutEnlargement: true,
}) });
.webp(webpDefault);
// byte range requestになる可能性があるので、Content-Lengthを送信するためにbufferにする
image = {
data: await data.webp(webpDefault).toBuffer(),
ext: 'webp',
type: 'image/webp',
};
} else {
const data = (await sharpBmp(file.path, file.mime, { animated: false }))
.resize({
height: 'emoji' in request.query ? 128 : 320,
withoutEnlargement: true,
});
image = { image = {
data, data: data.webp(webpDefault),
ext: 'webp', ext: 'webp',
type: 'image/webp', type: 'image/webp',
}; };
@ -420,36 +401,7 @@ export class FileServerService {
} }
if (!image) { if (!image) {
if (request.headers.range && file.file && file.file.size > 0) { image = this.processFileAndConvertToIImage(file, request, reply);
const range = request.headers.range as string;
const parts = range.replace(/bytes=/, '').split('-');
const start = parseInt(parts[0], 10);
let end = parts[1] ? parseInt(parts[1], 10) : file.file.size - 1;
if (end > file.file.size) {
end = file.file.size - 1;
}
const chunksize = end - start + 1;
image = {
data: fs.createReadStream(file.path, {
start,
end,
}),
ext: file.ext,
type: file.mime,
};
reply.header('Content-Range', `bytes ${start}-${end}/${file.file.size}`);
reply.header('Accept-Ranges', 'bytes');
reply.header('Content-Length', chunksize);
reply.code(206);
} else {
image = {
data: fs.createReadStream(file.path),
ext: file.ext,
type: file.mime,
};
}
} }
if ('cleanup' in file) { if ('cleanup' in file) {
@ -464,6 +416,8 @@ export class FileServerService {
} }
reply.header('Content-Type', image.type); reply.header('Content-Type', image.type);
const size = getSizeFromIImage(image);
if (size) reply.header('Content-Length', size);
reply.header('Cache-Control', 'max-age=31536000, immutable'); reply.header('Cache-Control', 'max-age=31536000, immutable');
reply.header('Content-Disposition', reply.header('Content-Disposition',
contentDisposition( contentDisposition(
@ -479,12 +433,7 @@ export class FileServerService {
} }
@bindThis @bindThis
private async getStreamAndTypeFromUrl(url: string): Promise< private async getStreamAndTypeFromUrl(url: string): Promise<Awaited<ReturnType<FileServerService['downloadAndDetectTypeFromUrl']>> | Awaited<ReturnType<FileServerService['getFileFromKey']>>> {
{ state: 'remote'; fileRole?: 'thumbnail' | 'webpublic' | 'original'; file?: MiDriveFile; mime: string; ext: string | null; path: string; cleanup: () => void; filename: string; }
| { state: 'stored_internal'; fileRole: 'thumbnail' | 'webpublic' | 'original'; file: MiDriveFile; filename: string; mime: string; ext: string | null; path: string; }
| '404'
| '204'
> {
if (url.startsWith(`${this.config.url}/files/`)) { if (url.startsWith(`${this.config.url}/files/`)) {
const key = url.replace(`${this.config.url}/files/`, '').split('/').shift(); const key = url.replace(`${this.config.url}/files/`, '').split('/').shift();
if (!key) throw new StatusError('Invalid File Key', 400, 'Invalid File Key'); if (!key) throw new StatusError('Invalid File Key', 400, 'Invalid File Key');
@ -497,17 +446,18 @@ export class FileServerService {
@bindThis @bindThis
private async downloadAndDetectTypeFromUrl(url: string): Promise< private async downloadAndDetectTypeFromUrl(url: string): Promise<
{ state: 'remote'; mime: string; ext: string | null; path: string; cleanup: () => void; filename: string; } { state: 'remote'; mime: string; ext: string | null; size: number; path: string; cleanup: () => void; filename: string; }
> { > {
const [path, cleanup] = await createTemp(); const [path, cleanup] = await createTemp();
try { try {
const { filename } = await this.downloadService.downloadUrl(url, path); const { filename } = await this.downloadService.downloadUrl(url, path);
const { mime, ext } = await this.fileInfoService.detectType(path); const { mime, ext } = await this.fileInfoService.detectType(path);
const { size } = await fs.promises.stat(path);
return { return {
state: 'remote', state: 'remote',
mime, ext, mime, ext, size,
path, cleanup, path, cleanup,
filename, filename,
}; };
@ -519,8 +469,8 @@ export class FileServerService {
@bindThis @bindThis
private async getFileFromKey(key: string): Promise< private async getFileFromKey(key: string): Promise<
{ state: 'remote'; fileRole: 'thumbnail' | 'webpublic' | 'original'; file: MiDriveFile; filename: string; url: string; mime: string; ext: string | null; path: string; cleanup: () => void; } { state: 'remote'; fileRole: 'thumbnail' | 'webpublic' | 'original'; file: MiDriveFile; filename: string; url: string; mime: string; ext: string | null; size: number; path: string; cleanup: () => void; }
| { state: 'stored_internal'; fileRole: 'thumbnail' | 'webpublic' | 'original'; file: MiDriveFile; filename: string; mime: string; ext: string | null; path: string; } | { state: 'stored_internal'; fileRole: 'thumbnail' | 'webpublic' | 'original'; file: MiDriveFile; filename: string; mime: string; ext: string | null; size: number; path: string; }
| '404' | '404'
| '204' | '204'
> { > {
@ -539,7 +489,6 @@ export class FileServerService {
if (!file.storedInternal) { if (!file.storedInternal) {
if (!(file.isLink && file.uri)) return '204'; if (!(file.isLink && file.uri)) return '204';
const result = await this.downloadAndDetectTypeFromUrl(file.uri); const result = await this.downloadAndDetectTypeFromUrl(file.uri);
file.size = (await fs.promises.stat(result.path)).size; // DB file.sizeは正確とは限らないので
return { return {
...result, ...result,
url: file.uri, url: file.uri,
@ -553,16 +502,18 @@ export class FileServerService {
if (isThumbnail || isWebpublic) { if (isThumbnail || isWebpublic) {
const { mime, ext } = await this.fileInfoService.detectType(path); const { mime, ext } = await this.fileInfoService.detectType(path);
const { size } = await fs.promises.stat(path);
return { return {
state: 'stored_internal', state: 'stored_internal',
fileRole: isThumbnail ? 'thumbnail' : 'webpublic', fileRole: isThumbnail ? 'thumbnail' : 'webpublic',
file, file,
filename: file.name, filename: file.name,
mime, ext, mime, ext, size,
path, path,
}; };
} }
const { size } = await fs.promises.stat(path);
return { return {
state: 'stored_internal', state: 'stored_internal',
fileRole: 'original', fileRole: 'original',
@ -571,6 +522,7 @@ export class FileServerService {
// 古いファイルは修正前のmimeを持っているのでできるだけ修正してあげる // 古いファイルは修正前のmimeを持っているのでできるだけ修正してあげる
mime: this.fileInfoService.fixMime(file.type), mime: this.fileInfoService.fixMime(file.type),
ext: null, ext: null,
size,
path, path,
}; };
} }