Refactor duplicate logic in image routes

This commit is contained in:
syeopite 2023-12-08 18:42:40 -08:00
parent 480e073fa9
commit 52bc9aa328
No known key found for this signature in database
GPG Key ID: A73C186DA3955A1A

View File

@ -11,29 +11,9 @@ module Invidious::Routes::Images
end
end
# We're encapsulating this into a proc in order to easily reuse this
# portion of the code for each request block below.
request_proc = ->(response : HTTP::Client::Response) {
env.response.status_code = response.status_code
response.headers.each do |key, value|
if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase)
env.response.headers[key] = value
end
end
env.response.headers["Access-Control-Allow-Origin"] = "*"
if response.status_code >= 300
env.response.headers.delete("Transfer-Encoding")
return
end
proxy_file(response, env)
}
begin
GGPHT_POOL.client &.get(url) do |resp|
return request_proc.call(resp)
return self.proxy_image(env, resp)
end
rescue ex
end
@ -61,27 +41,9 @@ module Invidious::Routes::Images
end
end
request_proc = ->(response : HTTP::Client::Response) {
env.response.status_code = response.status_code
response.headers.each do |key, value|
if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase)
env.response.headers[key] = value
end
end
env.response.headers["Connection"] = "close"
env.response.headers["Access-Control-Allow-Origin"] = "*"
if response.status_code >= 300
return env.response.headers.delete("Transfer-Encoding")
end
proxy_file(response, env)
}
begin
get_ytimg_pool(authority).client &.get(url) do |resp|
return request_proc.call(resp)
return self.proxy_image(env, resp)
end
rescue ex
end
@ -101,26 +63,9 @@ module Invidious::Routes::Images
end
end
request_proc = ->(response : HTTP::Client::Response) {
env.response.status_code = response.status_code
response.headers.each do |key, value|
if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase)
env.response.headers[key] = value
end
end
env.response.headers["Access-Control-Allow-Origin"] = "*"
if response.status_code >= 300 && response.status_code != 404
return env.response.headers.delete("Transfer-Encoding")
end
proxy_file(response, env)
}
begin
get_ytimg_pool("i9").client &.get(url) do |resp|
return request_proc.call(resp)
return self.proxy_image(env, resp)
end
rescue ex
end
@ -180,7 +125,15 @@ module Invidious::Routes::Images
end
end
request_proc = ->(response : HTTP::Client::Response) {
begin
get_ytimg_pool("i").client &.get(url) do |resp|
return self.proxy_image(env, resp)
end
rescue ex
end
end
private def self.proxy_image(env, response)
env.response.status_code = response.status_code
response.headers.each do |key, value|
if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase)
@ -190,18 +143,10 @@ module Invidious::Routes::Images
env.response.headers["Access-Control-Allow-Origin"] = "*"
if response.status_code >= 300 && response.status_code != 404
if response.status_code >= 300
return env.response.headers.delete("Transfer-Encoding")
end
proxy_file(response, env)
}
begin
get_ytimg_pool("i").client &.get(url) do |resp|
return request_proc.call(resp)
end
rescue ex
end
return proxy_file(response, env)
end
end