diff options
| author | Fuwn <[email protected]> | 2024-01-22 10:01:20 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-01-22 10:02:07 -0800 |
| commit | 4bb0f5b017e2d33c6e4d728f97f5f5e322012f61 (patch) | |
| tree | 8cc3cc7cd0adbd9a24f9d5b103efde7323a16324 /index.ts | |
| download | cdn.due.moe-4bb0f5b017e2d33c6e4d728f97f5f5e322012f61.tar.xz cdn.due.moe-4bb0f5b017e2d33c6e4d728f97f5f5e322012f61.zip | |
feat: initial release
Diffstat (limited to 'index.ts')
| -rw-r--r-- | index.ts | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..ae11e69 --- /dev/null +++ b/index.ts @@ -0,0 +1,106 @@ +// <https://github.com/soruly/trace.moe-www/blob/master/image-proxy.js> + +addEventListener("fetch", async (event) => { + event.respondWith(handleRequest(event.request)); +}); + +const errorResponse = (errorMessage) => + new Response(errorMessage, { + status: 400, + statusText: "Bad Request", + }); + +const handleRequest = async (originalRequest) => { + let originalURL = new URL(originalRequest.url); + if (!originalURL.searchParams.get("url")) { + return errorResponse("Error: Cannot get url from param"); + } + + let imageURL = null; + try { + imageURL = new URL(originalURL.searchParams.get("url")); + } catch (e) {} + if (!imageURL) { + return errorResponse("Error: Invalid URL string"); + } + + let imageRequest = new Request(imageURL, { + redirect: "follow", + headers: { + referer: imageURL.origin, + }, + }); + + let response = await fetch(imageRequest, { + cf: { + polish: "lossy", + }, + }); + + if (response.status >= 400) { + return new Response(response.body, { + status: response.status, + statusText: response.statusText, + headers: response.headers, + }); + } + + // if ( + // response.headers.get("Content-Type").toLowerCase() !== + // "application/octet-stream" && + // !["image", "video"].includes( + // response.headers.get("Content-Type").split("/")[0].toLowerCase() + // ) + // ) { + // // retry as bot to get og:image + // let webResponse = await fetch( + // new Request(imageURL, { + // redirect: "follow", + // headers: { + // referer: imageURL.origin, + // "User-Agent": "googlebot", + // }, + // }) + // ); + // if (response.status === 200) { + // const ogImageURL = (await webResponse.text()) + // ?.match(/<[^<]+?"og:image".*?>/, "$1")?.[0] + // ?.match(/content="(.*?)"/, "$1")?.[1]; + + // if (ogImageURL && ogImageURL.match(/^https?:\/\//)) { + // response = await fetch( + // new Request(ogImageURL, { + // redirect: "follow", + // headers: { + // referer: imageURL.origin, + // }, + // }) + // ); + // if (response.status >= 400) { + // return new Response(response.body, { + // status: response.status, + // statusText: response.statusText, + // headers: response.headers, + // }); + // } + // } + // } + // } + // if ( + // response.headers.get("Content-Type").toLowerCase() !== + // "application/octet-stream" && + // !["image", "video"].includes( + // response.headers.get("Content-Type").split("/")[0].toLowerCase() + // ) + // ) { + // return errorResponse( + // "Error: Content-Type is not image or video or application/octet-stream" + // ); + // } + + return new Response(response.body, { + status: response.status, + statusText: response.statusText, + headers: response.headers, + }); +}; |