From 4cdb8f78cbb1035e88174ca432e5e7dfedb4eb44 Mon Sep 17 00:00:00 2001 From: decoy-walrus Date: Mon, 7 Feb 2022 14:01:05 -0500 Subject: [PATCH] Add new endpoint for original resolution images This change is to work around the issue that chromium based browsers have handling the "name=orig" parameter appended to URLs. This parameter is needed to retrieve the full resolution image from twitter, but causes those browsers to fill in "jpg_name=orig" as the extension on the filename. This change adds a new endpoint, "/pic/orig/". This new endpoint will internally fetch the URL with ":orig" appended on the end for the full res image. Externally, the endpoint will serve the image without the extra parameter to expose the real extension to the browser. This new endpoint is used when rendering tweets with attached images. The old endpoint is still in place for all other proxied images, and for any legacy links. I also updated the "?name=small" parameter to ":small" since that seems to be the new pattern for image sizing. This should fix issue #458. --- src/routes/media.nim | 14 ++++++++++++++ src/utils.nim | 6 ++++++ src/views/tweet.nim | 6 +++--- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/routes/media.nim b/src/routes/media.nim index c953a93..95446a1 100644 --- a/src/routes/media.nim +++ b/src/routes/media.nim @@ -88,6 +88,20 @@ proc createMediaRouter*(cfg: Config) = get "/pic/?": resp Http404 + get re"^\/pic\/orig\/(enc)?\/?(.+)": + var url = decoded(request, 1) + if "twimg.com" notin url: + url.insert(twimg) + if not url.startsWith(https): + url.insert(https) + url.add(":orig") + + let uri = parseUri(url) + cond isTwitterUrl(uri) == true + + let code = await proxyMedia(request, url) + check code + get re"^\/pic\/(enc)?\/?(.+)": var url = decoded(request, 1) if "twimg.com" notin url: diff --git a/src/utils.nim b/src/utils.nim index 9c8414d..9002bbf 100644 --- a/src/utils.nim +++ b/src/utils.nim @@ -42,6 +42,12 @@ proc getPicUrl*(link: string): string = else: &"/pic/{encodeUrl(link)}" +proc getOrigPicUrl*(link: string): string = + if base64Media: + &"/pic/orig/enc/{encode(link, safe=true)}" + else: + &"/pic/orig/{encodeUrl(link)}" + proc filterParams*(params: Table): seq[(string, string)] = for p in params.pairs(): if p[1].len > 0 and p[0] notin nitterParams: diff --git a/src/views/tweet.nim b/src/views/tweet.nim index 8b712a6..e64f959 100644 --- a/src/views/tweet.nim +++ b/src/views/tweet.nim @@ -57,9 +57,9 @@ proc renderAlbum(tweet: Tweet): VNode = tdiv(class="attachment image"): let named = "name=" in photo - orig = if named: photo else: photo & "?name=orig" - small = if named: photo else: photo & "?name=small" - a(href=getPicUrl(orig), class="still-image", target="_blank"): + orig = photo + small = if named: photo else: photo & ":small" + a(href=getOrigPicUrl(orig), class="still-image", target="_blank"): genImg(small) proc isPlaybackEnabled(prefs: Prefs; video: Video): bool =