Skip to content

Commit

Permalink
Add download action for tracks (#487)
Browse files Browse the repository at this point in the history
  • Loading branch information
chvp authored Oct 1, 2023
1 parent 7934f33 commit 5f9031b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/controllers/tracks_controller.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions app/policies/track_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def audio?
index?
end

def download?
audio?
end

def merge?
create?
end
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
end
member do
get 'audio'
get 'download'
post 'merge'
end
end
Expand Down
10 changes: 10 additions & 0 deletions test/controllers/tracks_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5f9031b

Please sign in to comment.