From 5f9031bec5f8111c4a218f63d34439f618a2843b Mon Sep 17 00:00:00 2001 From: Charlotte Van Petegem Date: Sun, 1 Oct 2023 19:25:54 +0200 Subject: [PATCH] Add download action for tracks (#487) --- app/controllers/tracks_controller.rb | 9 ++++++++- app/policies/track_policy.rb | 4 ++++ config/routes.rb | 1 + test/controllers/tracks_controller_test.rb | 10 ++++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/controllers/tracks_controller.rb b/app/controllers/tracks_controller.rb index 256b12fc..3cd17835 100644 --- a/app/controllers/tracks_controller.rb +++ b/app/controllers/tracks_controller.rb @@ -1,7 +1,7 @@ class TracksController < ApplicationController include ActionController::Live - before_action :set_track, only: %i[show update destroy audio merge] + before_action :set_track, only: %i[show update destroy audio download merge] has_scope :by_filter, as: 'filter' has_scope :by_album, as: 'album_id' @@ -78,6 +78,13 @@ def audio end end + def download + audio_file = @track.audio_file + raise ActiveRecord::RecordNotFound.new('track has no audio', 'audio') unless audio_file.present? && audio_file.check_self + + send_file audio_file.full_path + end + def merge @track.merge(Track.find(params[:source_id])) # We don't do error handling here. The merge action isn't supposed to fail. diff --git a/app/policies/track_policy.rb b/app/policies/track_policy.rb index ddc2fa6b..fb76a0ef 100644 --- a/app/policies/track_policy.rb +++ b/app/policies/track_policy.rb @@ -33,6 +33,10 @@ def audio? index? end + def download? + audio? + end + def merge? create? end diff --git a/config/routes.rb b/config/routes.rb index eebfea41..bdbcc522 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -159,6 +159,7 @@ end member do get 'audio' + get 'download' post 'merge' end end diff --git a/test/controllers/tracks_controller_test.rb b/test/controllers/tracks_controller_test.rb index f8acba4a..916d8e0c 100644 --- a/test/controllers/tracks_controller_test.rb +++ b/test/controllers/tracks_controller_test.rb @@ -257,6 +257,16 @@ class TracksControllerTest < ActionDispatch::IntegrationTest assert_response :success end + + test 'should download to user' do + location = Location.create(path: Rails.root.join('test/files')) + audio_file = create(:audio_file, location:, filename: '/base.flac') + track = create(:track, audio_file:) + + get download_track_url(track) + + assert_response :success + end end class TracksControllerAudioTest < ActionDispatch::IntegrationTest