From 48c6e4a8a6404f65f6584f8c73ba715b877dd96c Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Wed, 27 Jul 2022 19:46:57 +1200 Subject: [PATCH] Save proxied media with shortcode as filename --- src/lib/structures/TimelineBaseMethods.js | 4 ++-- src/lib/structures/TimelineEntry.js | 6 +++--- src/lib/utils/proxyurl.js | 10 ++++++---- src/site/api/proxy.js | 4 ++-- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/lib/structures/TimelineBaseMethods.js b/src/lib/structures/TimelineBaseMethods.js index 84d5c94..f92df1b 100644 --- a/src/lib/structures/TimelineBaseMethods.js +++ b/src/lib/structures/TimelineBaseMethods.js @@ -27,13 +27,13 @@ class TimelineBaseMethods { getDisplayUrlP() { let url = this.data.display_url - if (constants.proxy_media.image) url = proxyImage(url) + if (constants.proxy_media.image) url = proxyImage(url, null, this.data.shortcode) return url } getVideoUrlP() { let url = this.data.video_url - if (constants.proxy_media.video) url = proxyVideo(url) + if (constants.proxy_media.video) url = proxyVideo(url, this.data.shortcode) return url } diff --git a/src/lib/structures/TimelineEntry.js b/src/lib/structures/TimelineEntry.js index 55f644f..36d958d 100644 --- a/src/lib/structures/TimelineEntry.js +++ b/src/lib/structures/TimelineEntry.js @@ -173,7 +173,7 @@ class TimelineEntry extends TimelineBaseMethods { if (this.data.thumbnail_resources) { return this.data.thumbnail_resources.map(tr => { let src = tr.src - if (constants.proxy_media.thumbnail) src = proxyImage(tr.src, tr.config_width) + if (constants.proxy_media.thumbnail) src = proxyImage(tr.src, tr.config_width, this.data.shortcode) return `${src} ${tr.config_width}w` }).join(", ") } else { @@ -195,7 +195,7 @@ class TimelineEntry extends TimelineBaseMethods { if (tr.config_width >= size) break // don't proceed once we find one large enough } let src = found.src - if (constants.proxy_media.thumbnail) src = proxyImage(src, found.config_width) // force resize to config rather than requested + if (constants.proxy_media.thumbnail) src = proxyImage(src, found.config_width, this.data.shortcode) // force resize to config rather than requested return { config_width: found.config_width, config_height: found.config_height, @@ -203,7 +203,7 @@ class TimelineEntry extends TimelineBaseMethods { } } else if (this.data.thumbnail_src) { let src = this.data.thumbnail_src - if (constants.proxy_media.thumbnail) src = proxyImage(src, size) // force resize to requested + if (constants.proxy_media.thumbnail) src = proxyImage(src, size, this.data.shortcode) // force resize to requested return { config_width: size, // probably? config_height: size, diff --git a/src/lib/utils/proxyurl.js b/src/lib/utils/proxyurl.js index 6265297..ff5a6a5 100644 --- a/src/lib/utils/proxyurl.js +++ b/src/lib/utils/proxyurl.js @@ -26,11 +26,12 @@ function verifyURL(completeURL) { return {status: "ok", url} } -function proxyImage(url, width) { +function proxyImage(url, width, shortcode) { + shortcode = shortcode ? `/${shortcode}${width ? "-small" : ""}.jpg` : "" const params = new URLSearchParams() if (width) params.set("width", width) params.set("url", url) - return "/imageproxy?"+params.toString() + return `/imageproxy${shortcode}?${params.toString()}` } function proxyProfilePic(url, userID) { @@ -40,10 +41,11 @@ function proxyProfilePic(url, userID) { return "/imageproxy?"+params.toString() } -function proxyVideo(url) { +function proxyVideo(url, shortcode) { + shortcode = shortcode ? `/${shortcode}.jpg` : "" const params = new URLSearchParams() params.set("url", url) - return "/videoproxy?"+params.toString() + return `/videoproxy${shortcode}?${params.toString()}` } /** diff --git a/src/site/api/proxy.js b/src/site/api/proxy.js index b8156a9..f6709f3 100644 --- a/src/site/api/proxy.js +++ b/src/site/api/proxy.js @@ -49,7 +49,7 @@ async function proxyResource(url, suggestedHeaders = {}, refreshCallback = null) module.exports = [ { - route: "/imageproxy", methods: ["GET"], code: async (input) => { + route: "/imageproxy(/.*)?", methods: ["GET"], code: async (input) => { const verifyResult = verifyURL(input.url) if (verifyResult.status !== "ok") return verifyResult.value if (!["png", "jpg", "webp"].some(ext => verifyResult.url.pathname.endsWith(ext))) return [400, "URL extension is not allowed"] @@ -119,7 +119,7 @@ module.exports = [ } }, { - route: "/videoproxy", methods: ["GET"], code: async (input) => { + route: "/videoproxy(/.*)?", methods: ["GET"], code: async (input) => { const verifyResult = verifyURL(input.url) if (verifyResult.status !== "ok") return verifyResult.value const url = verifyResult.url