diff --git a/api/assetProxy.js b/api/assetProxy.js deleted file mode 100644 index 115e965..0000000 --- a/api/assetProxy.js +++ /dev/null @@ -1,60 +0,0 @@ -import util from "util"; -import stream from "stream"; -import fetch from "node-fetch"; - -const streamPipeline = util.promisify(stream.pipeline); - -const VALID_URL_PATTERNS = [ - /^http:\/\/images\.neopets\.com\/items\/[a-zA-Z0-9_ -]+\.gif$/, - /^http:\/\/images\.neopets\.com\/cp\/(bio|items)\/data\/[0-9]{3}\/[0-9]{3}\/[0-9]{3}\/[a-f0-9_]+\/[a-zA-Z0-9_ \-\/]+\.(svg|png|js)(\?[0-9]*)?$/, - /^http:\/\/images\.neopets\.com\/cp\/(bio|items)\/swf\/[0-9]{3}\/[0-9]{3}\/[0-9]{3}\/[a-f0-9_]+\.swf$/, - - // These ones aren't actually used Impress 2020 - we added a cheap hack to - // old Impress to upgrade images to use the Impress 2020 asset proxy 😅 - /^http:\/\/pets\.neopets\.com\/cp\/[a-zA-Z90-9]+\/[0-9]+\/[0-9]+\.png$/, - /^http:\/\/pets\.neopets\.com\/cpn\/[a-zA-Z90-9_]+\/[0-9]+\/[0-9]+\.png$/, -]; - -export default async (req, res) => { - const urlToProxy = req.query.url; - if (!urlToProxy) { - return res - .status(400) - .send("Bad request: Must provide `?url` in the query string"); - } - - if (!VALID_URL_PATTERNS.some((p) => urlToProxy.match(p))) { - return res - .status(400) - .send("Bad request: URL did not match any valid patterns"); - } - - console.debug("[assetProxy] 💌 Sending: %s", urlToProxy); - - const proxyRes = await fetch(urlToProxy); - console.debug( - `[assetProxy] %s %s: %s`, - proxyRes.ok ? "✅" : "🛑", - `${proxyRes.status} ${proxyRes.statusText}`.padStart(7, " "), - urlToProxy - ); - - res.status(proxyRes.status); - - res.setHeader("Content-Type", proxyRes.headers.get("Content-Type")); - res.setHeader("Cache-Control", proxyRes.headers.get("Cache-Control")); - res.setHeader("ETag", proxyRes.headers.get("ETag")); - res.setHeader("Last-Modified", proxyRes.headers.get("Last-Modified")); - - if (!proxyRes.headers.get("Content-Encoding")) { - // If the content is not encoded (I think their images generally aren't?), - // stream the body directly, to speed things up a bit. - res.setHeader("Content-Length", proxyRes.headers.get("Content-Length")); - await streamPipeline(proxyRes.body, res); - } else { - // Otherwise, I don't immediately know how to stream encoded content, so, - // let's just await the full body and send it the normal way. - const buffer = await proxyRes.buffer(); - res.send(buffer); - } -}; diff --git a/public/animate-test.html b/public/animate-test.html deleted file mode 100644 index 9752391..0000000 --- a/public/animate-test.html +++ /dev/null @@ -1,150 +0,0 @@ - - - - - - - - -
-
-
- -
-
-
- - -
-
- FPS: … -
- - - diff --git a/src/app/WardrobePage/support/ItemLayerSupportUploadModal.js b/src/app/WardrobePage/support/ItemLayerSupportUploadModal.js index b1a4619..bfde694 100644 --- a/src/app/WardrobePage/support/ItemLayerSupportUploadModal.js +++ b/src/app/WardrobePage/support/ItemLayerSupportUploadModal.js @@ -15,6 +15,7 @@ import { } from "@chakra-ui/core"; import { ExternalLinkIcon } from "@chakra-ui/icons"; +import { safeImageUrl } from "../../util"; import useSupport from "./useSupport"; /** @@ -412,7 +413,7 @@ function ItemLayerSupportFlashPlayer({ swfUrl, backgroundColor }) { > diff --git a/src/app/util.js b/src/app/util.js index 6d95951..136ed09 100644 --- a/src/app/util.js +++ b/src/app/util.js @@ -64,16 +64,26 @@ export function Heading2({ children, ...props }) { /** * safeImageUrl returns an HTTPS-safe image URL for Neopets assets! */ -export function safeImageUrl(url) { - let safeUrl = `/api/assetProxy?url=${encodeURIComponent(url)}`; +export function safeImageUrl(urlString) { + const url = new URL(urlString); - // On our Storybook server, we need to request from the main dev server. - const { host } = document.location; - if (host === "localhost:6006") { - safeUrl = "http://localhost:3000" + safeUrl; + if (url.origin === "http://images.neopets.com") { + url.protocol = "https:"; + url.host = "images.neopets-asset-proxy.openneo.net"; + } else if (url.origin === "http://pets.neopets.com") { + url.protocol = "https:"; + url.host = "pets.neopets-asset-proxy.openneo.net"; } - return safeUrl; + if (url.protocol !== "https:") { + console.warn( + "safeImageUrl was provided an unsafe URL, but we don't know how to " + + "upgrade it to HTTPS. Returning as-is: " + + urlString + ); + } + + return url.toString(); } /**