From d28d9f348d01a140b13a64d70932678cb0847b20 Mon Sep 17 00:00:00 2001 From: Phumrapee Limpianchop Date: Thu, 14 Nov 2024 19:38:20 +0700 Subject: [PATCH] fix: some jpg is webp --- bun.lockb | Bin 270700 -> 270700 bytes package.json | 3 ++- web/package.json | 2 +- web/src/core/services/getImageUrl.ts | 2 +- web/src/routes/api/_image/+server.ts | 23 ++++++++++++++++++++++- 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/bun.lockb b/bun.lockb index 97fcdb2bb20715a6f6cf19e05c41567eef1b7e63..2b33ba1ad2333882f0c519d067c515417baf3418 100755 GIT binary patch delta 25 hcmaEJNZ`#Ofrb{wEljyP7){&rb}((v+rj)V0|1)73y1&! delta 25 hcmaEJNZ`#Ofrb{wEljyP7!BI { type === 'gallery' ? 'i' : 't' }.nhentai.net/galleries/${mediaId}/${ type === 'cover' ? 'cover' : `${page}${type === 'thumbnail' ? 't' : ''}` - }.${image.t === 'p' ? 'png' : image.t === 'g' ? 'gif' : 'jpg'}` + }.${image.t === 'p' ? 'png' : image.t === 'g' ? 'gif' : Number(mediaId) > 3110425 ? 'webp' : 'jpg'}` } diff --git a/web/src/routes/api/_image/+server.ts b/web/src/routes/api/_image/+server.ts index cc8333096..d6e00aa3c 100644 --- a/web/src/routes/api/_image/+server.ts +++ b/web/src/routes/api/_image/+server.ts @@ -3,6 +3,7 @@ import { env } from '$env/dynamic/private' import { createRequestHandler } from '@urami/core' import type { RequestHandler } from './$types' +import {URL} from "node:url"; const requestHandler = createRequestHandler({ remoteDomains: ['i.nhentai.net', 't.nhentai.net'], @@ -10,4 +11,24 @@ const requestHandler = createRequestHandler({ env.IMAGE_DOMAIN !== undefined ? [env.IMAGE_DOMAIN] : undefined, }) -export const GET: RequestHandler = ({ request }) => requestHandler(request) +export const GET: RequestHandler = async ({ request }) => { + try { + const response = await requestHandler(request) + + if (response.ok) + return response + else + throw response + } catch (error) { + // get current search params, get 'url' params. if .webp at the end then replace with .jpg. if .jpg at the end then replace with .webp. otherwise do nothing + const fullRequestUrl = new URL(request.url) + const url = fullRequestUrl.searchParams.get('url') ?? '' + + if (['.jpg', '.webp'].some(ext => url.endsWith(ext))) { + fullRequestUrl.searchParams.set('url', url.endsWith('.jpg') ? url.replace('.jpg', '.webp') : url.replace('.webp', '.jpg')) + return requestHandler(new Request(fullRequestUrl.toString())) + } else { + return error as Response + } + } +}