From e8c196d1c6cdbe1f13b317761d4ffe469a2e1e56 Mon Sep 17 00:00:00 2001 From: tamaina Date: Sun, 15 Jan 2023 15:16:36 +0000 Subject: [PATCH] refactor --- packages/backend/src/core/ImageProcessingService.ts | 8 ++++++++ packages/backend/src/server/FileServerService.ts | 12 +++++------- .../backend/src/server/MediaProxyServerService.ts | 9 ++++----- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/packages/backend/src/core/ImageProcessingService.ts b/packages/backend/src/core/ImageProcessingService.ts index 3bfb171ebc..8083d72c51 100644 --- a/packages/backend/src/core/ImageProcessingService.ts +++ b/packages/backend/src/core/ImageProcessingService.ts @@ -2,6 +2,8 @@ import { Inject, Injectable } from '@nestjs/common'; import sharp from 'sharp'; import { DI } from '@/di-symbols.js'; import type { Config } from '@/config.js'; +import { ReadableStream } from 'node:stream/web'; +import { Readable } from 'node:stream'; export type IImage = { data: Buffer; @@ -90,6 +92,12 @@ export class ImageProcessingService { } } + @bindThis + public convertToWebpFromWebReadable(readable: ReadableStream | null, width: number, height: number, options: sharp.WebpOptions = webpDefault): IImageStream { + if (readable == null) throw new Error('Input is null'); + return this.convertSharpToWebpStreamObj(Readable.fromWeb(readable).pipe(sharp()), width, height, options); + } + @bindThis public async convertToWebp(path: string, width: number, height: number, options: sharp.WebpOptions = webpDefault): Promise { return this.convertSharpToWebp(await sharp(path), width, height, options); diff --git a/packages/backend/src/server/FileServerService.ts b/packages/backend/src/server/FileServerService.ts index 56da144b49..0bff20bf7f 100644 --- a/packages/backend/src/server/FileServerService.ts +++ b/packages/backend/src/server/FileServerService.ts @@ -20,9 +20,7 @@ import { FileInfoService, TYPE_SVG } from '@/core/FileInfoService.js'; import { LoggerService } from '@/core/LoggerService.js'; import { bindThis } from '@/decorators.js'; import type { FastifyInstance, FastifyRequest, FastifyReply, FastifyPluginOptions } from 'fastify'; -import { PassThrough, Readable } from 'node:stream'; -import sharp from 'sharp'; -import { Request } from 'got'; +import { Readable } from 'node:stream'; const _filename = fileURLToPath(import.meta.url); const _dirname = dirname(_filename); @@ -126,8 +124,8 @@ export class FileServerService { const convertFile = async () => { if (isThumbnail) { if (['image/jpeg', 'image/webp', 'image/avif', 'image/png', 'image/svg+xml'].includes(mime)) { - return this.imageProcessingService.convertSharpToWebpStreamObj( - Readable.fromWeb(response.body).pipe(sharp()), + return this.imageProcessingService.convertToWebpFromWebReadable( + response.body, 498, 280 ); @@ -140,8 +138,8 @@ export class FileServerService { if (isWebpublic) { if (['image/svg+xml'].includes(mime)) { return { - data: this.imageProcessingService.convertSharpToWebpStream( - Readable.fromWeb(response.body).pipe(sharp()), + data: this.imageProcessingService.convertToWebpFromWebReadable( + response.body, 2048, 2048, { ...webpDefault, lossless: true } diff --git a/packages/backend/src/server/MediaProxyServerService.ts b/packages/backend/src/server/MediaProxyServerService.ts index 4ea44c0cbf..893499f4dd 100644 --- a/packages/backend/src/server/MediaProxyServerService.ts +++ b/packages/backend/src/server/MediaProxyServerService.ts @@ -18,8 +18,7 @@ import { FileInfoService, TYPE_SVG } from '@/core/FileInfoService.js'; import { LoggerService } from '@/core/LoggerService.js'; import { bindThis } from '@/decorators.js'; import type { FastifyInstance, FastifyPluginOptions, FastifyReply, FastifyRequest } from 'fastify'; -import { PassThrough, Readable, pipeline } from 'node:stream'; -import { Request } from 'got'; +import { Readable, pipeline } from 'node:stream'; const _filename = fileURLToPath(import.meta.url); const _dirname = dirname(_filename); @@ -123,9 +122,9 @@ export class MediaProxyServerService { }; } } else if ('static' in request.query && isConvertibleImage) { - image = this.imageProcessingService.convertSharpToWebpStreamObj(Readable.fromWeb(response.body).pipe(sharp()), 498, 280); + image = this.imageProcessingService.convertToWebpFromWebReadable(response.body, 498, 280); } else if ('preview' in request.query && isConvertibleImage) { - image = this.imageProcessingService.convertSharpToWebpStreamObj(Readable.fromWeb(response.body).pipe(sharp()), 200, 200); + image = this.imageProcessingService.convertToWebpFromWebReadable(response.body, 200, 200); } else if ('badge' in request.query) { if (!isConvertibleImage) { // 画像でないなら404でお茶を濁す @@ -164,7 +163,7 @@ export class MediaProxyServerService { type: 'image/png', }; } else if (mime === 'image/svg+xml') { - image = this.imageProcessingService.convertSharpToWebpStreamObj(Readable.fromWeb(response.body).pipe(sharp()), 2048, 2048); + image = this.imageProcessingService.convertToWebpFromWebReadable(response.body, 2048, 2048); } else if (!mime.startsWith('image/') || !FILE_TYPE_BROWSERSAFE.includes(mime)) { throw new StatusError('Rejected type', 403, 'Rejected type'); }