ddr-models 2.6.2 → 2.7.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/.travis.yml +1 -1
- data/Gemfile +2 -3
- data/app/models/collection.rb +11 -3
- data/app/models/component.rb +54 -4
- data/app/models/item.rb +1 -2
- data/config/initializers/active_fedora_base.rb +8 -5
- data/config/initializers/rubydora_monkey_patch.rb +11 -0
- data/config/initializers/subscriptions.rb +16 -9
- data/config/locales/ddr-models.en.yml +3 -0
- data/ddr-models.gemspec +3 -2
- data/lib/ddr/actions/virus_check.rb +10 -16
- data/lib/ddr/auth.rb +0 -8
- data/lib/ddr/datastreams.rb +13 -2
- data/lib/ddr/datastreams/administrative_metadata_datastream.rb +9 -0
- data/lib/ddr/datastreams/content_datastream.rb +5 -0
- data/lib/ddr/datastreams/datastream_behavior.rb +60 -15
- data/lib/ddr/datastreams/delete_external_files.rb +29 -0
- data/lib/ddr/datastreams/descriptive_metadata_datastream.rb +33 -33
- data/lib/ddr/datastreams/external_file_datastream.rb +69 -0
- data/lib/ddr/datastreams/fits_datastream.rb +15 -5
- data/lib/ddr/datastreams/intermediate_file_datastream.rb +5 -0
- data/lib/ddr/datastreams/metadata_datastream.rb +15 -17
- data/lib/ddr/datastreams/multires_image_datastream.rb +5 -0
- data/lib/ddr/derivatives.rb +1 -0
- data/lib/ddr/derivatives/ptif_generator.rb +5 -1
- data/lib/ddr/derivatives/update_derivatives.rb +23 -0
- data/lib/ddr/events/deletion_event.rb +5 -9
- data/lib/ddr/events/event.rb +7 -9
- data/lib/ddr/events/ingestion_event.rb +5 -9
- data/lib/ddr/events/update_event.rb +12 -5
- data/lib/ddr/index.rb +0 -1
- data/lib/ddr/index/csv_query_result.rb +10 -2
- data/lib/ddr/index/fields.rb +10 -9
- data/lib/ddr/index/filter.rb +1 -0
- data/lib/ddr/managers/derivatives_manager.rb +84 -98
- data/lib/ddr/managers/technical_metadata_manager.rb +20 -5
- data/lib/ddr/models.rb +24 -24
- data/lib/ddr/models/base.rb +98 -74
- data/lib/ddr/models/cache.rb +20 -0
- data/lib/ddr/models/engine.rb +4 -6
- data/lib/ddr/models/file_management.rb +57 -152
- data/lib/ddr/models/fixity_checkable.rb +0 -5
- data/lib/ddr/models/has_admin_metadata.rb +4 -2
- data/lib/ddr/models/has_children.rb +35 -1
- data/lib/ddr/models/has_content.rb +3 -33
- data/lib/ddr/models/has_intermediate_file.rb +18 -0
- data/lib/ddr/models/has_multires_image.rb +12 -15
- data/lib/ddr/models/has_struct_metadata.rb +2 -38
- data/lib/ddr/models/indexing.rb +160 -163
- data/lib/ddr/models/permanent_id.rb +26 -49
- data/lib/ddr/models/solr_document.rb +21 -19
- data/lib/ddr/models/structure.rb +168 -41
- data/lib/ddr/models/structures/agent.rb +49 -0
- data/lib/ddr/models/structures/div.rb +64 -0
- data/lib/ddr/models/structures/f_locat.rb +54 -0
- data/lib/ddr/models/structures/file.rb +52 -0
- data/lib/ddr/models/structures/file_grp.rb +35 -0
- data/lib/ddr/models/structures/file_sec.rb +22 -0
- data/lib/ddr/models/structures/fptr.rb +31 -0
- data/lib/ddr/models/structures/mets_hdr.rb +37 -0
- data/lib/ddr/models/structures/mptr.rb +49 -0
- data/lib/ddr/models/structures/struct_map.rb +40 -0
- data/lib/ddr/models/version.rb +1 -1
- data/lib/ddr/notifications.rb +10 -12
- data/lib/ddr/utils.rb +29 -16
- data/lib/ddr/vocab.rb +15 -17
- data/lib/ddr/vocab/asset.rb +29 -19
- data/lib/ddr/vocab/contact.rb +5 -7
- data/lib/ddr/vocab/display.rb +6 -8
- data/lib/ddr/vocab/duke_terms.rb +8 -10
- data/lib/ddr/vocab/rdf_vocabulary_parser.rb +37 -39
- data/lib/ddr/vocab/roles.rb +17 -19
- data/lib/ddr/vocab/vocabulary.rb +26 -26
- data/spec/datastreams/external_file_datastream_spec.rb +37 -0
- data/spec/derivatives/png_generator_spec.rb +21 -25
- data/spec/derivatives/ptif_generator_spec.rb +22 -26
- data/spec/dummy/config/environments/test.rb +1 -1
- data/spec/dummy/db/schema.rb +23 -23
- data/spec/factories/structure_factories.rb +8 -2
- data/spec/fixtures/imageA.jpg +0 -0
- data/spec/fixtures/imageB.jpg +0 -0
- data/spec/index/csv_query_result_spec.rb +3 -3
- data/spec/index/fields_spec.rb +7 -6
- data/spec/managers/derivatives_manager_spec.rb +105 -112
- data/spec/managers/technical_metadata_manager_spec.rb +22 -18
- data/spec/models/active_fedora_base_spec.rb +0 -106
- data/spec/models/active_fedora_datastream_spec.rb +33 -2
- data/spec/models/attachment_spec.rb +0 -2
- data/spec/models/cache_spec.rb +32 -0
- data/spec/models/collection_spec.rb +43 -19
- data/spec/models/component_spec.rb +41 -51
- data/spec/models/descriptive_metadata_datastream_spec.rb +99 -87
- data/spec/models/events_spec.rb +50 -69
- data/spec/models/file_management_spec.rb +79 -187
- data/spec/models/has_children_spec.rb +54 -3
- data/spec/models/has_struct_metadata_spec.rb +32 -38
- data/spec/models/indexing_spec.rb +11 -6
- data/spec/models/item_spec.rb +21 -7
- data/spec/models/permanent_id_spec.rb +1 -5
- data/spec/models/solr_document_spec.rb +13 -41
- data/spec/models/structure_spec.rb +85 -16
- data/spec/models/structures/agent_spec.rb +30 -0
- data/spec/models/structures/div_spec.rb +26 -0
- data/spec/models/structures/f_locat_spec.rb +78 -0
- data/spec/models/structures/file_grp_spec.rb +23 -0
- data/spec/models/structures/file_sec_spec.rb +22 -0
- data/spec/models/structures/file_spec.rb +60 -0
- data/spec/models/structures/fptr_spec.rb +23 -0
- data/spec/models/structures/mets_hdr_spec.rb +26 -0
- data/spec/models/structures/mptr_spec.rb +25 -0
- data/spec/models/structures/struct_map_spec.rb +24 -0
- data/spec/models/target_spec.rb +0 -2
- data/spec/spec_helper.rb +4 -9
- data/spec/support/shared_examples_for_ddr_models.rb +96 -156
- data/spec/support/shared_examples_for_has_content.rb +56 -30
- data/spec/support/shared_examples_for_has_intermediate_spec.rb +28 -0
- data/spec/support/shared_examples_for_non_collection_models.rb +5 -0
- data/spec/support/structural_metadata_helper.rb +230 -59
- data/spec/utils_spec.rb +0 -2
- metadata +86 -44
- data/app/helpers/models_helper.rb +0 -10
- data/lib/ddr/auth/legacy/abstract_legacy_permissions.rb +0 -17
- data/lib/ddr/auth/legacy/legacy_authorization.rb +0 -44
- data/lib/ddr/auth/legacy/legacy_default_permissions.rb +0 -33
- data/lib/ddr/auth/legacy/legacy_permissions.rb +0 -33
- data/lib/ddr/auth/legacy/legacy_roles.rb +0 -25
- data/lib/ddr/index/legacy_license_fields.rb +0 -12
- data/lib/ddr/jobs.rb +0 -12
- data/lib/ddr/jobs/fits_file_characterization.rb +0 -13
- data/lib/ddr/jobs/fixity_check.rb +0 -13
- data/lib/ddr/jobs/job.rb +0 -36
- data/lib/ddr/jobs/queue.rb +0 -27
- data/lib/ddr/jobs/update_index.rb +0 -13
- data/lib/ddr/models/access_controllable.rb +0 -24
- data/lib/ddr/models/struct_div.rb +0 -63
- data/spec/auth/legacy_default_permissions_spec.rb +0 -37
- data/spec/auth/legacy_permissions_spec.rb +0 -37
- data/spec/helpers/models_helper_spec.rb +0 -11
- data/spec/jobs/fits_file_characterization_spec.rb +0 -16
- data/spec/jobs/fixity_check_spec.rb +0 -22
- data/spec/jobs/job_spec.rb +0 -40
- data/spec/jobs/update_index_spec.rb +0 -22
- data/spec/models/struct_div_spec.rb +0 -70
- data/spec/support/shared_examples_for_access_controllables.rb +0 -6
data/lib/ddr/models/base.rb
CHANGED
@@ -2,9 +2,14 @@ module Ddr
|
|
2
2
|
module Models
|
3
3
|
class Base < ActiveFedora::Base
|
4
4
|
|
5
|
+
# Lifecycle events
|
6
|
+
INGEST = "ingest.repo_object"
|
7
|
+
UPDATE = "update.repo_object"
|
8
|
+
DELETE = "delete.repo_object"
|
9
|
+
DEACCESSION = "deaccession.repo_object"
|
10
|
+
|
5
11
|
include Describable
|
6
12
|
include Governable
|
7
|
-
include AccessControllable
|
8
13
|
include HasThumbnail
|
9
14
|
include EventLoggable
|
10
15
|
include FixityCheckable
|
@@ -12,21 +17,24 @@ module Ddr
|
|
12
17
|
include Indexing
|
13
18
|
include Hydra::Validations
|
14
19
|
include HasAdminMetadata
|
20
|
+
extend Deprecation
|
15
21
|
|
16
22
|
# Prevent accidental use of #delete which lacks callbacks
|
17
23
|
private :delete
|
18
24
|
|
19
|
-
define_model_callbacks :deaccession
|
25
|
+
define_model_callbacks :deaccession
|
20
26
|
|
21
|
-
|
22
|
-
|
23
|
-
|
27
|
+
before_create :set_ingestion_date, unless: :ingestion_date
|
28
|
+
before_create :set_ingested_by, if: :performed_by, unless: :ingested_by
|
29
|
+
before_create :grant_default_roles
|
30
|
+
|
31
|
+
after_create :notify_ingest
|
32
|
+
after_create :assign_permanent_id!, if: :assign_permanent_id?
|
33
|
+
|
34
|
+
around_save :notify_update, unless: :new_record?
|
24
35
|
|
25
|
-
around_save :notify_save
|
26
|
-
around_save :notify_workflow_change, if: [:workflow_state_changed?, :persisted?]
|
27
|
-
after_create :assign_permanent_id, if: :assign_permanent_id?
|
28
36
|
around_deaccession :notify_deaccession
|
29
|
-
around_destroy :
|
37
|
+
around_destroy :notify_delete
|
30
38
|
|
31
39
|
def deaccession
|
32
40
|
run_callbacks :deaccession do
|
@@ -66,88 +74,104 @@ module Ddr
|
|
66
74
|
false
|
67
75
|
end
|
68
76
|
|
69
|
-
def legacy_authorization
|
70
|
-
Ddr::Auth::LegacyAuthorization.new(self)
|
71
|
-
end
|
72
|
-
|
73
|
-
# Moves the first (descriptive metadata) identifier into
|
74
|
-
# (administrative metadata) local_id according to the following
|
75
|
-
# rubric:
|
76
|
-
#
|
77
|
-
# No existing local_id:
|
78
|
-
# - Set local_id to first identifier value
|
79
|
-
# - Remove first identifier value
|
80
|
-
#
|
81
|
-
# Existing local_id:
|
82
|
-
# Same as first identifier value
|
83
|
-
# - Remove first identifier value
|
84
|
-
# Not same as first identifier value
|
85
|
-
# :replace option is true
|
86
|
-
# - Set local_id to first identifier value
|
87
|
-
# - Remove first identifier value
|
88
|
-
# :replace option is false
|
89
|
-
# - Do nothing
|
90
|
-
#
|
91
|
-
# Returns true or false depending on whether the object was
|
92
|
-
# changed by this method
|
93
|
-
def move_first_identifier_to_local_id(replace: true)
|
94
|
-
moved = false
|
95
|
-
identifiers = identifier.to_a
|
96
|
-
first_id = identifiers.shift
|
97
|
-
if first_id
|
98
|
-
if local_id.blank?
|
99
|
-
self.local_id = first_id
|
100
|
-
self.identifier = identifiers
|
101
|
-
moved = true
|
102
|
-
else
|
103
|
-
if local_id == first_id
|
104
|
-
self.identifier = identifiers
|
105
|
-
moved = true
|
106
|
-
else
|
107
|
-
if replace
|
108
|
-
self.local_id = first_id
|
109
|
-
self.identifier = identifiers
|
110
|
-
moved = true
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
moved
|
116
|
-
end
|
117
|
-
|
118
77
|
def publishable?
|
119
78
|
raise NotImplementedError, "Must be implemented by subclasses"
|
120
79
|
end
|
121
80
|
|
81
|
+
def save(options={})
|
82
|
+
cache.with(options) { super }
|
83
|
+
end
|
84
|
+
|
85
|
+
def datastreams_changed
|
86
|
+
datastreams.select { |dsid, ds| ds.changed? }
|
87
|
+
end
|
88
|
+
|
89
|
+
def datastreams_having_content
|
90
|
+
datastreams.select { |dsid, ds| ds.has_content? }
|
91
|
+
end
|
92
|
+
alias_method :attached_files_having_content, :datastreams_having_content
|
93
|
+
alias_method :datastreams_to_validate, :datastreams_having_content
|
94
|
+
deprecation_deprecate :datastreams_to_validate
|
95
|
+
|
96
|
+
def datastream_history
|
97
|
+
datastreams_having_content.each_with_object({}) do |(dsid, ds), memo|
|
98
|
+
memo[dsid] = ds.version_history
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
122
102
|
private
|
123
103
|
|
124
|
-
def
|
125
|
-
|
126
|
-
|
127
|
-
changes: changes,
|
128
|
-
created: new_record?) do |payload|
|
129
|
-
yield
|
104
|
+
def grant_default_roles
|
105
|
+
if default_roles.present?
|
106
|
+
roles.grant *default_roles
|
130
107
|
end
|
131
108
|
end
|
132
109
|
|
133
|
-
def
|
134
|
-
|
110
|
+
def default_roles
|
111
|
+
[]
|
112
|
+
end
|
113
|
+
|
114
|
+
def cache
|
115
|
+
@__cache ||= Cache.new
|
116
|
+
end
|
117
|
+
|
118
|
+
def performed_by
|
119
|
+
if user = cache.get(:user)
|
120
|
+
user.to_s
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def set_ingested_by
|
125
|
+
self.ingested_by = performed_by
|
126
|
+
end
|
127
|
+
|
128
|
+
def set_ingestion_date
|
129
|
+
self.ingestion_date = Time.now.utc.iso8601
|
130
|
+
end
|
131
|
+
|
132
|
+
def default_notification_payload
|
133
|
+
cache.slice(:summary, :comment, :detail)
|
134
|
+
.merge(pid: pid,
|
135
|
+
user_key: performed_by,
|
136
|
+
permanent_id: permanent_id,
|
137
|
+
model: self.class.to_s)
|
138
|
+
end
|
139
|
+
|
140
|
+
def notify_ingest
|
141
|
+
payload = default_notification_payload.merge(
|
142
|
+
event_date_time: ingestion_date,
|
143
|
+
datastreams_changed: attached_files_having_content.keys
|
144
|
+
)
|
145
|
+
ActiveSupport::Notifications.instrument(INGEST, payload)
|
146
|
+
end
|
147
|
+
|
148
|
+
def notify_update
|
149
|
+
event_params = default_notification_payload.merge(
|
150
|
+
attributes_changed: changes,
|
151
|
+
datastreams_changed: datastreams_changed.keys
|
152
|
+
)
|
153
|
+
ActiveSupport::Notifications.instrument(UPDATE, event_params) do |payload|
|
135
154
|
yield
|
155
|
+
payload[:event_date_time] = modified_date
|
136
156
|
end
|
137
157
|
end
|
138
158
|
|
159
|
+
def delete_notification_payload
|
160
|
+
default_notification_payload.merge(
|
161
|
+
datastream_history: datastream_history,
|
162
|
+
create_date: create_date,
|
163
|
+
modified_date: modified_date
|
164
|
+
)
|
165
|
+
end
|
166
|
+
|
139
167
|
def notify_deaccession
|
140
|
-
ActiveSupport::Notifications.instrument(
|
141
|
-
pid: pid,
|
142
|
-
permanent_id: permanent_id) do |payload|
|
168
|
+
ActiveSupport::Notifications.instrument(DEACCESSION, delete_notification_payload) do |payload|
|
143
169
|
yield
|
144
170
|
end
|
145
171
|
end
|
146
172
|
|
147
|
-
def
|
148
|
-
ActiveSupport::Notifications.instrument(
|
149
|
-
pid: pid,
|
150
|
-
permanent_id: permanent_id) do |payload|
|
173
|
+
def notify_delete
|
174
|
+
ActiveSupport::Notifications.instrument(DELETE, delete_notification_payload) do |payload|
|
151
175
|
yield
|
152
176
|
end
|
153
177
|
end
|
@@ -156,7 +180,7 @@ module Ddr
|
|
156
180
|
permanent_id.nil? && Ddr::Models.auto_assign_permanent_id
|
157
181
|
end
|
158
182
|
|
159
|
-
def assign_permanent_id
|
183
|
+
def assign_permanent_id!
|
160
184
|
PermanentId.assign!(self)
|
161
185
|
end
|
162
186
|
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Ddr::Models
|
2
|
+
class Cache < Hash
|
3
|
+
|
4
|
+
def get(key)
|
5
|
+
self[key]
|
6
|
+
end
|
7
|
+
|
8
|
+
def put(key, value)
|
9
|
+
self[key] = value
|
10
|
+
end
|
11
|
+
|
12
|
+
def with(options, &block)
|
13
|
+
merge!(options)
|
14
|
+
block_result = yield
|
15
|
+
reject! { |k, v| options.include?(k) }
|
16
|
+
block_result
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
data/lib/ddr/models/engine.rb
CHANGED
@@ -48,12 +48,6 @@ module Ddr
|
|
48
48
|
[ :thumbnail ]
|
49
49
|
end
|
50
50
|
|
51
|
-
initializer "ddr_models.external_files" do
|
52
|
-
Ddr::Models.external_file_store = ENV["EXTERNAL_FILE_STORE"]
|
53
|
-
Ddr::Models.multires_image_external_file_store = ENV["MULTIRES_IMAGE_EXTERNAL_FILE_STORE"]
|
54
|
-
Ddr::Models.external_file_subpath_pattern = ENV["EXTERNAL_FILE_SUBPATH_PATTERN"] || "--"
|
55
|
-
end
|
56
|
-
|
57
51
|
initializer "ddr_models.image_server" do
|
58
52
|
Ddr::Models.image_server_url = ENV["IMAGE_SERVER_URL"]
|
59
53
|
end
|
@@ -88,6 +82,10 @@ module Ddr
|
|
88
82
|
Ddr::Models.fits_home = ENV["FITS_HOME"]
|
89
83
|
end
|
90
84
|
|
85
|
+
initializer "vips_path" do
|
86
|
+
Ddr::Models.vips_path = ENV["VIPS_PATH"]
|
87
|
+
end
|
88
|
+
|
91
89
|
initializer "ddr_antivirus" do
|
92
90
|
require "ddr-antivirus"
|
93
91
|
if Rails.env.test?
|
@@ -2,115 +2,57 @@ module Ddr::Models
|
|
2
2
|
module FileManagement
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
-
EXTERNAL_FILE_PERMISSIONS = 0644
|
6
|
-
|
7
5
|
included do
|
8
|
-
attr_accessor :file_to_add
|
9
|
-
|
10
6
|
define_model_callbacks :add_file
|
11
7
|
before_add_file :virus_scan
|
12
|
-
|
13
|
-
after_save :
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
# but use in place (set dsLocation to file URI for current path.
|
39
|
-
def add_file file, dsid, opts={}
|
40
|
-
opts[:mime_type] ||= Ddr::Utils.mime_type_for(file)
|
41
|
-
|
42
|
-
# @file_to_add is set for callbacks to access the data
|
43
|
-
original_name = opts.fetch(:original_name, Ddr::Utils.file_name_for(file))
|
44
|
-
self.file_to_add = FileToAdd.new(file, dsid, original_name)
|
45
|
-
|
46
|
-
run_callbacks(:add_file) do
|
47
|
-
if opts.delete(:external) || datastreams.include?(dsid) && datastreams[dsid].external?
|
48
|
-
add_external_file(file, dsid, opts)
|
49
|
-
else
|
50
|
-
file = File.new(file, "rb") if Ddr::Utils.file_path?(file)
|
51
|
-
# ActiveFedora method accepts file-like objects, not paths
|
52
|
-
add_file_datastream(file, dsid: dsid, mimeType: opts[:mime_type])
|
8
|
+
after_add_file :set_original_filename
|
9
|
+
after_save :create_virus_check_event
|
10
|
+
end
|
11
|
+
|
12
|
+
FileToAdd = Struct.new(:dsid, :source_path, :original_filename)
|
13
|
+
|
14
|
+
# @param file [String, File, ActionDispatch::Http::UploadedFile]
|
15
|
+
# The file, or path to file, to add.
|
16
|
+
# @param dsid [String] The datastream ID to which file should be added.
|
17
|
+
# @param mime_type [String] Explicit mime type to set
|
18
|
+
# (otherwise discerned from file path or name).
|
19
|
+
# @param external [Boolean] Add file to external datastream.
|
20
|
+
# Not required for external datastream classes
|
21
|
+
# or datastream instances having controlGroup 'E'.
|
22
|
+
def add_file(file, dsid, mime_type: nil, external: false)
|
23
|
+
mime_type ||= Ddr::Utils.mime_type_for(file)
|
24
|
+
source_path = Ddr::Utils.file_path(file)
|
25
|
+
original_filename = Ddr::Utils.file_name(file)
|
26
|
+
file_to_add = FileToAdd.new(dsid, source_path, original_filename)
|
27
|
+
cache.with(file_to_add: file_to_add) do
|
28
|
+
run_callbacks(:add_file) do
|
29
|
+
if external || ( datastreams.include?(dsid) && datastreams[dsid].external? )
|
30
|
+
add_external_file(file, dsid, mime_type: mime_type)
|
31
|
+
else
|
32
|
+
add_file_datastream(file, dsid: dsid, mimeType: mime_type)
|
33
|
+
end
|
53
34
|
end
|
54
35
|
end
|
36
|
+
end
|
55
37
|
|
56
|
-
|
57
|
-
|
38
|
+
def add_file_datastream(file, opts={})
|
39
|
+
if Ddr::Utils.file_path?(file)
|
40
|
+
file = File.new(file, "rb")
|
41
|
+
end
|
42
|
+
super
|
58
43
|
end
|
59
44
|
|
60
|
-
#
|
61
|
-
#
|
62
|
-
|
45
|
+
# @api private
|
46
|
+
# Normally this method should not be called directly.
|
47
|
+
# Call `add_file` with dsid for external datastream, or with
|
48
|
+
# `:external=>true` option if no spec for dsid.
|
49
|
+
def add_external_file(file, dsid, mime_type: nil)
|
63
50
|
file_path = Ddr::Utils.file_path(file) # raises ArgumentError
|
64
|
-
|
65
|
-
# Retrieve or create the datastream
|
66
|
-
ds = datastreams.include?(dsid) ? datastreams[dsid] : add_external_datastream(dsid)
|
67
|
-
|
51
|
+
ds = datastreams[dsid] || add_external_datastream(dsid)
|
68
52
|
unless ds.external?
|
69
|
-
raise ArgumentError, "Cannot add external file to datastream
|
53
|
+
raise ArgumentError, "Cannot add external file to non-external datastream."
|
70
54
|
end
|
71
|
-
|
72
|
-
if ds.dsLocation_changed?
|
73
|
-
raise Ddr::Models::Error, "Cannot add external file to datastream when dsLocation change is pending."
|
74
|
-
end
|
75
|
-
|
76
|
-
# Set the MIME type
|
77
|
-
# The :mime_type option will be present when called from `add_file`.
|
78
|
-
# The fallback is there in case `add_external_file` is called directly.
|
79
|
-
ds.mimeType = opts[:mime_type] || Ddr::Utils.mime_type_for(file, file_path)
|
80
|
-
|
81
|
-
# Copy the file to storage unless we're using the original
|
82
|
-
if opts[:use_original]
|
83
|
-
raise Ddr::Models::Error, "Cannot add file to repository that is owned by another user." unless File.owned?(file_path)
|
84
|
-
store_path = file_path
|
85
|
-
else
|
86
|
-
# generate new storage path for file
|
87
|
-
store_path = create_external_file_path!(ds)
|
88
|
-
# copy the original file to the storage location
|
89
|
-
FileUtils.cp file_path, store_path
|
90
|
-
end
|
91
|
-
|
92
|
-
# set appropriate permissions on the file
|
93
|
-
set_external_file_permissions!(store_path)
|
94
|
-
|
95
|
-
# set dsLocation to file URI for storage path
|
96
|
-
ds.dsLocation = Ddr::Utils.path_to_uri(store_path)
|
97
|
-
end
|
98
|
-
|
99
|
-
# Create directory (if necessary) for newly generated file path and return path
|
100
|
-
def create_external_file_path! ds
|
101
|
-
file_path = generate_external_file_path(ds)
|
102
|
-
FileUtils.mkdir_p(File.dirname(file_path))
|
103
|
-
file_path
|
104
|
-
end
|
105
|
-
|
106
|
-
#
|
107
|
-
# Generates a new external file storage location
|
108
|
-
#
|
109
|
-
# => {external_file_store}/1/e/69/1e691815-0631-4f9b-8e23-2dfb2eec9c70
|
110
|
-
#
|
111
|
-
def generate_external_file_path ds
|
112
|
-
file_name = generate_external_file_name(ds)
|
113
|
-
File.join(external_file_store(ds.dsid), generate_external_directory_subpath, file_name)
|
55
|
+
ds.add_file(file_path, mime_type: mime_type)
|
114
56
|
end
|
115
57
|
|
116
58
|
def external_datastreams
|
@@ -122,74 +64,37 @@ module Ddr::Models
|
|
122
64
|
end
|
123
65
|
|
124
66
|
def add_external_datastream dsid, opts={}
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
datastream
|
67
|
+
create_datastream(Ddr::Datastreams::ExternalFileDatastream, dsid).tap do |ds|
|
68
|
+
add_datastream(ds)
|
69
|
+
self.class.build_datastream_accessor(dsid)
|
70
|
+
end
|
130
71
|
end
|
131
72
|
|
132
73
|
protected
|
133
74
|
|
134
|
-
FileToAdd = Struct.new(:file, :dsid, :original_name)
|
135
|
-
|
136
|
-
def virus_scan_results
|
137
|
-
@virus_scan_results ||= []
|
138
|
-
end
|
139
|
-
|
140
75
|
def virus_scan
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
end
|
146
|
-
|
147
|
-
def notify_virus_scan_results
|
148
|
-
while result = virus_scan_results.shift
|
149
|
-
result.merge! pid: pid
|
150
|
-
ActiveSupport::Notifications.instrument(Ddr::Notifications::VIRUS_CHECK, result)
|
76
|
+
file_to_add = cache.get(:file_to_add)
|
77
|
+
result = Ddr::Actions::VirusCheck.call(file_to_add.source_path)
|
78
|
+
if file_to_add.dsid == Ddr::Datastreams::CONTENT
|
79
|
+
cache.put(:virus_scan_result, result)
|
151
80
|
end
|
81
|
+
rescue ArgumentError => e # file is a blob (string)
|
82
|
+
logger.error(e)
|
152
83
|
end
|
153
84
|
|
154
|
-
def
|
155
|
-
|
156
|
-
|
157
|
-
Ddr::Models.multires_image_external_file_store
|
158
|
-
else
|
159
|
-
Ddr::Models.external_file_store
|
85
|
+
def create_virus_check_event
|
86
|
+
if result = cache.get(:virus_scan_result)
|
87
|
+
Ddr::Events::VirusCheckEvent.create(result.merge(pid: pid))
|
160
88
|
end
|
161
89
|
end
|
162
90
|
|
163
|
-
def
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
def generate_external_file_name ds
|
168
|
-
content_file_name = Ddr::Utils::sanitize_filename(original_filename) || datastreams[ds.dsid].default_file_name
|
169
|
-
case ds.dsid
|
170
|
-
when Ddr::Datastreams::MULTIRES_IMAGE
|
171
|
-
case ds.mimeType
|
172
|
-
when "image/tiff"
|
173
|
-
"#{File.basename(content_file_name, File.extname(content_file_name))}.ptif"
|
91
|
+
def set_original_filename
|
92
|
+
if file_to_add = cache.get(:file_to_add)
|
93
|
+
if file_to_add.dsid == Ddr::Datastreams::CONTENT
|
94
|
+
self.original_filename = file_to_add.original_filename
|
174
95
|
end
|
175
|
-
else
|
176
|
-
content_file_name
|
177
96
|
end
|
178
97
|
end
|
179
98
|
|
180
|
-
def generate_external_directory_subpath
|
181
|
-
subdir = SecureRandom.uuid
|
182
|
-
m = Ddr::Models.external_file_subpath_regexp.match(subdir)
|
183
|
-
raise "File name does not match external file subpath pattern: #{file_name}" unless m
|
184
|
-
subpath_segments = m.to_a[1..-1]
|
185
|
-
File.join *subpath_segments, subdir
|
186
|
-
end
|
187
|
-
|
188
|
-
def cleanup_external_files_on_destroy
|
189
|
-
paths = external_datastream_file_paths
|
190
|
-
yield
|
191
|
-
File.unlink *paths
|
192
|
-
end
|
193
|
-
|
194
99
|
end
|
195
100
|
end
|