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

pulibrary / pdc_describe / 31a62f9e-3a5b-4033-a3e3-effd2d651737

pending completion
31a62f9e-3a5b-4033-a3e3-effd2d651737

Pull #962

circleci

Hector Correa
Fixes tests for S3 query service (and a couple of code duplications from a merge?)
Pull Request #962: Fetch pre-curation files from directly from AWS

35 of 35 new or added lines in 4 files covered. (100.0%)

1821 of 1864 relevant lines covered (97.69%)

149.32 hits per line

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

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

3
# rubocop:disable Metrics/ClassLength
4
class Work < ApplicationRecord
1✔
5
  # Errors for cases where there is no valid Collection
6
  class InvalidCollectionError < ::ArgumentError; end
1✔
7

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

12
  belongs_to :collection
1✔
13
  belongs_to :curator, class_name: "User", foreign_key: "curator_user_id", optional: true
1✔
14

15
  attribute :work_type, :string, default: "DATASET"
1✔
16
  attribute :profile, :string, default: "DATACITE"
1✔
17

18
  attr_accessor :user_entered_doi
1✔
19

20
  alias state_history user_work
1✔
21

22
  include AASM
1✔
23

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

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

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

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

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

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

48
    event :resubmit do
1✔
49
      transitions from: :withdrawn, to: :draft
1✔
50
    end
51

52
    event :remove do
1✔
53
      transitions from: :withdrawn, to: :tombstone
1✔
54
    end
55

56
    after_all_events :track_state_change
1✔
57
  end
58

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

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

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

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

86
  def submitted_by?(user)
1✔
87
    created_by_user_id == user.id
141✔
88
  end
89

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

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

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

107
    delegate :resource_type_general_values, to: PDCMetadata::Resource
1✔
108

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

117
  include Rails.application.routes.url_helpers
1✔
118

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

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

132
  validate do |work|
1✔
133
    if none?
680✔
134
      work.validate_doi
100✔
135
    elsif draft?
580✔
136
      work.valid_to_draft
363✔
137
    else
138
      work.valid_to_submit
217✔
139
    end
140
  end
141

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

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

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

178
  def valid_to_submit
1✔
179
    valid_to_draft
286✔
180
    validate_metadata
286✔
181
    errors.count == 0
286✔
182
  end
183

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

192
  def title
1✔
193
    resource.main_title
252✔
194
  end
195

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

209
  def form_attributes
1✔
210
    {
211
      uploads: uploads_attributes
35✔
212
    }
213
  end
214

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

231
  def created_by_user
1✔
232
    User.find(created_by_user_id)
301✔
233
  rescue ActiveRecord::RecordNotFound
234
    nil
1✔
235
  end
236

237
  def resource=(resource)
1✔
238
    @resource = resource
403✔
239
    # Ensure that the metadata JSONB postgres field is persisted properly
240
    self.metadata = JSON.parse(resource.to_json)
403✔
241
  end
242

243
  def resource
1✔
244
    @resource ||= PDCMetadata::Resource.new_from_jsonb(metadata)
11,252✔
245
  end
246

247
  def url
1✔
248
    return unless persisted?
3✔
249

250
    @url ||= url_for(self)
3✔
251
  end
252

253
  def files_location_upload?
1✔
254
    files_location.blank? || files_location == "file_upload"
2✔
255
  end
256

257
  def files_location_cluster?
1✔
258
    files_location == "file_cluster"
42✔
259
  end
260

261
  def files_location_other?
1✔
262
    files_location == "file_other"
42✔
263
  end
264

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

273
  def clear_curator(current_user)
1✔
274
    # Update the curator on the Work
275
    self.curator_user_id = nil
2✔
276
    save!
2✔
277

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

282
  def update_curator(curator_user_id, current_user)
1✔
283
    # Update the curator on the Work
284
    self.curator_user_id = curator_user_id
5✔
285
    save!
5✔
286

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

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

306
  def add_message(message, current_user_id)
1✔
307
    WorkActivity.add_work_activity(id, message, current_user_id, activity_type: WorkActivity::MESSAGE)
7✔
308
  end
309

310
  def add_provenance_note(date, note, current_user_id)
1✔
311
    WorkActivity.add_work_activity(id, note, current_user_id, activity_type: WorkActivity::PROVENANCE_NOTES, created_at: date)
1✔
312
  end
313

314
  def log_changes(resource_compare, current_user_id)
1✔
315
    return if resource_compare.identical?
22✔
316
    WorkActivity.add_work_activity(id, resource_compare.differences.to_json, current_user_id, activity_type: WorkActivity::CHANGES)
22✔
317
  end
318

319
  def log_file_changes(changes, current_user_id)
1✔
320
    return if changes.count == 0
4✔
321
    WorkActivity.add_work_activity(id, changes.to_json, current_user_id, activity_type: WorkActivity::FILE_CHANGES)
4✔
322
  end
323

324
  def activities
1✔
325
    WorkActivity.activities_for_work(id, WorkActivity::MESSAGE_ACTIVITY_TYPES + WorkActivity::CHANGE_LOG_ACTIVITY_TYPES)
61✔
326
  end
327

328
  def new_notification_count_for_user(user_id)
1✔
329
    WorkActivityNotification.joins(:work_activity)
55✔
330
                            .where(user_id: user_id, read_at: nil)
331
                            .where(work_activity: { work_id: id })
332
                            .count
333
  end
334

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

348
  def current_transition
1✔
349
    aasm.current_event.to_s.humanize.delete("!")
16✔
350
  end
351

352
  def uploads
1✔
353
    return post_curation_uploads if approved?
123✔
354

355
    pre_curation_uploads_fast
106✔
356
  end
357

358
  # Fetches the data from S3 directly bypassing ActiveStorage
359
  def pre_curation_uploads_fast
1✔
360
    s3_query_service.client_s3_files
217✔
361
  end
362

363
  # This ensures that new ActiveStorage::Attachment objects can be modified before they are persisted
364
  def save_pre_curation_uploads
1✔
365
    return if pre_curation_uploads.empty?
658✔
366

367
    new_attachments = pre_curation_uploads.reject(&:persisted?)
139✔
368
    return if new_attachments.empty?
139✔
369

370
    save_new_attachments(new_attachments: new_attachments)
119✔
371
  end
372

373
  # Accesses post-curation S3 Bucket Objects
374
  def post_curation_s3_resources
1✔
375
    return [] unless approved?
92✔
376

377
    s3_resources
83✔
378
  end
379
  alias post_curation_uploads post_curation_s3_resources
1✔
380

381
  def s3_client
1✔
382
    s3_query_service.client
70✔
383
  end
384

385
  delegate :bucket_name, to: :s3_query_service
1✔
386

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

401
  # Generates the S3 Object key
402
  # @return [String]
403
  def s3_object_key
1✔
404
    "#{doi}/#{id}"
303✔
405
  end
406

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

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

431
  # 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).
432
  # However, a consequence of this is that #after_save is invoked whenever a new attached Blob or Attachment object is persisted.
433
  def attach_s3_resources
1✔
434
    return if approved?
25✔
435
    changes = []
2✔
436
    # This retrieves and adds S3 uploads if they do not exist
437
    pre_curation_s3_resources.each do |s3_file|
2✔
438
      if add_pre_curation_s3_object(s3_file)
2✔
439
        changes << { action: :added, filename: s3_file.filename }
×
440
      end
441
    end
442

443
    # Log the new files, but don't link the change to the current_user since we really don't know
444
    # who added the files directly to AWS S3.
445
    log_file_changes(changes, nil)
×
446
  end
447

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

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

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

474
  def pre_curation_uploads_count
1✔
475
    s3_query_service.file_count
14✔
476
  end
477

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

481

482
  # S3QueryService object associated with this Work
483
  # @return [S3QueryService]
484
  def s3_query_service
1✔
485
    @s3_query_service ||= S3QueryService.new(self, !approved?)
445✔
486
  end
487

488
  protected
1✔
489

490
    # This must be protected, NOT private for ActiveRecord to work properly with this attribute.
491
    #   Protected will still keep others from setting the metatdata, but allows ActiveRecord the access it needs
492
    def metadata=(metadata)
1✔
493
      super
1,061✔
494
      @resource = PDCMetadata::Resource.new_from_jsonb(metadata)
1,061✔
495
    end
496

497
  private
1✔
498

499
    def publish(user)
1✔
500
      publish_doi(user)
23✔
501
      update_ark_information
23✔
502
      publish_precurated_files
23✔
503
      save!
23✔
504
    end
505

506
    # Update EZID (our provider of ARKs) with the new information for this work.
507
    def update_ark_information
1✔
508
      # We only want to update the ark url under certain conditions.
509
      # Set this value in config/update_ark_url.yml
510
      if Rails.configuration.update_ark_url
23✔
511
        if ark.present?
6✔
512
          Ark.update(ark, url)
3✔
513
        end
514
      end
515
    end
516

517
    # Generates the key for ActiveStorage::Attachment and Attachment::Blob objects
518
    # @param attachment [ActiveStorage::Attachment]
519
    # @return [String]
520
    def generate_attachment_key(attachment)
1✔
521
      attachment_filename = attachment.filename.to_s
69✔
522
      attachment_key = attachment.key
69✔
523

524
      # Files actually coming from S3 include the DOI and bucket as part of the file name
525
      #  Files being attached in another manner may not have it, so we should include it.
526
      #  This is really for testing only.
527
      key_base = "#{doi}/#{id}"
69✔
528
      attachment_key = [key_base, attachment_filename].join("/") unless attachment_key.include?(key_base)
69✔
529

530
      attachment_ext = File.extname(attachment_filename)
69✔
531
      attachment_query = attachment_key.gsub(attachment_ext, "")
69✔
532
      results = ActiveStorage::Blob.where("key LIKE :query", query: "%#{attachment_query}%")
69✔
533
      blobs = results.to_a
69✔
534

535
      if blobs.present?
69✔
536
        index = blobs.length + 1
10✔
537
        attachment_key = attachment_key.gsub(/\.([a-zA-Z0-9\.]+)$/, "_#{index}.\\1")
10✔
538
      end
539

540
      attachment_key
69✔
541
    end
542

543
    def track_state_change(user, state = aasm.to_state)
1✔
544
      uw = UserWork.new(user_id: user.id, work_id: id, state: state)
126✔
545
      uw.save!
126✔
546
      WorkActivity.add_work_activity(id, "marked as #{state.to_s.titleize}", user.id, activity_type: WorkActivity::SYSTEM)
126✔
547
      WorkStateTransitionNotification.new(self, user.id).send
126✔
548
    end
549

550
    def data_cite_connection
1✔
551
      @data_cite_connection ||= Datacite::Client.new(username: Rails.configuration.datacite.user,
37✔
552
                                                     password: Rails.configuration.datacite.password,
553
                                                     host: Rails.configuration.datacite.host)
554
    end
555

556
    def validate_ark
1✔
557
      return if ark.blank?
680✔
558
      first_save = id.blank?
113✔
559
      changed_value = metadata["ark"] != ark
113✔
560
      if first_save || changed_value
113✔
561
        errors.add(:base, "Invalid ARK provided for the Work: #{ark}") unless Ark.valid?(ark)
40✔
562
      end
563
    end
564

565
    # rubocop:disable Metrics/AbcSize
566
    def validate_metadata
1✔
567
      return if metadata.blank?
286✔
568
      errors.add(:base, "Must provide a title") if resource.main_title.blank?
286✔
569
      errors.add(:base, "Must provide a description") if resource.description.blank?
286✔
570
      errors.add(:base, "Must indicate the Publisher") if resource.publisher.blank?
286✔
571
      errors.add(:base, "Must indicate the Publication Year") if resource.publication_year.blank?
286✔
572
      errors.add(:base, "Must indicate a Rights statement") if resource.rights.nil?
286✔
573
      errors.add(:base, "Must provide a Version number") if resource.version_number.blank?
286✔
574
      validate_creators
286✔
575
      validate_related_objects
286✔
576
    end
577
    # rubocop:enable Metrics/AbcSize
578

579
    def validate_creators
1✔
580
      if resource.creators.count == 0
966✔
581
        errors.add(:base, "Must provide at least one Creator")
1✔
582
      else
583
        resource.creators.each do |creator|
965✔
584
          if creator.orcid.present? && Orcid.invalid?(creator.orcid)
1,414✔
585
            errors.add(:base, "ORCID for creator #{creator.value} is not in format 0000-0000-0000-0000")
1✔
586
          end
587
        end
588
      end
589
    end
590

591
    def validate_related_objects
1✔
592
      return if resource.related_objects.empty?
286✔
593
      invalid = resource.related_objects.reject(&:valid?)
3✔
594
      errors.add(:base, "Related Objects are invalid: #{invalid.map(&:errors).join(', ')}") if invalid.count.positive?
3✔
595
    end
596

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

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

612
    def doi_attribute_url
1✔
613
      "https://datacommons.princeton.edu/discovery/doi/#{doi}"
19✔
614
    end
615

616
    def doi_attribute_resource
1✔
617
      PDCMetadata::Resource.new_from_jsonb(metadata)
19✔
618
    end
619

620
    def doi_attribute_xml
1✔
621
      unencoded = doi_attribute_resource.to_xml
19✔
622
      Base64.encode64(unencoded)
19✔
623
    end
624

625
    def doi_attributes
1✔
626
      {
627
        "event" => "publish",
19✔
628
        "xml" => doi_attribute_xml,
629
        "url" => doi_attribute_url
630
      }
631
    end
632

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

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

646
        attachment.save
69✔
647
      end
648
    end
649

650
    # Request S3 Bucket Objects associated with this Work
651
    # @return [Array<S3File>]
652
    def s3_resources
1✔
653
      data_profile = s3_query_service.data_profile
85✔
654
      data_profile.fetch(:objects, [])
85✔
655
    end
656
    alias pre_curation_s3_resources s3_resources
1✔
657

658
    def s3_object_persisted?(s3_file)
1✔
659
      uploads_keys = uploads.map(&:key)
2✔
660
      uploads_keys.include?(s3_file.key)
×
661
    end
662

663
    def add_pre_curation_s3_object(s3_file)
1✔
664
      return if s3_object_persisted?(s3_file)
2✔
665

666
      persisted = s3_file.to_blob
×
667
      pre_curation_uploads.attach(persisted)
×
668
    end
669

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

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

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

681
      # Copy the pre-curation S3 Objects to the post-curation S3 Bucket...
682
      transferred_files = s3_post_curation_query_service.publish_files
23✔
683

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

690
      # ...and delete them from the pre-curation bucket.
691
      transferred_files.each(&:purge)
23✔
692
      delete_pre_curation_s3_dir
23✔
693
    end
694
end
695
# 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