1
0
forked from mirror/misskey

refactor detectRequestType

This commit is contained in:
tamaina 2023-01-15 15:36:37 +00:00
parent 00866c754e
commit cdef862999
3 changed files with 18 additions and 19 deletions

View File

@ -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 @bindThis
public async detectRequestType(_response: Response): Promise<{ public async detectRequestType(_response: Response, path?: string, fileSavingPromise: Promise<any> = Promise.resolve()): Promise<{
mime: string; mime: string;
ext: string | null; ext: string | null;
}> { }> {
const response = _response.clone(); const response = _response.clone();
// Check 0 byte
if (!response.body) { if (!response.body) {
throw new StatusError('No Body', 400, 'No 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)); const type = await fileTypeFromStream(stream.Readable.fromWeb(response.body));
if (type) { if (type) {
// XMLはSVGかもしれない
if (path && type.mime === 'application/xml') {
await fileSavingPromise;
if (await this.checkSvg(path)) {
return TYPE_SVG;
}
}
return { return {
mime: type.mime, mime: type.mime,
ext: type.ext, ext: type.ext,
}; };
} }
// 種類が不明でもSVGかもしれない
if (path) {
await fileSavingPromise;
if (await this.checkSvg(path)) return TYPE_SVG;
}
// 種類が不明なら application/octet-stream にする // 種類が不明なら application/octet-stream にする
return TYPE_OCTET_STREAM; return TYPE_OCTET_STREAM;
} }

View File

@ -111,14 +111,7 @@ export class FileServerService {
const response = await this.downloadService.fetchUrl(file.uri); const response = await this.downloadService.fetchUrl(file.uri);
const fileSaving = this.downloadService.pipeRequestToFile(response, path); const fileSaving = this.downloadService.pipeRequestToFile(response, path);
let { mime, ext } = await this.fileInfoService.detectRequestType(response); const { mime, ext } = await this.fileInfoService.detectRequestType(response, path, fileSaving);
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 convertFile = async () => { const convertFile = async () => {
if (isThumbnail) { if (isThumbnail) {

View File

@ -77,14 +77,7 @@ export class MediaProxyServerService {
const response = await this.downloadService.fetchUrl(url); const response = await this.downloadService.fetchUrl(url);
const fileSaving = this.downloadService.pipeRequestToFile(response, path); const fileSaving = this.downloadService.pipeRequestToFile(response, path);
let { mime, ext } = await this.fileInfoService.detectRequestType(response); const { mime, ext } = await this.fileInfoService.detectRequestType(response, path, fileSaving);
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 isConvertibleImage = isMimeImage(mime, 'sharp-convertible-image'); const isConvertibleImage = isMimeImage(mime, 'sharp-convertible-image');
const isAnimationConvertibleImage = isMimeImage(mime, 'sharp-animation-convertible-image'); const isAnimationConvertibleImage = isMimeImage(mime, 'sharp-animation-convertible-image');