Update watch.cr

JSON::ParseException
This commit is contained in:
Atilla.js ⌐■_■ 2023-09-07 14:06:26 +02:00 committed by GitHub
parent ac0c0609bb
commit fb7f4ced04
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -294,15 +294,28 @@ module Invidious::Routes::Watch
return error_template(403, "Administrator has disabled this endpoint.")
end
title = env.params.body["title"]? || ""
video_id = env.params.body["id"]? || ""
selection = env.params.body["download_widget"]?
title = env.params.body["title"] || ""
video_id = env.params.body["id"] || ""
selection = env.params.body["download_widget"]
if title.empty? || video_id.empty? || selection.nil?
return error_template(400, "Missing form data")
end
download_widget = JSON.parse(selection)
begin
# Check if the JSON data starts with '{' (indicating an object) or '[' (indicating an array).
json_data = selection.strip
if json_data.start_with?('{', '[')
download_widget = JSON.parse(json_data)
# Your existing code for processing the JSON data goes here
else
# Handle the case where the JSON data is not correctly formatted.
return error_template(400, "Invalid JSON format")
end
rescue JSON::ParserError => e
# Handle any JSON parsing errors
return error_template(400, "Invalid JSON: #{e.message}")
end
extension = download_widget["ext"].as_s
filename = "#{title}-#{video_id}.#{extension}"
@ -313,7 +326,7 @@ module Invidious::Routes::Watch
env.params.body.delete("download_widget")
# Pass form parameters as URL parameters for the handlers of both
# /latest_version and /api/v1/captions. This avoids an un-necessary
# /latest_version and /api/v1/captions. This avoids an unnecessary
# redirect and duplicated (and hazardous) sanity checks.
if label = download_widget["label"]?
# URL params specific to /api/v1/captions/:id
@ -334,4 +347,5 @@ module Invidious::Routes::Watch
return error_template(400, "Invalid label or itag")
end
end
end