This repository has been archived by the owner on Jan 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #859 from psu-libraries/i#649-file-downloads
DownloadsController for files
- Loading branch information
Showing
11 changed files
with
341 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
module Repository | ||
class Download | ||
attr_reader :id, :use | ||
|
||
def initialize(id:, use: nil) | ||
@id = id | ||
@use = use | ||
end | ||
|
||
def available? | ||
file.present? && Pathname.new(file.path).exist? | ||
end | ||
|
||
def file | ||
@file ||= begin | ||
return if file_set.nil? | ||
select_file | ||
end | ||
end | ||
|
||
delegate :path, to: :file | ||
|
||
private | ||
|
||
def select_file | ||
case use | ||
when 'PreservationMasterFile' | ||
preservation_file | ||
when 'ServiceFile' | ||
file_set.service | ||
when 'AccessFile' | ||
file_set.access | ||
else | ||
default_file | ||
end | ||
end | ||
|
||
def preservation_file | ||
file_set.preservation_redacted || file_set.preservation | ||
end | ||
|
||
def default_file | ||
file_set.service || preservation_file | ||
end | ||
|
||
def file_set | ||
@file_set ||= Valkyrie.config.metadata_adapter.query_service.find_by(id: Valkyrie::ID.new(id)) | ||
rescue Valkyrie::Persistence::ObjectNotFoundError | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module Repository | ||
class DownloadsController < ApplicationController | ||
def download | ||
if resource.available? | ||
send_file(resource.path) | ||
else | ||
render file: 'public/404.html', status: 404, layout: false | ||
end | ||
end | ||
|
||
private | ||
|
||
def resource | ||
@resource ||= Download.new(id: params.fetch(:id), use: params.fetch(:use_type, nil)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Repository::Download, type: :model do | ||
describe '#available?' do | ||
subject { described_class.new(id: file_set.id) } | ||
|
||
context 'when the file set has a file that is present on the system' do | ||
let(:file_set) { create(:file_set, :with_member_file) } | ||
|
||
it { is_expected.to be_available } | ||
end | ||
|
||
context 'when the file in the file set is missing from the system' do | ||
let(:file_set) { create(:file_set, :with_missing_file) } | ||
|
||
it { is_expected.not_to be_available } | ||
end | ||
|
||
context 'when the file does not have a file' do | ||
let(:file_set) { create(:file_set) } | ||
|
||
it { is_expected.not_to be_available } | ||
end | ||
end | ||
|
||
describe '#path' do | ||
subject { described_class.new(id: 'id') } | ||
|
||
it { is_expected.to delegate_method(:path).to(:file) } | ||
end | ||
|
||
describe '#file' do | ||
subject { described_class.new(id: file_set.id) } | ||
|
||
let(:file_set) { create(:file_set, :with_member_file) } | ||
|
||
its(:file) { is_expected.to be_kind_of(Work::File) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Repository::DownloadsController, type: :controller do | ||
describe 'GET #download' do | ||
context 'when not requesting a specific use type with a preservation file present' do | ||
let(:file_set) { create(:file_set, :with_preservation_file) } | ||
|
||
specify do | ||
get :download, params: { id: file_set.id } | ||
expect(response).to be_success | ||
expect(response.body).to eq('Hello World!') | ||
end | ||
end | ||
|
||
context 'when not requesting a specific use type with a redacted preservation file present' do | ||
let(:file_set) { create(:file_set, :with_redacted_preservation_file) } | ||
|
||
specify do | ||
get :download, params: { id: file_set.id } | ||
expect(response).to be_success | ||
expect(response.body).to eq('Hello World!') | ||
end | ||
end | ||
|
||
context 'when not requesting a specific use type with a service file present' do | ||
let(:file_set) { create(:file_set, :with_service_file) } | ||
|
||
specify do | ||
get :download, params: { id: file_set.id } | ||
expect(response).to be_success | ||
expect(response.body).to eq('Hello World!') | ||
end | ||
end | ||
|
||
context 'when requesting the preservation file when a redacted one is present' do | ||
let(:file_set) { create(:file_set, :with_redacted_preservation_file) } | ||
|
||
specify do | ||
get :download, params: { id: file_set.id, use_type: 'PreservationMasterFile' } | ||
expect(response).to be_success | ||
expect(response.body).to eq('Hello World!') | ||
end | ||
end | ||
|
||
context 'when requesting the preservation file' do | ||
let(:file_set) { create(:file_set, :with_preservation_file) } | ||
|
||
specify do | ||
get :download, params: { id: file_set.id, use_type: 'PreservationMasterFile' } | ||
expect(response).to be_success | ||
expect(response.body).to eq('Hello World!') | ||
end | ||
end | ||
|
||
context 'when requesting the access file' do | ||
let(:file_set) { create(:file_set, :with_access_file) } | ||
|
||
specify do | ||
get :download, params: { id: file_set.id, use_type: 'AccessFile' } | ||
expect(response).to be_success | ||
expect(response.body).to eq('Hello World!') | ||
end | ||
end | ||
|
||
context 'when requesting the service file' do | ||
let(:file_set) { create(:file_set, :with_service_file) } | ||
|
||
specify do | ||
get :download, params: { id: file_set.id, use_type: 'ServiceFile' } | ||
expect(response).to be_success | ||
expect(response.body).to eq('Hello World!') | ||
end | ||
end | ||
|
||
context 'with an unsupported use type' do | ||
let(:file_set) { create(:file_set, :with_service_file) } | ||
|
||
specify do | ||
get :download, params: { id: file_set.id, use_type: :bogus } | ||
expect(response).to be_success | ||
expect(response.body).to eq('Hello World!') | ||
end | ||
end | ||
|
||
context 'with no files in the file set' do | ||
let(:file_set) { create(:file_set) } | ||
|
||
specify do | ||
get :download, params: { id: file_set.id } | ||
expect(response).to be_not_found | ||
end | ||
end | ||
|
||
context 'when the file set does not exist' do | ||
specify do | ||
get :download, params: { id: 'missing-id' } | ||
expect(response).to be_not_found | ||
end | ||
end | ||
|
||
context 'when the file does not exist' do | ||
let(:file_set) { create(:file_set, :with_missing_file) } | ||
|
||
specify do | ||
get :download, params: { id: file_set.id } | ||
expect(response).to be_not_found | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Repository::DownloadsController, type: :routing do | ||
describe 'routing' do | ||
it 'has a route to #download' do | ||
expect(get: '/downloads/1').to route_to('repository/downloads#download', id: '1') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.