From 9b1da926e1f3aa31bd438d2a1ca11cf88f3f335e Mon Sep 17 00:00:00 2001 From: tamaina Date: Tue, 7 Feb 2023 14:00:02 +0000 Subject: [PATCH] Better Convertible Handling --- built/index.js | 20 +++++++++++++------- package.json | 2 +- src/index.ts | 25 +++++++++++++++++-------- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/built/index.js b/built/index.js index 2a513ce..1433d3b 100644 --- a/built/index.js +++ b/built/index.js @@ -79,8 +79,18 @@ async function proxyHandler(request, reply) { try { const isConvertibleImage = isMimeImage(file.mime, 'sharp-convertible-image'); const isAnimationConvertibleImage = isMimeImage(file.mime, 'sharp-animation-convertible-image'); + if ('emoji' in request.query || + 'avatar' in request.query || + 'static' in request.query || + 'preview' in request.query || + 'badge' in request.query) { + if (!isConvertibleImage) { + // 画像でないなら404でお茶を濁す + throw new StatusError('Unexpected mime', 404); + } + } let image = null; - if (('emoji' in request.query || 'avatar' in request.query) && isConvertibleImage) { + if ('emoji' in request.query || 'avatar' in request.query) { if (!isAnimationConvertibleImage && !('static' in request.query)) { image = { data: fs.createReadStream(file.path), @@ -102,17 +112,13 @@ async function proxyHandler(request, reply) { }; } } - else if ('static' in request.query && isConvertibleImage) { + else if ('static' in request.query) { image = convertToWebpStream(file.path, 498, 280); } - else if ('preview' in request.query && isConvertibleImage) { + else if ('preview' in request.query) { image = convertToWebpStream(file.path, 200, 200); } else if ('badge' in request.query) { - if (!isConvertibleImage) { - // 画像でないなら404でお茶を濁す - throw new StatusError('Unexpected mime', 404); - } const mask = sharp(file.path) .resize(96, 96, { fit: 'inside', diff --git a/package.json b/package.json index 99f162a..3610791 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "misskey-media-proxy", - "version": "0.0.4", + "version": "0.0.5", "description": "The Media Proxy for Misskey", "main": "built/index.js", "packageManager": "pnpm@7.26.0", diff --git a/src/index.ts b/src/index.ts index 95cc408..2317d31 100644 --- a/src/index.ts +++ b/src/index.ts @@ -119,8 +119,22 @@ async function proxyHandler(request: FastifyRequest<{ Params: { url: string; }; const isConvertibleImage = isMimeImage(file.mime, 'sharp-convertible-image'); const isAnimationConvertibleImage = isMimeImage(file.mime, 'sharp-animation-convertible-image'); + if ( + 'emoji' in request.query || + 'avatar' in request.query || + 'static' in request.query || + 'preview' in request.query || + 'badge' in request.query + ) { + if (!isConvertibleImage) { + // 画像でないなら404でお茶を濁す + throw new StatusError('Unexpected mime', 404); + } + } + let image: IImageStreamable | null = null; - if (('emoji' in request.query || 'avatar' in request.query) && isConvertibleImage) { + + if ('emoji' in request.query || 'avatar' in request.query) { if (!isAnimationConvertibleImage && !('static' in request.query)) { image = { data: fs.createReadStream(file.path), @@ -141,16 +155,11 @@ async function proxyHandler(request: FastifyRequest<{ Params: { url: string; }; type: 'image/webp', }; } - } else if ('static' in request.query && isConvertibleImage) { + } else if ('static' in request.query) { image = convertToWebpStream(file.path, 498, 280); - } else if ('preview' in request.query && isConvertibleImage) { + } else if ('preview' in request.query) { image = convertToWebpStream(file.path, 200, 200); } else if ('badge' in request.query) { - if (!isConvertibleImage) { - // 画像でないなら404でお茶を濁す - throw new StatusError('Unexpected mime', 404); - } - const mask = sharp(file.path) .resize(96, 96, { fit: 'inside',