Skip to content

Commit

Permalink
fix: some jpg is webp
Browse files Browse the repository at this point in the history
  • Loading branch information
rayriffy committed Nov 14, 2024
1 parent d714162 commit d28d9f3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
"overrides": {
"@rollup/plugin-node-resolve": "15.2.3",
"sharp": "0.33.0"
}
},
"packageManager": "[email protected]+sha512.73a29afa36a0d092ece5271de5177ecbf8318d454ecd701343131b8ebc0c1a91c487da46ab77c8e596d6acf1461e3594ced4becedf8921b074fbd8653ed7051c"
}
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"nanostores": "0.11.3",
"p-queue": "8.0.1",
"set-cookie-parser": "2.7.0",
"sharp": "0.33.0",
"sharp": "0.33.5",
"trpc-svelte-query": "2.1.0",
"zod": "3.23.8"
},
Expand Down
2 changes: 1 addition & 1 deletion web/src/core/services/getImageUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export const getImageUrl = (args: GetImageUrlArgs): string => {
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'}`
}
23 changes: 22 additions & 1 deletion web/src/routes/api/_image/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,32 @@ 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'],
allowedDomains:
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
}
}
}

0 comments on commit d28d9f3

Please sign in to comment.