diff --git a/packages/backend/src/core/FileInfoService.ts b/packages/backend/src/core/FileInfoService.ts index e7d106a285..fab2af658c 100644 --- a/packages/backend/src/core/FileInfoService.ts +++ b/packages/backend/src/core/FileInfoService.ts @@ -341,16 +341,15 @@ export class FileInfoService { } /** - * Detect MIME Type and extension by stream for performance (this cannot detect SVG) + * Detect MIME Type and extension by stream and path for performance */ @bindThis - public async detectRequestType(_response: Response): Promise<{ + public async detectRequestType(_response: Response, path?: string, fileSavingPromise: Promise = Promise.resolve()): Promise<{ mime: string; ext: string | null; }> { const response = _response.clone(); - // Check 0 byte if (!response.body) { throw new StatusError('No Body', 400, 'No Body'); } @@ -358,12 +357,26 @@ export class FileInfoService { const type = await fileTypeFromStream(stream.Readable.fromWeb(response.body)); if (type) { + // XMLはSVGかもしれない + if (path && type.mime === 'application/xml') { + await fileSavingPromise; + if (await this.checkSvg(path)) { + return TYPE_SVG; + } + } + return { mime: type.mime, ext: type.ext, }; } + // 種類が不明でもSVGかもしれない + if (path) { + await fileSavingPromise; + if (await this.checkSvg(path)) return TYPE_SVG; + } + // 種類が不明なら application/octet-stream にする return TYPE_OCTET_STREAM; } diff --git a/packages/backend/src/server/FileServerService.ts b/packages/backend/src/server/FileServerService.ts index 04ff7113d8..c87041cabf 100644 --- a/packages/backend/src/server/FileServerService.ts +++ b/packages/backend/src/server/FileServerService.ts @@ -111,14 +111,7 @@ export class FileServerService { const response = await this.downloadService.fetchUrl(file.uri); const fileSaving = this.downloadService.pipeRequestToFile(response, path); - let { mime, ext } = await this.fileInfoService.detectRequestType(response); - if (mime === 'application/octet-stream' || mime === 'application/xml') { - await fileSaving; - if (await this.fileInfoService.checkSvg(path)) { - mime = TYPE_SVG.mime; - ext = TYPE_SVG.ext; - } - } + const { mime, ext } = await this.fileInfoService.detectRequestType(response, path, fileSaving); const convertFile = async () => { if (isThumbnail) { diff --git a/packages/backend/src/server/MediaProxyServerService.ts b/packages/backend/src/server/MediaProxyServerService.ts index c7def0a386..509ad661c4 100644 --- a/packages/backend/src/server/MediaProxyServerService.ts +++ b/packages/backend/src/server/MediaProxyServerService.ts @@ -77,14 +77,7 @@ export class MediaProxyServerService { const response = await this.downloadService.fetchUrl(url); const fileSaving = this.downloadService.pipeRequestToFile(response, path); - let { mime, ext } = await this.fileInfoService.detectRequestType(response); - if (mime === 'application/octet-stream' || mime === 'application/xml') { - await fileSaving; - if (await this.fileInfoService.checkSvg(path)) { - mime = TYPE_SVG.mime; - ext = TYPE_SVG.ext; - } - } + const { mime, ext } = await this.fileInfoService.detectRequestType(response, path, fileSaving); const isConvertibleImage = isMimeImage(mime, 'sharp-convertible-image'); const isAnimationConvertibleImage = isMimeImage(mime, 'sharp-animation-convertible-image');