mirror of
https://gitea.invidious.io/iv-org/invidious.git
synced 2024-08-15 00:53:41 +00:00
Routes: use 'resolve_url()' in 404 error route
This commit is contained in:
parent
1ae14cc224
commit
8de8f23481
1 changed files with 18 additions and 38 deletions
|
@ -5,48 +5,28 @@ module Invidious::Routes::ErrorRoutes
|
|||
return env.redirect "#{env.request.path[15..]}?#{env.params.query}"
|
||||
end
|
||||
|
||||
if md = env.request.path.match(/^\/(?<id>([a-zA-Z0-9_-]{11})|(\w+))$/)
|
||||
item = md["id"]
|
||||
|
||||
if match = env.request.path.match(/^\/(?<id>[a-zA-Z0-9_-]{11})$/)
|
||||
# NOTE: we assume that a 11 chars long path is a video ID
|
||||
# to spare a call to 'resolve_url' and improve response time.
|
||||
id = match["id"]
|
||||
url = HttpServer::Utils.add_params_to_url("/watch?v=#{id}", env.params.query)
|
||||
return env.redirect url.to_s
|
||||
#
|
||||
elsif match = env.request.path.match(/^\/(?<name>\w+)$/)
|
||||
# Check if item is branding URL e.g. https://youtube.com/gaming
|
||||
response = YT_POOL.client &.get("/#{item}")
|
||||
begin
|
||||
response = YoutubeAPI.resolve_url("https://youtube.com/#{env.request.path}")
|
||||
endpoint = response["endpoint"]
|
||||
|
||||
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
|
||||
if ucid = endpoint.dig?("browseEndpoint", "browseId")
|
||||
url = HttpServer::Utils.add_params_to_url("/channel/#{ucid}", env.params.query)
|
||||
return env.redirect url.to_s
|
||||
end
|
||||
rescue ex
|
||||
end
|
||||
end
|
||||
|
||||
env.response.headers["Location"] = "/"
|
||||
haltf env, status_code: 302
|
||||
# TODO: create a proper 404 page
|
||||
haltf env, status_code: 404
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue