-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Downloading and updating file content
- Loading branch information
Showing
8 changed files
with
189 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
class UploadDecorator < Draper::Decorator | ||
attr_reader :original_filename | ||
delegate_all | ||
def initialize(file, original_filename) | ||
super(file) | ||
@original_filename = original_filename | ||
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
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,48 @@ | ||
# frozen_string_literal: true | ||
require 'rails_helper' | ||
|
||
RSpec.describe Valhalla::DownloadsController do | ||
let(:meta) { Valkyrie.config.metadata_adapter } | ||
let(:disk) { Valkyrie.config.storage_adapter } | ||
let(:change_set_persister) { PlumChangeSetPersister.new(metadata_adapter: meta, storage_adapter: disk) } | ||
let(:sample_file) { fixture_file_upload('files/example.tif', 'image/tiff') } | ||
let(:resource) { FactoryGirl.create_for_repository(:scanned_resource, files: [sample_file]) } | ||
let(:file_set) { resource.member_ids.map { |id| meta.query_service.find_by(id: id) }.first } | ||
let(:file_node) { file_set.file_metadata.first } | ||
let(:user) { FactoryGirl.create(:admin) } | ||
|
||
routes { Valhalla::Engine.routes } | ||
|
||
describe 'GET /downloads/:obj/file/:id' do | ||
context "when logged in" do | ||
before do | ||
sign_in user if user | ||
end | ||
|
||
it "serves files that exist" do | ||
get :show, params: { resource_id: file_set.id.to_s, id: file_node.id.to_s } | ||
expect(response.body).to eq(sample_file.read) | ||
expect(response.content_length).to eq(196_882) | ||
expect(response.content_type).to eq('image/tiff') | ||
expect(response.headers['Content-Disposition']).to eq('inline; filename="example.tif"') | ||
end | ||
|
||
it "returns an appropriate error when the file doesn't exist" do | ||
get :show, params: { resource_id: file_set.id.to_s, id: 'bogus' } | ||
expect(response.status).to eq(404) | ||
end | ||
|
||
it "returns an appropriate error when the resource doesn't exist" do | ||
get :show, params: { resource_id: 'bogus', id: 'bogus' } | ||
expect(response.status).to eq(404) | ||
end | ||
end | ||
|
||
context "when not logged in" do | ||
it "redirects to login" do | ||
get :show, params: { resource_id: file_set.id.to_s, id: file_node.id.to_s } | ||
expect(response).to redirect_to('/users/auth/cas') | ||
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,70 @@ | ||
# frozen_string_literal: true | ||
class Valhalla::DownloadsController < ApplicationController | ||
include Hydra::Controller::DownloadBehavior | ||
|
||
def show | ||
if resource && load_file | ||
send_content | ||
else | ||
render_404 | ||
end | ||
end | ||
|
||
def resource | ||
@resource ||= query_service.find_by(id: params[:resource_id]) | ||
rescue Valkyrie::Persistence::ObjectNotFoundError | ||
nil | ||
end | ||
|
||
def load_file | ||
return unless binary_file && file_desc | ||
@load_file ||= FileWithMetadata.new(id: params[:id], file: binary_file, mime_type: file_desc.mime_type, original_name: file_desc.original_filename.first) | ||
end | ||
|
||
def file_desc | ||
return unless resource | ||
@file_desc ||= resource.file_metadata.find { |m| m.id.to_s == params[:id] } | ||
end | ||
|
||
def binary_file | ||
return unless file_desc | ||
@binary_file ||= storage_adapter.find_by(id: file_desc.file_identifiers.first) | ||
end | ||
|
||
class FileWithMetadata < Dry::Struct | ||
delegate :size, :read, :stream, to: :file | ||
attribute :id, Valkyrie::Types::Any | ||
attribute :file, Valkyrie::Types::Any | ||
attribute :mime_type, Valkyrie::Types::SingleValuedString | ||
attribute :original_name, Valkyrie::Types::SingleValuedString | ||
end | ||
|
||
# Customize the :download ability in your Ability class, or override this method | ||
def authorize_download! | ||
authorize! :download, resource | ||
end | ||
|
||
# Copied from hydra-head and adjusted to handle the fact that we don't have a | ||
# modified_date in Valkyrie yet. | ||
def prepare_file_headers | ||
send_file_headers! content_options | ||
response.headers['Content-Type'] = file_desc.mime_type.first.to_s | ||
response.headers['Content-Length'] ||= binary_file.size.to_s | ||
# Prevent Rack::ETag from calculating a digest over body | ||
response.headers['Last-Modified'] = modified_date | ||
end | ||
|
||
def modified_date | ||
return unless load_file.respond_to?(:updated_at) | ||
# Copied/pasted from Hydra-Head. | ||
file_desc.updated_at.utc.strftime("%a, %d %b %Y %T GMT") | ||
end | ||
|
||
def query_service | ||
Valkyrie.config.metadata_adapter.query_service | ||
end | ||
|
||
def storage_adapter | ||
Valkyrie.config.storage_adapter | ||
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# frozen_string_literal: true | ||
Valhalla::Engine.routes.draw do | ||
get '/downloads/:resource_id/file/:id', to: 'downloads#show', as: :download | ||
end |