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

pulibrary / pdc_describe / 599eeb90-6ff1-4336-9df7-f77411624ed7

pending completion
599eeb90-6ff1-4336-9df7-f77411624ed7

Pull #1028

circleci

jrgriffiniii
wip
Pull Request #1028: Implementing the UploadSnapshotsController

70 of 70 new or added lines in 5 files covered. (100.0%)

1910 of 1951 relevant lines covered (97.9%)

173.48 hits per line

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

96.52
/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
186✔
9
  has_many :user_work, -> { order(updated_at: :desc) }, dependent: :destroy
9✔
10
  has_many :upload_snapshots, -> { order(updated_at: :desc) }, dependent: :destroy
6✔
11
  has_many_attached :pre_curation_uploads, service: :amazon_pre_curation
1✔
12

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

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

19
  attr_accessor :user_entered_doi
1✔
20

21
  alias state_history user_work
1✔
22

23
  include AASM
1✔
24

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

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

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

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

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

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

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

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

57
    after_all_events :track_state_change
1✔
58
  end
59

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

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

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

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

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

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

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

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

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

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

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

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

126
  after_save do |work|
1✔
127
    if work.approved?
716✔
128
      work.reload
91✔
129
    end
130
  end
131

132
  validate do |work|
1✔
133
    if none?
743✔
134
      work.validate_doi
105✔
135
    elsif draft?
638✔
136
      work.valid_to_draft
405✔
137
    else
138
      work.valid_to_submit
233✔
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
187✔
155
    # Force `resource` to be reloaded
156
    @resource = nil
187✔
157
    self
187✔
158
  end
159

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

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

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

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

192
  def title
1✔
193
    resource.main_title
307✔
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
56✔
198
    uploads.map do |upload|
52✔
199
      {
200
        id: upload.id,
20✔
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
56✔
212
    }
213
  end
214

215
  def draft_doi
1✔
216
    return if resource.doi.present?
36✔
217
    resource.doi = if self.class.publish_test_doi?
20✔
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)
19✔
222
                     if result.success?
19✔
223
                       result.success.doi
18✔
224
                     else
225
                       raise("Error generating DOI. #{result.failure.status} / #{result.failure.reason_phrase}")
1✔
226
                     end
227
                   end
228
    save!
19✔
229
  end
230

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

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

243
  def resource
1✔
244
    @resource ||= PDCMetadata::Resource.new_from_jsonb(metadata)
13,988✔
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"
5✔
255
  end
256

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

261
  def files_location_other?
1✔
262
    files_location == "file_other"
63✔
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?
12✔
299
                  user
11✔
300
                else
301
                  curator
1✔
302
                end
303
    persisted.uid
12✔
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)
11✔
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?
38✔
316
    WorkActivity.add_work_activity(id, resource_compare.differences.to_json, current_user_id, activity_type: WorkActivity::CHANGES)
38✔
317
  end
318

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

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

328
  def new_notification_count_for_user(user_id)
1✔
329
    WorkActivityNotification.joins(:work_activity)
82✔
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|
79✔
340
      unread_notifications = WorkActivityNotification.where(user_id: user_id, work_activity_id: activity.id, read_at: nil)
90✔
341
      unread_notifications.each do |notification|
90✔
342
        notification.read_at = Time.now.utc
26✔
343
        notification.save
26✔
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?
486✔
354

355
    pre_curation_uploads_fast
419✔
356
  end
357

358
  def local_pre_curation_s3_files
1✔
359
    return [] unless Rails.env.development?
×
360
    loaded = pre_curation_uploads.sort_by(&:filename)
×
361

362
    loaded.map do |attachment|
×
363
      S3File.new(
×
364
        work: self,
365
        filename: attachment.key,
366
        last_modified: attachment.created_at,
367
        size: nil,
368
        checksum: ""
369
      )
370
    end
371
  end
372

373
  # Fetches the data from S3 directly bypassing ActiveStorage
374
  def pre_curation_uploads_fast
1✔
375
    return local_pre_curation_s3_files if Rails.env.development?
547✔
376

377
    s3_query_service.client_s3_files.sort_by(&:filename)
547✔
378
  end
379

380
  # This ensures that new ActiveStorage::Attachment objects can be modified before they are persisted
381
  def save_pre_curation_uploads
1✔
382
    return if pre_curation_uploads.empty?
717✔
383

384
    new_attachments = pre_curation_uploads.reject(&:persisted?)
36✔
385
    return if new_attachments.empty?
36✔
386

387
    save_new_attachments(new_attachments: new_attachments)
28✔
388
  end
389

390
  # Accesses post-curation S3 Bucket Objects
391
  def post_curation_s3_resources
1✔
392
    return [] unless approved?
141✔
393

394
    s3_resources
101✔
395
  end
396
  alias post_curation_uploads post_curation_s3_resources
1✔
397

398
  def s3_client
1✔
399
    s3_query_service.client
22✔
400
  end
401

402
  delegate :bucket_name, to: :s3_query_service
1✔
403

404
  # Generates the S3 Object key
405
  # @return [String]
406
  def s3_object_key
1✔
407
    "#{doi}/#{id}"
51✔
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
    # TODO: Directories really do not exists in S3
415
    #      if we really need this check then we need to do something else to check the bucket
416
    s3_client.head_object({
22✔
417
                            bucket: bucket_name,
418
                            key: s3_object_key
419
                          })
420
    true
×
421
  rescue Aws::S3::Errors::NotFound
422
    nil
22✔
423
  end
424

425
  def as_json(*)
1✔
426
    # Pre-curation files are not accessible externally,
427
    # so we are not interested in listing them in JSON.
428
    # (The items in pre_curation_uploads also have different properties.)
429
    files = post_curation_uploads.map do |upload|
62✔
430
      {
431
        "filename": upload.filename,
42✔
432
        "size": upload.size,
433
        "url": upload.globus_url
434
      }
435
    end
436

437
    # to_json returns a string of serialized JSON.
438
    # as_json returns the corresponding hash.
439
    {
440
      "resource" => resource.as_json,
62✔
441
      "files" => files,
442
      "collection" => collection.as_json.except("id")
443
    }
444
  end
445

446
  def pre_curation_uploads_count
1✔
447
    s3_query_service.file_count
2✔
448
  end
449

450
  delegate :ark, :doi, :resource_type, :resource_type=, :resource_type_general, :resource_type_general=,
1✔
451
           :to_xml, to: :resource
452

453
  # S3QueryService object associated with this Work
454
  # @return [S3QueryService]
455
  def s3_query_service
1✔
456
    @s3_query_service ||= S3QueryService.new(self, !approved?)
746✔
457
  end
458

459
  protected
1✔
460

461
    # This must be protected, NOT private for ActiveRecord to work properly with this attribute.
462
    #   Protected will still keep others from setting the metatdata, but allows ActiveRecord the access it needs
463
    def metadata=(metadata)
1✔
464
      super
1,190✔
465
      @resource = PDCMetadata::Resource.new_from_jsonb(metadata)
1,190✔
466
    end
467

468
  private
1✔
469

470
    def publish(user)
1✔
471
      publish_doi(user)
30✔
472
      update_ark_information
30✔
473
      publish_precurated_files
30✔
474
      save!
30✔
475
    end
476

477
    # Update EZID (our provider of ARKs) with the new information for this work.
478
    def update_ark_information
1✔
479
      # We only want to update the ark url under certain conditions.
480
      # Set this value in config/update_ark_url.yml
481
      if Rails.configuration.update_ark_url
30✔
482
        if ark.present?
15✔
483
          Ark.update(ark, url)
3✔
484
        end
485
      end
486
    end
487

488
    # Generates the key for ActiveStorage::Attachment and Attachment::Blob objects
489
    # @param attachment [ActiveStorage::Attachment]
490
    # @return [String]
491
    def generate_attachment_key(attachment)
1✔
492
      attachment_filename = attachment.filename.to_s
24✔
493
      attachment_key = attachment.key
24✔
494

495
      # Files actually coming from S3 include the DOI and bucket as part of the file name
496
      #  Files being attached in another manner may not have it, so we should include it.
497
      #  This is really for testing only.
498
      key_base = "#{doi}/#{id}"
24✔
499
      attachment_key = [key_base, attachment_filename].join("/") unless attachment_key.include?(key_base)
24✔
500

501
      attachment_ext = File.extname(attachment_filename)
24✔
502
      attachment_query = attachment_key.gsub(attachment_ext, "")
24✔
503
      results = ActiveStorage::Blob.where("key LIKE :query", query: "%#{attachment_query}%")
24✔
504
      blobs = results.to_a
24✔
505

506
      if blobs.present?
24✔
507
        index = blobs.length + 1
4✔
508
        attachment_key = attachment_key.gsub(/\.([a-zA-Z0-9\.]+)$/, "_#{index}.\\1")
4✔
509
      end
510

511
      attachment_key
24✔
512
    end
513

514
    def track_state_change(user, state = aasm.to_state)
1✔
515
      uw = UserWork.new(user_id: user.id, work_id: id, state: state)
140✔
516
      uw.save!
140✔
517
      WorkActivity.add_work_activity(id, "marked as #{state.to_s.titleize}", user.id, activity_type: WorkActivity::SYSTEM)
140✔
518
      WorkStateTransitionNotification.new(self, user.id).send
140✔
519
    end
520

521
    def data_cite_connection
1✔
522
      @data_cite_connection ||= Datacite::Client.new(username: Rails.configuration.datacite.user,
45✔
523
                                                     password: Rails.configuration.datacite.password,
524
                                                     host: Rails.configuration.datacite.host)
525
    end
526

527
    def validate_ark
1✔
528
      return if ark.blank?
752✔
529
      first_save = id.blank?
135✔
530
      changed_value = metadata["ark"] != ark
135✔
531
      if first_save || changed_value
135✔
532
        errors.add(:base, "Invalid ARK provided for the Work: #{ark}") unless Ark.valid?(ark)
49✔
533
      end
534
    end
535

536
    # rubocop:disable Metrics/AbcSize
537
    def validate_metadata
1✔
538
      return if metadata.blank?
312✔
539
      errors.add(:base, "Must provide a title") if resource.main_title.blank?
312✔
540
      errors.add(:base, "Must provide a description") if resource.description.blank?
312✔
541
      errors.add(:base, "Must indicate the Publisher") if resource.publisher.blank?
312✔
542
      errors.add(:base, "Must indicate the Publication Year") if resource.publication_year.blank?
312✔
543
      errors.add(:base, "Must indicate a Rights statement") if resource.rights.nil?
312✔
544
      errors.add(:base, "Must provide a Version number") if resource.version_number.blank?
312✔
545
      validate_creators
312✔
546
      validate_related_objects
312✔
547
    end
548
    # rubocop:enable Metrics/AbcSize
549

550
    def validate_creators
1✔
551
      if resource.creators.count == 0
1,064✔
552
        errors.add(:base, "Must provide at least one Creator")
1✔
553
      else
554
        resource.creators.each do |creator|
1,063✔
555
          if creator.orcid.present? && Orcid.invalid?(creator.orcid)
1,809✔
556
            errors.add(:base, "ORCID for creator #{creator.value} is not in format 0000-0000-0000-0000")
1✔
557
          end
558
        end
559
      end
560
    end
561

562
    def validate_related_objects
1✔
563
      return if resource.related_objects.empty?
312✔
564
      invalid = resource.related_objects.reject(&:valid?)
6✔
565
      errors.add(:base, "Related Objects are invalid: #{invalid.map(&:errors).join(', ')}") if invalid.count.positive?
6✔
566
    end
567

568
    def publish_doi(user)
1✔
569
      return Rails.logger.info("Publishing hard-coded test DOI during development.") if self.class.publish_test_doi?
28✔
570

571
      if doi.starts_with?(Rails.configuration.datacite.prefix)
28✔
572
        result = data_cite_connection.update(id: doi, attributes: doi_attributes)
26✔
573
        if result.failure?
26✔
574
          resolved_user = curator_or_current_uid(user)
11✔
575
          message = "@#{resolved_user} Error publishing DOI. #{result.failure.status} / #{result.failure.reason_phrase}"
11✔
576
          WorkActivity.add_work_activity(id, message, user.id, activity_type: WorkActivity::DATACITE_ERROR)
11✔
577
        end
578
      elsif ark.blank? # we can not update the url anywhere
2✔
579
        Honeybadger.notify("Publishing for a DOI we do not own and no ARK is present: #{doi}")
1✔
580
      end
581
    end
582

583
    def doi_attribute_url
1✔
584
      "https://datacommons.princeton.edu/discovery/doi/#{doi}"
26✔
585
    end
586

587
    def doi_attribute_resource
1✔
588
      PDCMetadata::Resource.new_from_jsonb(metadata)
26✔
589
    end
590

591
    def doi_attribute_xml
1✔
592
      unencoded = doi_attribute_resource.to_xml
26✔
593
      Base64.encode64(unencoded)
26✔
594
    end
595

596
    def doi_attributes
1✔
597
      {
598
        "event" => "publish",
26✔
599
        "xml" => doi_attribute_xml,
600
        "url" => doi_attribute_url
601
      }
602
    end
603

604
    # This needs to be called #before_save
605
    # This ensures that new ActiveStorage::Attachment objects are persisted with custom keys (which are generated from the file name and DOI)
606
    # @param new_attachments [Array<ActiveStorage::Attachment>]
607
    def save_new_attachments(new_attachments:)
1✔
608
      new_attachments.each do |attachment|
28✔
609
        # There are cases (race conditions?) where the ActiveStorage::Blob objects are not persisted
610
        next if attachment.frozen?
28✔
611

612
        # This ensures that the custom key for the ActiveStorage::Attachment and ActiveStorage::Blob objects are generated
613
        generated_key = generate_attachment_key(attachment)
24✔
614
        attachment.blob.key = generated_key
24✔
615
        attachment.blob.save
24✔
616

617
        attachment.save
24✔
618
      end
619
    end
620

621
    # Request S3 Bucket Objects associated with this Work
622
    # @return [Array<S3File>]
623
    def s3_resources
1✔
624
      data_profile = s3_query_service.data_profile
101✔
625
      data_profile.fetch(:objects, [])
101✔
626
    end
627
    alias pre_curation_s3_resources s3_resources
1✔
628

629
    def s3_object_persisted?(s3_file)
1✔
630
      uploads_keys = uploads.map(&:key)
×
631
      uploads_keys.include?(s3_file.key)
×
632
    end
633

634
    def add_pre_curation_s3_object(s3_file)
1✔
635
      return if s3_object_persisted?(s3_file)
×
636

637
      persisted = s3_file.to_blob
×
638
      pre_curation_uploads.attach(persisted)
×
639
    end
640

641
    def publish_precurated_files
1✔
642
      # An error is raised if there are no files to be moved
643
      raise(StandardError, "Attempting to publish a Work without attached uploads for #{s3_object_key}") if pre_curation_uploads_fast.empty? && post_curation_uploads.empty?
22✔
644

645
      # We need to explicitly access to post-curation services here.
646
      # Lets explicitly create it so the state of the work does not have any impact.
647
      s3_post_curation_query_service = S3QueryService.new(self, false)
22✔
648

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

652
      # Copy the pre-curation S3 Objects to the post-curation S3 Bucket...
653
      transferred_file_errors = s3_query_service.publish_files
22✔
654

655
      # ...check that the files are indeed now in the post-curation bucket...
656
      if transferred_file_errors.count > 0
22✔
657
        raise(StandardError, "Failed to validate the uploaded S3 Object #{transferred_file_errors.map(&:key).join(', ')}")
×
658
      end
659
    end
660
end
661
# 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