Skip to content

Commit

Permalink
Refactor DownloadBehavior
Browse files Browse the repository at this point in the history
Extract derivative_download_options so that it's easier to override
  • Loading branch information
jcoyne committed Feb 25, 2016
1 parent 00162db commit 1b56745
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ def show
# For derivatives stored on the local file system
response.headers['Accept-Ranges'] = 'bytes'
response.headers['Content-Length'] = File.size(file).to_s
send_file file, type: mime_type_for(file), disposition: 'inline'
send_file file, derivative_download_options
else
render_404
end
end

protected

# Override this method if you want to change the options sent when downloading
# a derivative file
def derivative_download_options
{ type: mime_type_for(file), disposition: 'inline' }
end

# Customize the :download ability in your Ability class, or override this method
def authorize_download!
# authorize! :download, file # can't use this because Hydra::Ability#download_permissions assumes that files are in Basic Container (and thus include the asset's uri)
Expand Down
8 changes: 8 additions & 0 deletions spec/controllers/downloads_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,12 @@
end
end
end

describe "derivative_download_options" do
before do
allow(controller).to receive(:default_file).and_return 'world.png'
end
subject { controller.send(:derivative_download_options) }
it { is_expected.to eq(disposition: 'inline', type: 'image/png') }
end
end

0 comments on commit 1b56745

Please sign in to comment.