• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / pdc_describe / e71dda69-4c6c-449e-b2c9-4282b2b3c755

pending completion
e71dda69-4c6c-449e-b2c9-4282b2b3c755

Pull #962

circleci

Carolyn Cole
Removing upload method change and adding stubbing of S3 method that is now being called directly
Pull Request #962: Fetch pre-curation files from directly from AWS

13 of 13 new or added lines in 3 files covered. (100.0%)

1832 of 1858 relevant lines covered (98.6%)

167.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.8
/app/models/work.rb
1
# frozen_string_literal: true
2

3
# rubocop:disable Metrics/ClassLength
4
class Work < ApplicationRecord
1✔
5
  MAX_UPLOADS = 20
1✔
6

7
  # Errors for cases where there is no valid Collection
8
  class InvalidCollectionError < ::ArgumentError; end
1✔
9

10
  has_many :work_activity, -> { order(updated_at: :desc) }, dependent: :destroy
164✔
11
  has_many :user_work, -> { order(updated_at: :desc) }, dependent: :destroy
8✔
12
  has_many_attached :pre_curation_uploads, service: :amazon_pre_curation
1✔
13

14
  belongs_to :collection
1✔
15
  belongs_to :curator, class_name: "User", foreign_key: "curator_user_id", optional: true
1✔
16

17
  attribute :work_type, :string, default: "DATASET"
1✔
18
  attribute :profile, :string, default: "DATACITE"
1✔
19

20
  attr_accessor :user_entered_doi
1✔
21

22
  alias state_history user_work
1✔
23

24
  include AASM
1✔
25

26
  aasm column: :state do
1✔
27
    state :none, inital: true
1✔
28
    state :draft, :awaiting_approval, :approved, :withdrawn, :tombstone
1✔
29

30
    event :draft, after: :draft_doi do
1✔
31
      transitions from: :none, to: :draft, guard: :valid_to_draft
1✔
32
    end
33

34
    event :complete_submission do
1✔
35
      transitions from: :draft, to: :awaiting_approval, guard: :valid_to_submit
1✔
36
    end
37

38
    event :request_changes do
1✔
39
      transitions from: :awaiting_approval, to: :awaiting_approval, guard: :valid_to_submit
1✔
40
    end
41

42
    event :approve do
1✔
43
      transitions from: :awaiting_approval, to: :approved, guard: :valid_to_approve, after: :publish
1✔
44
    end
45

46
    event :withdraw do
1✔
47
      transitions from: [:draft, :awaiting_approval, :approved], to: :withdrawn
1✔
48
    end
49

50
    event :resubmit do
1✔
51
      transitions from: :withdrawn, to: :draft
1✔
52
    end
53

54
    event :remove do
1✔
55
      transitions from: :withdrawn, to: :tombstone
1✔
56
    end
57

58
    after_all_events :track_state_change
1✔
59
  end
60

61
  def state=(new_state)
1✔
62
    new_state_sym = new_state.to_sym
278✔
63
    valid_states = self.class.aasm.states.map(&:name)
278✔
64
    raise(StandardError, "Invalid state '#{new_state}'") unless valid_states.include?(new_state_sym)
278✔
65
    aasm_write_state_without_persistence(new_state_sym)
277✔
66
  end
67

68
  ##
69
  # Is this work editable by a given user?
70
  # A work is editable when:
71
  # * it is being edited by the person who made it
72
  # * it is being edited by a collection admin of the collection where is resides
73
  # * it is being edited by a super admin
74
  # @param [User]
75
  # @return [Boolean]
76
  def editable_by?(user)
1✔
77
    submitted_by?(user) || administered_by?(user)
161✔
78
  end
79

80
  def editable_in_current_state?(user)
1✔
81
    # anyone with edit privleges can edit a work while it is in draft or awaiting approval
82
    return editable_by?(user) if draft? || awaiting_approval?
103✔
83

84
    # Only admisitrators can edit a work in other states
85
    administered_by?(user)
20✔
86
  end
87

88
  def submitted_by?(user)
1✔
89
    created_by_user_id == user.id
161✔
90
  end
91

92
  def administered_by?(user)
1✔
93
    user.has_role?(:collection_admin, collection)
58✔
94
  end
95

96
  class << self
1✔
97
    def find_by_doi(doi)
1✔
98
      prefix = "10.34770/"
3✔
99
      doi = "#{prefix}#{doi}" unless doi.start_with?(prefix)
3✔
100
      Work.find_by!("metadata @> ?", JSON.dump(doi: doi))
3✔
101
    end
102

103
    def find_by_ark(ark)
1✔
104
      prefix = "ark:/"
3✔
105
      ark = "#{prefix}#{ark}" unless ark.start_with?(prefix)
3✔
106
      Work.find_by!("metadata @> ?", JSON.dump(ark: ark))
3✔
107
    end
108

109
    delegate :resource_type_general_values, to: PDCMetadata::Resource
1✔
110

111
    # Determines whether or not a test DOI should be referenced
112
    # (this avoids requests to the DOI API endpoint for non-production deployments)
113
    # @return [Boolean]
114
    def publish_test_doi?
1✔
115
      (Rails.env.development? || Rails.env.test?) && Rails.configuration.datacite.user.blank?
41✔
116
    end
117
  end
118

119
  include Rails.application.routes.url_helpers
1✔
120

121
  before_save do |work|
1✔
122
    # Ensure that the metadata JSONB postgres field is persisted properly
123
    work.metadata = JSON.parse(work.resource.to_json)
717✔
124
    work.save_pre_curation_uploads
717✔
125
  end
126

127
  after_save do |work|
1✔
128
    if work.approved?
716✔
129
      work.attach_s3_resources if !work.pre_curation_uploads.empty? && work.pre_curation_uploads.length > work.post_curation_uploads.length
75✔
130
      work.reload
75✔
131
    end
132
  end
133

134
  validate do |work|
1✔
135
    if none?
743✔
136
      work.validate_doi
101✔
137
    elsif draft?
642✔
138
      work.valid_to_draft
417✔
139
    else
140
      work.valid_to_submit
225✔
141
    end
142
  end
143

144
  # Overload ActiveRecord.reload method
145
  # https://apidock.com/rails/ActiveRecord/Base/reload
146
  #
147
  # NOTE: Usually `after_save` is a better place to put this kind of code:
148
  #
149
  #   after_save do |work|
150
  #     work.resource = nil
151
  #   end
152
  #
153
  # but that does not work in this case because the block points to a different
154
  # memory object for `work` than the we want we want to reload.
155
  def reload(options = nil)
1✔
156
    super
167✔
157
    # Force `resource` to be reloaded
158
    @resource = nil
167✔
159
    self
167✔
160
  end
161

162
  def validate_doi
1✔
163
    return true unless user_entered_doi
101✔
164
    if /^10.\d{4,9}\/[-._;()\/:a-z0-9\-]+$/.match?(doi.downcase)
12✔
165
      response = Faraday.get("#{Rails.configuration.datacite.doi_url}#{doi}")
11✔
166
      errors.add(:base, "Invalid DOI: can not verify it's authenticity") unless response.success? || response.status == 302
11✔
167
    else
168
      errors.add(:base, "Invalid DOI: does not match format")
1✔
169
    end
170
    errors.count == 0
12✔
171
  end
172

173
  def valid_to_draft
1✔
174
    errors.add(:base, "Must provide a title") if resource.main_title.blank?
746✔
175
    validate_ark
746✔
176
    validate_creators
746✔
177
    validate_uploads
746✔
178
    errors.count == 0
746✔
179
  end
180

181
  def valid_to_submit
1✔
182
    valid_to_draft
297✔
183
    validate_metadata
297✔
184
    errors.count == 0
297✔
185
  end
186

187
  def valid_to_approve(user)
1✔
188
    valid_to_submit
28✔
189
    unless user.has_role? :collection_admin, collection
28✔
190
      errors.add :base, "Unauthorized to Approve"
4✔
191
    end
192
    errors.count == 0
28✔
193
  end
194

195
  def title
1✔
196
    resource.main_title
272✔
197
  end
198

199
  def uploads_attributes
1✔
200
    return [] if approved? # once approved we no longer allow the updating of uploads via the application
44✔
201
    uploads.map do |upload|
42✔
202
      {
203
        id: upload.id,
9✔
204
        key: upload.key,
205
        filename: upload.filename.to_s,
206
        created_at: upload.created_at,
207
        url: upload.url
208
      }
209
    end
210
  end
211

212
  def form_attributes
1✔
213
    {
214
      uploads: uploads_attributes
44✔
215
    }
216
  end
217

218
  def draft_doi
1✔
219
    return if resource.doi.present?
32✔
220
    resource.doi = if self.class.publish_test_doi?
20✔
221
                     Rails.logger.info "Using hard-coded test DOI during development."
1✔
222
                     "10.34770/tbd"
1✔
223
                   else
224
                     result = data_cite_connection.autogenerate_doi(prefix: Rails.configuration.datacite.prefix)
19✔
225
                     if result.success?
19✔
226
                       result.success.doi
18✔
227
                     else
228
                       raise("Error generating DOI. #{result.failure.status} / #{result.failure.reason_phrase}")
1✔
229
                     end
230
                   end
231
    save!
19✔
232
  end
233

234
  def created_by_user
1✔
235
    User.find(created_by_user_id)
311✔
236
  rescue ActiveRecord::RecordNotFound
237
    nil
1✔
238
  end
239

240
  def resource=(resource)
1✔
241
    @resource = resource
413✔
242
    # Ensure that the metadata JSONB postgres field is persisted properly
243
    self.metadata = JSON.parse(resource.to_json)
413✔
244
  end
245

246
  def resource
1✔
247
    @resource ||= PDCMetadata::Resource.new_from_jsonb(metadata)
12,840✔
248
  end
249

250
  def url
1✔
251
    return unless persisted?
3✔
252

253
    @url ||= url_for(self)
3✔
254
  end
255

256
  def files_location_upload?
1✔
257
    files_location.blank? || files_location == "file_upload"
5✔
258
  end
259

260
  def files_location_cluster?
1✔
261
    files_location == "file_cluster"
47✔
262
  end
263

264
  def files_location_other?
1✔
265
    files_location == "file_other"
47✔
266
  end
267

268
  def change_curator(curator_user_id, current_user)
1✔
269
    if curator_user_id == "no-one"
5✔
270
      clear_curator(current_user)
1✔
271
    else
272
      update_curator(curator_user_id, current_user)
4✔
273
    end
274
  end
275

276
  def clear_curator(current_user)
1✔
277
    # Update the curator on the Work
278
    self.curator_user_id = nil
2✔
279
    save!
2✔
280

281
    # ...and log the activity
282
    WorkActivity.add_work_activity(id, "Unassigned existing curator", current_user.id, activity_type: WorkActivity::SYSTEM)
2✔
283
  end
284

285
  def update_curator(curator_user_id, current_user)
1✔
286
    # Update the curator on the Work
287
    self.curator_user_id = curator_user_id
5✔
288
    save!
5✔
289

290
    # ...and log the activity
291
    new_curator = User.find(curator_user_id)
4✔
292
    message = if curator_user_id == current_user.id
4✔
293
                "Self-assigned as curator"
1✔
294
              else
295
                "Set curator to @#{new_curator.uid}"
3✔
296
              end
297
    WorkActivity.add_work_activity(id, message, current_user.id, activity_type: WorkActivity::SYSTEM)
4✔
298
  end
299

300
  def curator_or_current_uid(user)
1✔
301
    persisted = if curator.nil?
4✔
302
                  user
3✔
303
                else
304
                  curator
1✔
305
                end
306
    persisted.uid
4✔
307
  end
308

309
  def add_message(message, current_user_id)
1✔
310
    WorkActivity.add_work_activity(id, message, current_user_id, activity_type: WorkActivity::MESSAGE)
7✔
311
  end
312

313
  def add_provenance_note(date, note, current_user_id)
1✔
314
    WorkActivity.add_work_activity(id, note, current_user_id, activity_type: WorkActivity::PROVENANCE_NOTES, created_at: date)
1✔
315
  end
316

317
  def log_changes(resource_compare, current_user_id)
1✔
318
    return if resource_compare.identical?
34✔
319
    WorkActivity.add_work_activity(id, resource_compare.differences.to_json, current_user_id, activity_type: WorkActivity::CHANGES)
34✔
320
  end
321

322
  def log_file_changes(changes, current_user_id)
1✔
323
    return if changes.count == 0
68✔
324
    WorkActivity.add_work_activity(id, changes.to_json, current_user_id, activity_type: WorkActivity::FILE_CHANGES)
16✔
325
  end
326

327
  def activities
1✔
328
    WorkActivity.activities_for_work(id, WorkActivity::MESSAGE_ACTIVITY_TYPES + WorkActivity::CHANGE_LOG_ACTIVITY_TYPES)
67✔
329
  end
330

331
  def new_notification_count_for_user(user_id)
1✔
332
    WorkActivityNotification.joins(:work_activity)
64✔
333
                            .where(user_id: user_id, read_at: nil)
334
                            .where(work_activity: { work_id: id })
335
                            .count
336
  end
337

338
  # Marks as read the notifications for the given user_id in this work.
339
  # In practice, the user_id is the id of the current user and therefore this method marks the current's user
340
  # notifications as read.
341
  def mark_new_notifications_as_read(user_id)
1✔
342
    activities.each do |activity|
67✔
343
      unread_notifications = WorkActivityNotification.where(user_id: user_id, work_activity_id: activity.id, read_at: nil)
70✔
344
      unread_notifications.each do |notification|
70✔
345
        notification.read_at = Time.now.utc
21✔
346
        notification.save
21✔
347
      end
348
    end
349
  end
350

351
  def current_transition
1✔
352
    aasm.current_event.to_s.humanize.delete("!")
18✔
353
  end
354

355
  def uploads
1✔
356
    return post_curation_uploads if approved?
153✔
357

358
    pre_curation_uploads
144✔
359
  end
360

361
  # Fetches the data from S3 directly bypassing ActiveStorage
362
  def pre_curation_uploads_fast
1✔
363
    s3_query_service.client_s3_files
127✔
364
  end
365

366
  # This ensures that new ActiveStorage::Attachment objects can be modified before they are persisted
367
  def save_pre_curation_uploads
1✔
368
    return if pre_curation_uploads.empty?
717✔
369

370
    new_attachments = pre_curation_uploads.reject(&:persisted?)
188✔
371
    return if new_attachments.empty?
188✔
372

373
    save_new_attachments(new_attachments: new_attachments)
161✔
374
  end
375

376
  # Accesses post-curation S3 Bucket Objects
377
  def post_curation_s3_resources
1✔
378
    return [] unless approved?
75✔
379

380
    s3_resources
65✔
381
  end
382
  alias post_curation_uploads post_curation_s3_resources
1✔
383

384
  def s3_client
1✔
385
    s3_query_service.client
70✔
386
  end
387

388
  delegate :bucket_name, to: :s3_query_service
1✔
389

390
  # Transmit a HEAD request for an S3 Object in the post-curation Bucket
391
  # @param key [String]
392
  # @param bucket_name [String]
393
  # @return [Aws::S3::Types::HeadObjectOutput]
394
  def find_post_curation_s3_object(bucket_name:, key:)
1✔
395
    s3_client.head_object({
24✔
396
                            bucket: bucket_name,
397
                            key: key
398
                          })
399
    true
24✔
400
  rescue Aws::S3::Errors::NotFound
401
    nil
×
402
  end
403

404
  # Generates the S3 Object key
405
  # @return [String]
406
  def s3_object_key
1✔
407
    "#{doi}/#{id}"
314✔
408
  end
409

410
  # Transmit a HEAD request for the S3 Bucket directory for this Work
411
  # @param bucket_name location to be checked to be found
412
  # @return [Aws::S3::Types::HeadObjectOutput]
413
  def find_post_curation_s3_dir(bucket_name:)
1✔
414
    s3_client.head_object({
23✔
415
                            bucket: bucket_name,
416
                            key: s3_object_key
417
                          })
418
    true
×
419
  rescue Aws::S3::Errors::NotFound
420
    nil
23✔
421
  end
422

423
  # Transmit a DELETE request for the S3 directory in the pre-curation Bucket
424
  # @return [Aws::S3::Types::DeleteObjectOutput]
425
  def delete_pre_curation_s3_dir
1✔
426
    s3_client.delete_object({
23✔
427
                              bucket: bucket_name,
428
                              key: s3_object_key
429
                            })
430
  rescue Aws::S3::Errors::ServiceError => error
431
    raise(StandardError, "Failed to delete the pre-curation S3 Bucket directory #{s3_object_key}: #{error}")
×
432
  end
433

434
  # This is invoked within the scope of #after_save. Attachment objects require that the parent record be persisted (hence, #before_save is not an option).
435
  # However, a consequence of this is that #after_save is invoked whenever a new attached Blob or Attachment object is persisted.
436
  def attach_s3_resources
1✔
437
    return if approved?
94✔
438
    changes = []
59✔
439
    # This retrieves and adds S3 uploads if they do not exist
440
    pre_curation_s3_resources.each do |s3_file|
59✔
441
      if add_pre_curation_s3_object(s3_file)
16✔
442
        changes << { action: :added, filename: s3_file.filename }
13✔
443
      end
444
    end
445

446
    # Log the new files, but don't link the change to the current_user since we really don't know
447
    # who added the files directly to AWS S3.
448
    log_file_changes(changes, nil)
59✔
449
  end
450

451
  def as_json(options = nil)
1✔
452
    if options&.present?
15✔
453
      raise(StandardError, "Received options #{options}, but not supported")
×
454
      # Included in signature for compatibility with Rails.
455
    end
456

457
    # Pre-curation files are not accessible externally,
458
    # so we are not interested in listing them in JSON.
459
    # (The items in pre_curation_uploads also have different properties.)
460
    files = post_curation_uploads.map do |upload|
15✔
461
      {
462
        "filename": upload.filename,
8✔
463
        "size": upload.size,
464
        "url": upload.globus_url
465
      }
466
    end
467

468
    # to_json returns a string of serialized JSON.
469
    # as_json returns the corresponding hash.
470
    {
471
      "resource" => resource.as_json,
15✔
472
      "files" => files,
473
      "collection" => collection.as_json.except("id")
474
    }
475
  end
476

477
  delegate :ark, :doi, :resource_type, :resource_type=, :resource_type_general, :resource_type_general=,
1✔
478
           :to_xml, to: :resource
479

480
  protected
1✔
481

482
    # This must be protected, NOT private for ActiveRecord to work properly with this attribute.
483
    #   Protected will still keep others from setting the metatdata, but allows ActiveRecord the access it needs
484
    def metadata=(metadata)
1✔
485
      super
1,130✔
486
      @resource = PDCMetadata::Resource.new_from_jsonb(metadata)
1,130✔
487
    end
488

489
  private
1✔
490

491
    def publish(user)
1✔
492
      publish_doi(user)
23✔
493
      update_ark_information
23✔
494
      publish_precurated_files
23✔
495
      save!
23✔
496
    end
497

498
    # Update EZID (our provider of ARKs) with the new information for this work.
499
    def update_ark_information
1✔
500
      # We only want to update the ark url under certain conditions.
501
      # Set this value in config/update_ark_url.yml
502
      if Rails.configuration.update_ark_url
23✔
503
        if ark.present?
5✔
504
          Ark.update(ark, url)
3✔
505
        end
506
      end
507
    end
508

509
    # Generates the key for ActiveStorage::Attachment and Attachment::Blob objects
510
    # @param attachment [ActiveStorage::Attachment]
511
    # @return [String]
512
    def generate_attachment_key(attachment)
1✔
513
      attachment_filename = attachment.filename.to_s
109✔
514
      attachment_key = attachment.key
109✔
515

516
      # Files actually coming from S3 include the DOI and bucket as part of the file name
517
      #  Files being attached in another manner may not have it, so we should include it.
518
      #  This is really for testing only.
519
      key_base = "#{doi}/#{id}"
109✔
520
      attachment_key = [key_base, attachment_filename].join("/") unless attachment_key.include?(key_base)
109✔
521

522
      attachment_ext = File.extname(attachment_filename)
109✔
523
      attachment_query = attachment_key.gsub(attachment_ext, "")
109✔
524
      results = ActiveStorage::Blob.where("key LIKE :query", query: "%#{attachment_query}%")
109✔
525
      blobs = results.to_a
109✔
526

527
      if blobs.present?
109✔
528
        index = blobs.length + 1
28✔
529
        attachment_key = attachment_key.gsub(/\.([a-zA-Z0-9\.]+)$/, "_#{index}.\\1")
28✔
530
      end
531

532
      attachment_key
109✔
533
    end
534

535
    def track_state_change(user, state = aasm.to_state)
1✔
536
      uw = UserWork.new(user_id: user.id, work_id: id, state: state)
129✔
537
      uw.save!
129✔
538
      WorkActivity.add_work_activity(id, "marked as #{state.to_s.titleize}", user.id, activity_type: WorkActivity::SYSTEM)
129✔
539
      WorkStateTransitionNotification.new(self, user.id).send
129✔
540
    end
541

542
    def data_cite_connection
1✔
543
      @data_cite_connection ||= Datacite::Client.new(username: Rails.configuration.datacite.user,
38✔
544
                                                     password: Rails.configuration.datacite.password,
545
                                                     host: Rails.configuration.datacite.host)
546
    end
547

548
    def validate_ark
1✔
549
      return if ark.blank?
746✔
550
      first_save = id.blank?
128✔
551
      changed_value = metadata["ark"] != ark
128✔
552
      if first_save || changed_value
128✔
553
        errors.add(:base, "Invalid ARK provided for the Work: #{ark}") unless Ark.valid?(ark)
42✔
554
      end
555
    end
556

557
    # rubocop:disable Metrics/AbcSize
558
    def validate_metadata
1✔
559
      return if metadata.blank?
297✔
560
      errors.add(:base, "Must provide a title") if resource.main_title.blank?
297✔
561
      errors.add(:base, "Must provide a description") if resource.description.blank?
297✔
562
      errors.add(:base, "Must indicate the Publisher") if resource.publisher.blank?
297✔
563
      errors.add(:base, "Must indicate the Publication Year") if resource.publication_year.blank?
297✔
564
      errors.add(:base, "Must indicate a Rights statement") if resource.rights.nil?
297✔
565
      errors.add(:base, "Must provide a Version number") if resource.version_number.blank?
297✔
566
      validate_creators
297✔
567
      validate_related_objects
297✔
568
    end
569
    # rubocop:enable Metrics/AbcSize
570

571
    def validate_creators
1✔
572
      if resource.creators.count == 0
1,043✔
573
        errors.add(:base, "Must provide at least one Creator")
1✔
574
      else
575
        resource.creators.each do |creator|
1,042✔
576
          if creator.orcid.present? && Orcid.invalid?(creator.orcid)
1,612✔
577
            errors.add(:base, "ORCID for creator #{creator.value} is not in format 0000-0000-0000-0000")
1✔
578
          end
579
        end
580
      end
581
    end
582

583
    def validate_related_objects
1✔
584
      return if resource.related_objects.empty?
297✔
585
      invalid = resource.related_objects.reject(&:valid?)
6✔
586
      errors.add(:base, "Related Objects are invalid: #{invalid.map(&:errors).join(', ')}") if invalid.count.positive?
6✔
587
    end
588

589
    def publish_doi(user)
1✔
590
      return Rails.logger.info("Publishing hard-coded test DOI during development.") if self.class.publish_test_doi?
21✔
591

592
      if doi.starts_with?(Rails.configuration.datacite.prefix)
21✔
593
        result = data_cite_connection.update(id: doi, attributes: doi_attributes)
19✔
594
        if result.failure?
19✔
595
          resolved_user = curator_or_current_uid(user)
3✔
596
          message = "@#{resolved_user} Error publishing DOI. #{result.failure.status} / #{result.failure.reason_phrase}"
3✔
597
          WorkActivity.add_work_activity(id, message, user.id, activity_type: WorkActivity::DATACITE_ERROR)
3✔
598
        end
599
      elsif ark.blank? # we can not update the url anywhere
2✔
600
        Honeybadger.notify("Publishing for a DOI we do not own and no ARK is present: #{doi}")
1✔
601
      end
602
    end
603

604
    def doi_attribute_url
1✔
605
      "https://datacommons.princeton.edu/discovery/doi/#{doi}"
19✔
606
    end
607

608
    def doi_attribute_resource
1✔
609
      PDCMetadata::Resource.new_from_jsonb(metadata)
19✔
610
    end
611

612
    def doi_attribute_xml
1✔
613
      unencoded = doi_attribute_resource.to_xml
19✔
614
      Base64.encode64(unencoded)
19✔
615
    end
616

617
    def doi_attributes
1✔
618
      {
619
        "event" => "publish",
19✔
620
        "xml" => doi_attribute_xml,
621
        "url" => doi_attribute_url
622
      }
623
    end
624

625
    def validate_uploads
1✔
626
      # The number of pre-curation uploads should be validated, as these are mutated directly
627
      if pre_curation_uploads.length > MAX_UPLOADS
746✔
628
        errors.add(:base, "Only #{MAX_UPLOADS} files may be uploaded by a user to a given Work. #{pre_curation_uploads.length} files were uploaded for the Work: #{ark}")
2✔
629
      end
630
    end
631

632
    # This needs to be called #before_save
633
    # This ensures that new ActiveStorage::Attachment objects are persisted with custom keys (which are generated from the file name and DOI)
634
    # @param new_attachments [Array<ActiveStorage::Attachment>]
635
    def save_new_attachments(new_attachments:)
1✔
636
      new_attachments.each do |attachment|
161✔
637
        # There are cases (race conditions?) where the ActiveStorage::Blob objects are not persisted
638
        next if attachment.frozen?
167✔
639

640
        # This ensures that the custom key for the ActiveStorage::Attachment and ActiveStorage::Blob objects are generated
641
        generated_key = generate_attachment_key(attachment)
109✔
642
        attachment.blob.key = generated_key
109✔
643
        attachment.blob.save
109✔
644

645
        attachment.save
109✔
646
      end
647
    end
648

649
    # S3QueryService object associated with this Work
650
    # @return [S3QueryService]
651
    def s3_query_service
1✔
652
      @s3_query_service = S3QueryService.new(self, !approved?)
344✔
653
    end
654

655
    # Request S3 Bucket Objects associated with this Work
656
    # @return [Array<S3File>]
657
    def s3_resources
1✔
658
      data_profile = s3_query_service.data_profile
124✔
659
      data_profile.fetch(:objects, [])
124✔
660
    end
661
    alias pre_curation_s3_resources s3_resources
1✔
662

663
    def s3_object_persisted?(s3_file)
1✔
664
      uploads_keys = uploads.map(&:key)
16✔
665
      uploads_keys.include?(s3_file.key)
16✔
666
    end
667

668
    def add_pre_curation_s3_object(s3_file)
1✔
669
      return if s3_object_persisted?(s3_file)
16✔
670

671
      persisted = s3_file.to_blob
13✔
672
      pre_curation_uploads.attach(persisted)
13✔
673
    end
674

675
    def publish_precurated_files
1✔
676
      # An error is raised if there are no files to be moved
677
      raise(StandardError, "Attempting to publish a Work without attached uploads for #{s3_object_key}") if pre_curation_uploads.empty? && post_curation_uploads.empty?
23✔
678

679
      # We need to explicitly access to post-curation services here.
680
      # Lets explicitly create it so the state of the work does not have any impact.
681
      s3_post_curation_query_service = S3QueryService.new(self, false)
23✔
682

683
      s3_dir = find_post_curation_s3_dir(bucket_name: s3_post_curation_query_service.bucket_name)
23✔
684
      raise(StandardError, "Attempting to publish a Work with an existing S3 Bucket directory for: #{s3_object_key}") unless s3_dir.nil?
23✔
685

686
      # Copy the pre-curation S3 Objects to the post-curation S3 Bucket...
687
      transferred_files = s3_post_curation_query_service.publish_files
23✔
688

689
      # ...check that the files are indeed now in the post-curation bucket...
690
      pre_curation_uploads.each do |attachment|
23✔
691
        s3_object = find_post_curation_s3_object(bucket_name: s3_post_curation_query_service.bucket_name, key: attachment.key)
24✔
692
        raise(StandardError, "Failed to validate the uploaded S3 Object #{attachment.key}") if s3_object.nil?
24✔
693
      end
694

695
      # ...and delete them from the pre-curation bucket.
696
      transferred_files.each(&:purge)
23✔
697
      delete_pre_curation_s3_dir
23✔
698
    end
699
end
700
# rubocop:enable Metrics/ClassLength
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc