From fe057c78737458132248e39b7ee7572b67f26918 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Tue, 22 Feb 2022 17:42:41 +0100 Subject: [PATCH 1/3] Make a function that builds the download widget's HTML --- src/invidious.cr | 2 + src/invidious/frontend/watch_page.cr | 108 +++++++++++++++++++++++++++ src/invidious/routes/watch.cr | 8 ++ src/invidious/views/watch.ecr | 36 +-------- 4 files changed, 119 insertions(+), 35 deletions(-) create mode 100644 src/invidious/frontend/watch_page.cr diff --git a/src/invidious.cr b/src/invidious.cr index d4878759..d742cd59 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -29,6 +29,8 @@ require "protodec/utils" require "./invidious/database/*" require "./invidious/helpers/*" require "./invidious/yt_backend/*" +require "./invidious/frontend/*" + require "./invidious/*" require "./invidious/channels/*" require "./invidious/user/*" diff --git a/src/invidious/frontend/watch_page.cr b/src/invidious/frontend/watch_page.cr new file mode 100644 index 00000000..d3a50705 --- /dev/null +++ b/src/invidious/frontend/watch_page.cr @@ -0,0 +1,108 @@ +module Invidious::Frontend::WatchPage + extend self + + # A handy structure to pass many elements at + # once to the download widget function + struct VideoAssets + getter full_videos : Array(Hash(String, JSON::Any)) + getter video_streams : Array(Hash(String, JSON::Any)) + getter audio_streams : Array(Hash(String, JSON::Any)) + getter captions : Array(Caption) + + def initialize( + @full_videos, + @video_streams, + @audio_streams, + @captions + ) + end + end + + def download_widget(locale : String, video : Video, video_assets : VideoAssets) : String + if CONFIG.disabled?("downloads") + return "

#{translate(locale, "Download is disabled.")}

" + end + + return String.build(4000) do |str| + str << "" + str << '\n' + + str << "\t
\n" + + str << "\t\t\n" + + # TODO: remove inline style + str << "\t\t\n" + str << "\t
\n" + + str << "\t\n" + + str << "\n" + end + end +end diff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.cr index 42bc4219..c34ce715 100644 --- a/src/invidious/routes/watch.cr +++ b/src/invidious/routes/watch.cr @@ -189,6 +189,14 @@ module Invidious::Routes::Watch return env.redirect url end + # Structure used for the download widget + video_assets = Invidious::Frontend::WatchPage::VideoAssets.new( + full_videos: fmt_stream, + video_streams: video_streams, + audio_streams: audio_streams, + captions: video.captions + ) + templated "watch" end diff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecr index 2e0aee99..0e4af3ab 100644 --- a/src/invidious/views/watch.ecr +++ b/src/invidious/views/watch.ecr @@ -168,41 +168,7 @@ we're going to need to do it here in order to allow for translations. <% end %> <% end %> - <% if CONFIG.dmca_content.includes?(video.id) || CONFIG.disabled?("downloads") %> -

<%= translate(locale, "Download is disabled.") %>

- <% else %> -
-
- - -
- - -
- <% end %> + <%= Invidious::Frontend::WatchPage.download_widget(locale, video, video_assets) %>

<%= number_with_separator(video.views) %>

<%= number_with_separator(video.likes) %>

From 2f335b3d2c2805d5de1b0204920c439b87f5646b Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Tue, 22 Feb 2022 18:11:11 +0100 Subject: [PATCH 2/3] Use a dedicated endpoind for downloads This allows us to not pass file name ("title") in the form data and to enforce some sanity checks --- src/invidious.cr | 3 ++ src/invidious/frontend/watch_page.cr | 20 +++++------ src/invidious/routes/api/v1/videos.cr | 6 +++- src/invidious/routes/video_playback.cr | 36 +++++++++---------- src/invidious/routes/watch.cr | 48 ++++++++++++++++++++++++++ 5 files changed, 82 insertions(+), 31 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index d742cd59..d1c3ac83 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -236,6 +236,7 @@ before_all do |env| "/api/manifest/", "/videoplayback", "/latest_version", + "/download", }.any? { |r| env.request.resource.starts_with? r } if env.request.cookies.has_key? "SID" @@ -348,6 +349,8 @@ end Invidious::Routing.get "/e/:id", Invidious::Routes::Watch, :redirect Invidious::Routing.get "/redirect", Invidious::Routes::Misc, :cross_instance_redirect + Invidious::Routing.post "/download", Invidious::Routes::Watch, :download + Invidious::Routing.get "/embed/", Invidious::Routes::Embed, :redirect Invidious::Routing.get "/embed/:id", Invidious::Routes::Embed, :show diff --git a/src/invidious/frontend/watch_page.cr b/src/invidious/frontend/watch_page.cr index d3a50705..80b67641 100644 --- a/src/invidious/frontend/watch_page.cr +++ b/src/invidious/frontend/watch_page.cr @@ -26,12 +26,16 @@ module Invidious::Frontend::WatchPage return String.build(4000) do |str| str << "" str << '\n' + # Hidden inputs for video id and title + str << "\n" + str << "\n" + str << "\t
\n" str << "\t\t