From 07c1a37e718d2c2532875b611354801aa48b12e1 Mon Sep 17 00:00:00 2001 From: Carolyn Cole Date: Fri, 14 Dec 2018 15:10:16 -0500 Subject: [PATCH] Adding downlod directly from disk --- .rubocop_todo.yml | 58 +++++++------------ .../concerns/external_download_behavior.rb | 35 +++++++++-- app/controllers/downloads_controller.rb | 19 +++++- spec/controllers/downloads_controller_spec.rb | 11 +++- spec/factories/file_sets.rb | 2 +- 5 files changed, 77 insertions(+), 48 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 08dc807a8..1fa952f87 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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 @@ -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: @@ -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. @@ -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 @@ -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: @@ -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' @@ -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 @@ -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/**/* @@ -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: @@ -287,12 +278,11 @@ 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' @@ -300,39 +290,33 @@ Style/DateTime: - '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: diff --git a/app/controllers/concerns/external_download_behavior.rb b/app/controllers/concerns/external_download_behavior.rb index 46015d03b..448d555d9 100644 --- a/app/controllers/concerns/external_download_behavior.rb +++ b/app/controllers/concerns/external_download_behavior.rb @@ -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 @@ -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 diff --git a/app/controllers/downloads_controller.rb b/app/controllers/downloads_controller.rb index 76c0ea595..8323653b9 100644 --- a/app/controllers/downloads_controller.rb +++ b/app/controllers/downloads_controller.rb @@ -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 @@ -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 diff --git a/spec/controllers/downloads_controller_spec.rb b/spec/controllers/downloads_controller_spec.rb index d195d4e51..324fd497f 100644 --- a/spec/controllers/downloads_controller_spec.rb +++ b/spec/controllers/downloads_controller_spec.rb @@ -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 diff --git a/spec/factories/file_sets.rb b/spec/factories/file_sets.rb index dcdccf1fd..307256232 100644 --- a/spec/factories/file_sets.rb +++ b/spec/factories/file_sets.rb @@ -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