Skip to content
This repository has been archived by the owner on Nov 18, 2020. It is now read-only.

Commit

Permalink
Adding downlod directly from disk
Browse files Browse the repository at this point in the history
  • Loading branch information
carolyncole committed Jan 10, 2019
1 parent b29f972 commit 07c1a37
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 48 deletions.
58 changes: 21 additions & 37 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-10-17 20:41:09 -0400 using RuboCop version 0.52.1.
# on 2018-12-14 18:30:21 -0500 using RuboCop version 0.52.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 3
# Offense count: 4
# Cop supports --auto-correct.
# Configuration parameters: Include, TreatCommentsAsGroupSeparators.
# Include: **/Gemfile, **/gems.rb
Expand All @@ -19,11 +19,6 @@ Lint/DuplicateMethods:
Exclude:
- 'app/presenters/error_presenter.rb'

# Offense count: 1
Lint/RescueException:
Exclude:
- 'lib/tasks/scholarsphere.rake'

# Offense count: 2
# Cop supports --auto-correct.
Lint/UnifiedInteger:
Expand All @@ -36,24 +31,24 @@ Lint/UriEscapeUnescape:
- 'app/actors/sufia/create_with_remote_files_actor.rb'
- 'spec/features/authentication_spec.rb'

# Offense count: 236
# Offense count: 215
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 222

# Offense count: 1
# Offense count: 3
Metrics/CyclomaticComplexity:
Max: 9

# Offense count: 303
# Offense count: 291
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 770

# Offense count: 1
# Offense count: 3
Metrics/PerceivedComplexity:
Max: 9
Max: 10

# Offense count: 1
# Cop supports --auto-correct.
Expand All @@ -66,19 +61,18 @@ Performance/HashEachMethods:
RSpec/AnyInstance:
Enabled: false

# Offense count: 12
# Offense count: 11
RSpec/BeEql:
Exclude:
- 'spec/controllers/my/works_controller_spec.rb'
- 'spec/features/collection/view_and_search_spec.rb'

# Offense count: 142
# Offense count: 128
# Configuration parameters: Prefixes.
# Prefixes: when, with, without
RSpec/ContextWording:
Enabled: false

# Offense count: 129
# Offense count: 118
# Configuration parameters: Max.
RSpec/ExampleLength:
Enabled: false
Expand All @@ -101,24 +95,22 @@ RSpec/LetBeforeExamples:
Exclude:
- 'spec/features/dashboard/dashboard_works_spec.rb'

# Offense count: 166
# Offense count: 157
# Configuration parameters: EnforcedStyle.
# SupportedStyles: have_received, receive
RSpec/MessageSpies:
Enabled: false

# Offense count: 296
# Offense count: 282
# Configuration parameters: AggregateFailuresByDefault.
RSpec/MultipleExpectations:
Exclude:
- 'spec/features/**/*'
Max: 16
Max: 53

# Offense count: 87
RSpec/NamedSubject:
Enabled: false

# Offense count: 56
# Offense count: 52
# Configuration parameters: Max.
RSpec/NestedGroups:
Exclude:
Expand All @@ -128,7 +120,6 @@ RSpec/NestedGroups:
- 'spec/jobs/copy_permissions_job_spec.rb'
- 'spec/jobs/share_notify_job_spec.rb'
- 'spec/lib/devise/http_header_authenticatable_spec.rb'
- 'spec/models/migration/creator_list_spec.rb'
- 'spec/models/permissions_change_set_spec.rb'
- 'spec/models/user_spec.rb'
- 'spec/rake/scholarsphere/solr_spec.rb'
Expand Down Expand Up @@ -222,7 +213,7 @@ Rails/DynamicFindBy:
- 'spec/factories/works.rb'
- 'spec/views/curations_concerns/base/_form_relationships.html.erb_spec.rb'

# Offense count: 26
# Offense count: 32
Rails/FilePath:
Enabled: false

Expand All @@ -233,7 +224,7 @@ Rails/HasManyOrHasOneDependent:
Exclude:
- 'app/models/agent.rb'

# Offense count: 48
# Offense count: 47
# Cop supports --auto-correct.
# Configuration parameters: Include.
# Include: spec/**/*, test/**/*
Expand All @@ -257,7 +248,7 @@ Rails/InverseOf:
Exclude:
- 'app/models/alias.rb'

# Offense count: 11
# Offense count: 9
# Configuration parameters: Include.
# Include: app/controllers/**/*.rb
Rails/LexicallyScopedActionFilter:
Expand Down Expand Up @@ -287,52 +278,45 @@ Rails/SkipsModelValidations:
Exclude:
- 'spec/factories/users.rb'

# Offense count: 20
# Offense count: 19
Style/DateTime:
Exclude:
- 'app/controllers/admin/stats_controller.rb'
- 'config/initializers/sufia.rb'
- 'lib/tasks/scholarsphere/fixup.rake'
- 'spec/features/dashboard/dashboard_works_spec.rb'
- 'spec/models/user_mailer_spec.rb'
- 'spec/presenters/stats_presenter_spec.rb'
- 'spec/presenters/sufia/work_usage_spec.rb'
- 'spec/services/generic_work_to_share_json_service_spec.rb'
- 'spec/views/user_mailer/stats_email.html.erb_spec.rb'

# Offense count: 15
# Offense count: 11
Style/MixinUsage:
Exclude:
- 'lib/tasks/scholarsphere.rake'
- 'spec/controllers/agents_controller_spec.rb'
- 'spec/features/collection/create_spec.rb'
- 'spec/features/collection/delete_spec.rb'
- 'spec/features/collection/edit_spec.rb'
- 'spec/features/collection/view_and_search_spec.rb'
- 'spec/features/dashboard/dashboard_collections_spec.rb'
- 'spec/features/dashboard/dashboard_highlights_spec.rb'
- 'spec/features/dashboard/dashboard_shares_spec.rb'
- 'spec/features/dashboard/dashboard_spec.rb'
- 'spec/features/dashboard/dashboard_works_spec.rb'
- 'spec/features/generic_work/add_multiple_creators_spec.rb'
- 'spec/features/generic_work/edit_work_spec.rb'
- 'spec/features/generic_work/upload_and_delete_spec.rb'
- 'spec/features/generic_work/view_and_download_spec.rb'

# Offense count: 7
# Offense count: 4
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: implicit, explicit
Style/RescueStandardError:
Exclude:
- 'app/models/user.rb'
- 'lib/tasks/scholarsphere/analytic-load.rake'
- 'lib/tasks/scholarsphere/fixup.rake'
- 'lib/tasks/scholarsphere/solr.rake'
- 'spec/features/support/feature_cleanup.rb'
- 'tasks/dev.rake'

# Offense count: 1
# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: ConvertCodeThatCanStartToReturnNil.
Style/SafeNavigation:
Expand Down
35 changes: 29 additions & 6 deletions app/controllers/concerns/external_download_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@
# external content in Fedora.
module ExternalDownloadBehavior
def show
af_file = file.is_a?(ActiveFedora::File)
if af_file && file.new_record?
render_404
else
if af_file
case file
when ActiveFedora::File
# For original files that are stored in fedora

if file.new_record?
render_404
else
file.mime_type ||= 'text/plain'
if response
response.headers['Content-Length'] = file.size.to_i.to_s
end
super
end
super
when String
# For derivatives or files stored on the local file system
response.headers['Accept-Ranges'] = 'bytes'
response.headers['Content-Length'] = File.size(file).to_s
send_file file, derivative_download_options
else
render_404
end
end

Expand All @@ -29,4 +38,18 @@ def file_name
file.original_name
end
end

def file_path
return unless remote?
@file_path ||= Scholarsphere::Pairtree.new(asset, nil).storage_path(file_url)
end

def file_url
@file_url ||= ActiveFedora.fedora.connection.head(file.uri).response.headers['content-type'].split('"')[1]
end

def attribute_url
local_mime_type = metadata.attributes['http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#hasMimeType'].first
local_mime_type.split('url="')[1][0..-2]
end
end
19 changes: 17 additions & 2 deletions app/controllers/downloads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,19 @@ def authorize_download!
end

def load_file
return super if params['file'] == 'thumbnail' || asset.is_a?(FileSet)
zip_service.call
if params['file'] == 'thumbnail'
super
elsif asset.is_a?(FileSet)
pcdm_file = asset.association(:original_file).find_target
path = Scholarsphere::Pairtree.new(asset, nil).storage_path(disk_file_url(pcdm_file))
if File.exist?(path)
path
else
super
end
else
zip_service.call
end
end

def work_directory
Expand All @@ -43,4 +54,8 @@ def zip_service
end

class ZipServiceError < StandardError; end

def disk_file_url(file)
@file_url ||= ActiveFedora.fedora.connection.head(file.uri).response.headers['content-type'].split('"')[1]
end
end
11 changes: 9 additions & 2 deletions spec/controllers/downloads_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,22 @@
describe '#show' do
subject { controller.send(:show) }

let(:response) { instance_double ActionDispatch::Response, headers: {} }

before do
allow_any_instance_of(User).to receive(:groups).and_return([])
allow(controller).to receive(:response).and_return(response)
end

context 'with a FileSet' do
before { controller.params[:id] = my_file.id }
before do
allow(response).to receive(:"status=")
controller.params[:id] = my_file.id
end
it 'sends content' do
expect(controller).to receive(:send_content)
# expect(controller).to receive(:send_content)
expect(WorkZipService).not_to receive(:new)
expect(controller).to receive(:send_file).with(/.*#{my_file.id}.*world.png/, type: 'image/png', disposition: 'inline')
subject
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/file_sets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

after(:create) do |file, evaluator|
if evaluator.content
Hydra::Works::UploadFileToFileSet.call(file, evaluator.content)
IngestFileJob.perform_now(file, evaluator.content.path, evaluator.user)
end
end

Expand Down

0 comments on commit 07c1a37

Please sign in to comment.