Skip to content

Commit

Permalink
Send the stored filename when using content-addressed storage
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Aug 5, 2024
1 parent ba34404 commit f045f78
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/controllers/file_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def show
ip: request.remote_ip
)

send_file current_file.path, disposition:
send_file current_file.path, filename: current_file.file_name, disposition:
end
# rubocop:enable Metrics/AbcSize

Expand Down
2 changes: 1 addition & 1 deletion app/models/storage_root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def absolute_path
def content_addressable_path
@content_addressable_path ||= begin
md5 = @cocina.find_file_md5(@file_name)
File.join(Settings.stacks.content_addressable_storage_root, @treeified_id, @cocina.druid, 'content', md5)
File.join(Settings.stacks.storage_root, @treeified_id, @cocina.druid, 'content', md5)
end
end
end
Expand Down
1 change: 0 additions & 1 deletion config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ features:

stacks:
storage_root: /stacks
content_addressable_storage_root: /stacks/content_addressable

imageserver:
base_uri: "http://imageserver-prod.stanford.edu/iiif/2/"
Expand Down
36 changes: 31 additions & 5 deletions spec/controllers/file_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,40 @@
subject { get :show, params: { id: druid, file_name: 'image.jp2' } }

it 'sends the file to the user' do
expect(controller).to receive(:send_file).with(path, disposition: :inline).and_call_original
expect(controller).to receive(:send_file).with(path, filename: 'image.jp2', disposition: :inline).and_call_original
subject
end

it 'sends headers for content' do
expect(controller).to receive(:send_file).with(path, disposition: :attachment).and_call_original
get :show, params: { id: druid, file_name: 'image.jp2', download: 'any' }
expect(response.headers.to_h).to include 'content-length' => 11_043, 'accept-ranges' => 'bytes'
context 'when file is not in a content addressable path' do
it 'returns legacy file' do
expect(controller).to receive(:send_file).with(path, filename: 'image.jp2', disposition: :attachment).and_call_original
get :show, params: { id: druid, file_name: 'image.jp2', download: 'any' }
expect(response.headers.to_h).to include(
'content-length' => 11_043,
'accept-ranges' => 'bytes',
"content-disposition" => "attachment; filename=\"image.jp2\"; filename*=UTF-8''image.jp2"
)
end
end

context 'when file is in a content addressable path' do
let(:path) { 'spec/fixtures/nr/349/ct/7889/nr349ct7889/content/02f77c96c40ad3c7c843baa9c7b2ff2c' }
around do |ex|
FileUtils.mkdir_p('spec/fixtures/nr/349/ct/7889/nr349ct7889/content/')
File.link('spec/fixtures/nr/349/ct/7889/image.jp2', path)
ex.run
File.unlink(path)
end

it 'sends headers for content' do
expect(controller).to receive(:send_file).with(path, filename: 'image.jp2', disposition: :attachment).and_call_original
get :show, params: { id: druid, file_name: 'image.jp2', download: 'any' }
expect(response.headers.to_h).to include(
'content-length' => 11_043,
'accept-ranges' => 'bytes',
"content-disposition" => "attachment; filename=\"image.jp2\"; filename*=UTF-8''image.jp2"
)
end
end

it 'missing file returns 404 Not Found' do
Expand Down
6 changes: 4 additions & 2 deletions spec/requests/file_auth_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
context 'webauthed user' do
it 'allows when user webauthed and authorized' do
allow_any_instance_of(FileController).to receive(:current_user).and_return(user_webauth_stanford_no_loc)
expect_any_instance_of(FileController).to receive(:send_file).with(stacks_file.path, disposition: :inline).and_call_original
expect_any_instance_of(FileController).to receive(:send_file).with(stacks_file.path, filename: 'image.jp2',
disposition: :inline).and_call_original
get "/file/#{druid}/#{file_name}"
end

Expand All @@ -56,7 +57,8 @@

it 'allows when user in location' do
allow_any_instance_of(FileController).to receive(:current_user).and_return(user_loc_no_webauth)
expect_any_instance_of(FileController).to receive(:send_file).with(stacks_file.path, disposition: :inline).and_call_original
expect_any_instance_of(FileController).to receive(:send_file).with(stacks_file.path, filename: 'image.jp2',
disposition: :inline).and_call_original
get "/file/#{druid}/#{file_name}"
end

Expand Down
2 changes: 1 addition & 1 deletion spec/requests/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

before do
allow_any_instance_of(FileController).to receive(:send_file)
.with('spec/fixtures/nr/349/ct/7889/path/to/image.jp2', disposition: :inline)
.with('spec/fixtures/nr/349/ct/7889/path/to/image.jp2', filename: 'path/to/image.jp2', disposition: :inline)
end

it 'returns a successful HTTP response' do
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/versioned_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

before do
allow_any_instance_of(FileController).to receive(:send_file)
.with('spec/fixtures/nr/349/ct/7889/path/to/image.jp2', disposition: :inline)
.with('spec/fixtures/nr/349/ct/7889/path/to/image.jp2', filename: 'path/to/image.jp2', disposition: :inline)
stub_request(:get, "https://purl.stanford.edu/#{druid}/#{version_id}.json")
.to_return(status: 200, body: public_json.to_json)
end
Expand Down

0 comments on commit f045f78

Please sign in to comment.