refactor detectRequestType
This commit is contained in:
parent
00866c754e
commit
cdef862999
|
@ -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<any> = 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;
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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');
|
||||
|
||||
|
|
Loading…
Reference in a new issue