This commit is contained in:
tamaina 2023-01-01 17:18:57 +00:00
parent a4a614e180
commit 453ba25af2
5 changed files with 148 additions and 47 deletions

View file

@ -32,8 +32,8 @@ export class DownloadService {
}
@bindThis
public async downloadUrl(url: string, path: string): Promise<void> {
this.logger.info(`Downloading ${chalk.cyan(url)} to ${chalk.cyanBright(path)} ...`);
public gotUrl(url: string): Got.Request {
this.logger.info(`Downloading ${chalk.cyan(url)} ...`);
const timeout = 30 * 1000;
const operationTimeout = 60 * 1000;
@ -82,9 +82,16 @@ export class DownloadService {
req.destroy();
}
});
return req;
}
@bindThis
public async pipeRequestToFile(req: Got.Request, path: string): Promise<void> {
const copied = req.pipe(new stream.PassThrough());
try {
await pipeline(req, fs.createWriteStream(path));
this.logger.info(`Saving File to ${chalk.cyanBright(path)} from downloading ...`);
await pipeline(copied, fs.createWriteStream(path));
} catch (e) {
if (e instanceof Got.HTTPError) {
throw new StatusError(`${e.response.statusCode} ${e.response.statusMessage}`, e.response.statusCode, e.response.statusMessage);
@ -92,7 +99,11 @@ export class DownloadService {
throw e;
}
}
}
@bindThis
public async downloadUrl(url: string, path: string): Promise<void> {
await this.pipeRequestToFile(this.gotUrl(url), path);
this.logger.succ(`Download finished: ${chalk.cyan(url)}`);
}

View file

@ -5,7 +5,7 @@ import * as stream from 'node:stream';
import * as util from 'node:util';
import { Inject, Injectable } from '@nestjs/common';
import { FSWatcher } from 'chokidar';
import { fileTypeFromFile } from 'file-type';
import { fileTypeFromFile, fileTypeFromStream } from 'file-type';
import FFmpeg from 'fluent-ffmpeg';
import isSvg from 'is-svg';
import probeImageSize from 'probe-image-size';
@ -15,6 +15,7 @@ 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';
const pipeline = util.promisify(stream.pipeline);
@ -39,7 +40,7 @@ const TYPE_OCTET_STREAM = {
ext: null,
};
const TYPE_SVG = {
export const TYPE_SVG = {
mime: 'image/svg+xml',
ext: 'svg',
};
@ -306,9 +307,9 @@ export class FileInfoService {
*/
@bindThis
public async detectType(path: string): Promise<{
mime: string;
ext: string | null;
}> {
mime: string;
ext: string | null;
}> {
// Check 0 byte
const fileSize = await this.getFileSize(path);
if (fileSize === 0) {
@ -332,21 +333,54 @@ export class FileInfoService {
// 種類が不明でもSVGかもしれない
if (await this.checkSvg(path)) {
return TYPE_SVG;
}
}
// それでも種類が不明なら application/octet-stream にする
return TYPE_OCTET_STREAM;
}
/**
* Detect MIME Type and extension by stream for performance (this cannot detect SVG)
*/
@bindThis
public async detectRequestType(request: Request): Promise<{
mime: string;
ext: string | null;
}> {
// Check 0 byte
if ((request.response?.complete || request.closed) && !request.response?.rawBody?.length) {
return TYPE_OCTET_STREAM;
}
const copied = request.pipe(new stream.PassThrough());
const type = await fileTypeFromStream(copied);
if (type) {
return {
mime: type.mime,
ext: type.ext,
};
}
// 種類が不明なら application/octet-stream にする
return TYPE_OCTET_STREAM;
}
/**
* Check the file is SVG or not
*/
@bindThis
public async checkSvg(path: string) {
public async checkSvg(target: string | Buffer) {
try {
const size = await this.getFileSize(path);
if (size > 1 * 1024 * 1024) return false;
return isSvg(fs.readFileSync(path));
if (typeof target === 'string') {
const size = await this.getFileSize(target);
if (size > 1 * 1024 * 1024) return false;
return isSvg(await fs.promises.readFile(target));
} else {
if (target.length > 1 * 1024 * 1024) return false;
return isSvg(target);
}
} catch {
return false;
}

View file

@ -9,6 +9,14 @@ export type IImage = {
type: string;
};
export type IImageStream = {
data: NodeJS.ReadableStream;
ext: string | null;
type: string;
};
export type IImageStreamable = IImage | IImageStream;
export const webpDefault: sharp.WebpOptions = {
quality: 85,
alphaQuality: 95,
@ -62,6 +70,26 @@ export class ImageProcessingService {
* Convert to WebP
* with resize, remove metadata, resolve orientation, stop animation
*/
@bindThis
public convertSharpToWebpStream(sharp: sharp.Sharp, width: number, height: number, options: sharp.WebpOptions = webpDefault): sharp.Sharp {
return sharp
.resize(width, height, {
fit: 'inside',
withoutEnlargement: true,
})
.rotate()
.webp(options);
}
@bindThis
public convertSharpToWebpStreamObj(sharp: sharp.Sharp, width: number, height: number, options: sharp.WebpOptions = webpDefault): IImageStream {
return {
data: this.convertSharpToWebpStream(sharp, width, height, options),
ext: 'webp',
type: 'image/webp',
}
}
@bindThis
public async convertToWebp(path: string, width: number, height: number, options: sharp.WebpOptions = webpDefault): Promise<IImage> {
return this.convertSharpToWebp(await sharp(path), width, height, options);
@ -69,14 +97,7 @@ export class ImageProcessingService {
@bindThis
public async convertSharpToWebp(sharp: sharp.Sharp, width: number, height: number, options: sharp.WebpOptions = webpDefault): Promise<IImage> {
const data = await sharp
.resize(width, height, {
fit: 'inside',
withoutEnlargement: true,
})
.rotate()
.webp(options)
.toBuffer();
const data = await this.convertSharpToWebpStream(sharp, width, height, options).toBuffer();
return {
data,