mirror of
https://github.com/iv-org/invidious.git
synced 2024-12-23 14:03:37 +00:00
routes: move error 404 logic to its own module
This commit is contained in:
parent
870350fd61
commit
88ea794fdb
@ -187,49 +187,7 @@ end
|
|||||||
Invidious::Routing.register_all
|
Invidious::Routing.register_all
|
||||||
|
|
||||||
error 404 do |env|
|
error 404 do |env|
|
||||||
if md = env.request.path.match(/^\/(?<id>([a-zA-Z0-9_-]{11})|(\w+))$/)
|
Invidious::Routes::ErrorRoutes.error_404(env)
|
||||||
item = md["id"]
|
|
||||||
|
|
||||||
# Check if item is branding URL e.g. https://youtube.com/gaming
|
|
||||||
response = YT_POOL.client &.get("/#{item}")
|
|
||||||
|
|
||||||
if response.status_code == 301
|
|
||||||
response = YT_POOL.client &.get(URI.parse(response.headers["Location"]).request_target)
|
|
||||||
end
|
|
||||||
|
|
||||||
if response.body.empty?
|
|
||||||
env.response.headers["Location"] = "/"
|
|
||||||
halt env, status_code: 302
|
|
||||||
end
|
|
||||||
|
|
||||||
html = XML.parse_html(response.body)
|
|
||||||
ucid = html.xpath_node(%q(//link[@rel="canonical"])).try &.["href"].split("/")[-1]
|
|
||||||
|
|
||||||
if ucid
|
|
||||||
env.response.headers["Location"] = "/channel/#{ucid}"
|
|
||||||
halt env, status_code: 302
|
|
||||||
end
|
|
||||||
|
|
||||||
params = [] of String
|
|
||||||
env.params.query.each do |k, v|
|
|
||||||
params << "#{k}=#{v}"
|
|
||||||
end
|
|
||||||
params = params.join("&")
|
|
||||||
|
|
||||||
url = "/watch?v=#{item}"
|
|
||||||
if !params.empty?
|
|
||||||
url += "&#{params}"
|
|
||||||
end
|
|
||||||
|
|
||||||
# Check if item is video ID
|
|
||||||
if item.match(/^[a-zA-Z0-9_-]{11}$/) && YT_POOL.client &.head("/watch?v=#{item}").status_code != 404
|
|
||||||
env.response.headers["Location"] = url
|
|
||||||
halt env, status_code: 302
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
env.response.headers["Location"] = "/"
|
|
||||||
halt env, status_code: 302
|
|
||||||
end
|
end
|
||||||
|
|
||||||
error 500 do |env, ex|
|
error 500 do |env, ex|
|
||||||
|
47
src/invidious/routes/errors.cr
Normal file
47
src/invidious/routes/errors.cr
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
module Invidious::Routes::ErrorRoutes
|
||||||
|
def self.error_404(env)
|
||||||
|
if md = env.request.path.match(/^\/(?<id>([a-zA-Z0-9_-]{11})|(\w+))$/)
|
||||||
|
item = md["id"]
|
||||||
|
|
||||||
|
# Check if item is branding URL e.g. https://youtube.com/gaming
|
||||||
|
response = YT_POOL.client &.get("/#{item}")
|
||||||
|
|
||||||
|
if response.status_code == 301
|
||||||
|
response = YT_POOL.client &.get(URI.parse(response.headers["Location"]).request_target)
|
||||||
|
end
|
||||||
|
|
||||||
|
if response.body.empty?
|
||||||
|
env.response.headers["Location"] = "/"
|
||||||
|
haltf env, status_code: 302
|
||||||
|
end
|
||||||
|
|
||||||
|
html = XML.parse_html(response.body)
|
||||||
|
ucid = html.xpath_node(%q(//link[@rel="canonical"])).try &.["href"].split("/")[-1]
|
||||||
|
|
||||||
|
if ucid
|
||||||
|
env.response.headers["Location"] = "/channel/#{ucid}"
|
||||||
|
haltf env, status_code: 302
|
||||||
|
end
|
||||||
|
|
||||||
|
params = [] of String
|
||||||
|
env.params.query.each do |k, v|
|
||||||
|
params << "#{k}=#{v}"
|
||||||
|
end
|
||||||
|
params = params.join("&")
|
||||||
|
|
||||||
|
url = "/watch?v=#{item}"
|
||||||
|
if !params.empty?
|
||||||
|
url += "&#{params}"
|
||||||
|
end
|
||||||
|
|
||||||
|
# Check if item is video ID
|
||||||
|
if item.match(/^[a-zA-Z0-9_-]{11}$/) && YT_POOL.client &.head("/watch?v=#{item}").status_code != 404
|
||||||
|
env.response.headers["Location"] = url
|
||||||
|
haltf env, status_code: 302
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
env.response.headers["Location"] = "/"
|
||||||
|
haltf env, status_code: 302
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user