This commit is contained in:
tamaina 2023-01-06 16:29:45 +00:00
parent ca0025b499
commit 12afc294f7
4 changed files with 48 additions and 41 deletions

View file

@ -15,7 +15,8 @@ import { encode } from 'blurhash';
import { createTempDir } from '@/misc/create-temp.js';
import { AiService } from '@/core/AiService.js';
import { bindThis } from '@/decorators.js';
import type { Request } from 'got';
import { Response } from 'undici';
import { StatusError } from '@/misc/status-error.js';
const pipeline = util.promisify(stream.pipeline);
@ -343,18 +344,18 @@ export class FileInfoService {
* Detect MIME Type and extension by stream for performance (this cannot detect SVG)
*/
@bindThis
public async detectRequestType(request: Request): Promise<{
public async detectRequestType(_response: Response): Promise<{
mime: string;
ext: string | null;
}> {
const response = _response.clone();
// Check 0 byte
if ((request.response?.complete || request.closed) && !request.response?.rawBody?.length) {
return TYPE_OCTET_STREAM;
if (!response.body) {
throw new StatusError('No Body', 400, 'No Body');
}
const copied = request.pipe(new stream.PassThrough());
const type = await fileTypeFromStream(copied);
const type = await fileTypeFromStream(stream.Readable.fromWeb(response.body));
if (type) {
return {
@ -375,7 +376,7 @@ export class FileInfoService {
try {
const size = await this.getFileSize(path);
if (size > 1 * 1024 * 1024) return false;
return isSvg(await fs.promises.readFile(target));
return isSvg(await fs.promises.readFile(path));
} catch {
return false;
}