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

pulibrary / pdc_describe / aa4d17ae-5b88-4b80-9b41-f98c7bc03ef4

pending completion
aa4d17ae-5b88-4b80-9b41-f98c7bc03ef4

Pull #1060

circleci

jrgriffiniii
wip
Pull Request #1060: [wip] Integrating Support for Upload Snapshots

152 of 152 new or added lines in 7 files covered. (100.0%)

2016 of 2058 relevant lines covered (97.96%)

171.45 hits per line

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

93.65
/app/models/s3_file.rb
1
# frozen_string_literal: true
2
class S3File
1✔
3
  include Rails.application.routes.url_helpers
1✔
4

5
  attr_accessor :safe_id, :filename, :last_modified, :size, :checksum, :url, :filename_display, :last_modified_display
1✔
6
  alias key filename
1✔
7
  alias id filename
1✔
8

9
  def initialize(filename:, last_modified:, size:, checksum:, work:)
1✔
10
    @safe_id = filename_as_id(filename)
172✔
11
    @filename = filename
172✔
12
    @filename_display = filename_short(work, filename)
172✔
13
    @last_modified = last_modified
172✔
14
    @last_modified_display = last_modified.in_time_zone.strftime("%m/%d/%Y %I:%M %p") # mm/dd/YYYY HH:MM AM
172✔
15
    @size = size
172✔
16
    @checksum = checksum.delete('"')
172✔
17
    @url = work_download_path(work, filename: filename)
172✔
18
    @work = work
172✔
19
  end
20

21
  def created_at
1✔
22
    last_modified
19✔
23
  end
24

25
  def byte_size
1✔
26
    size
×
27
  end
28

29
  def globus_url
1✔
30
    encoded_filename = filename.split("/").map { |name| ERB::Util.url_encode(name) }.join("/")
217✔
31
    File.join(Rails.configuration.globus["post_curation_base_url"], encoded_filename)
44✔
32
  end
33

34
  def create_blob(key: nil)
1✔
35
    key ||= filename
7✔
36
    params = { filename: filename, content_type: "", byte_size: size, checksum: checksum }
7✔
37

38
    blob = ActiveStorage::Blob.create_before_direct_upload!(**params)
7✔
39
    blob.key = key
7✔
40
    blob
7✔
41
  end
42

43
  def to_blob
1✔
44
    existing_blob = ActiveStorage::Blob.find_by(key: filename)
3✔
45

46
    if existing_blob.present?
3✔
47
      Rails.logger.warn("There is a blob existing for #{filename}, which we are not expecting!  It will be reattached #{existing_blob.inspect}")
1✔
48
      return existing_blob
1✔
49
    end
50

51
    create_blob
2✔
52
  end
53

54
  delegate :s3_query_service, to: :@work
1✔
55
  delegate :bucket_name, to: :s3_query_service
1✔
56
  def s3_client
1✔
57
    s3_query_service.client
1✔
58
  end
59

60
  # Create a new snapshot of the current upload
61
  # @return [UploadSnapshot]
62
  def create_snapshot
1✔
63
    created = UploadSnapshot.create(url: url, filename: filename, work: @work)
4✔
64

65
    persisted_blob = create_blob(key: created.key)
4✔
66
    created.bitstream.attach(persisted_blob)
4✔
67

68
    copy_source = "/#{bucket_name}/#{key}"
1✔
69
    params = {
70
      copy_source: copy_source,
1✔
71
      bucket: bucket_name,
72
      key: created.key
73
    }
74
    Rails.logger.info("Copying #{copy_source} to #{created.key}")
1✔
75
    s3_client.copy_object(params)
1✔
76

77
    persisted_blob = create_blob(key: created.key)
1✔
78
    created.bitstream.attach(persisted_blob)
1✔
79

80
    created.upload = self
1✔
81
    created.save
1✔
82
    created.reload
1✔
83
  end
84

85
  # @return [UploadSnapshot]
86
  def snapshots
1✔
87
    persisted = UploadSnapshot.where(key: key, url: url, work: @work)
×
88
    return [] if persisted.blank?
×
89

90
    persisted
×
91
  end
92

93
  private
1✔
94

95
    # Filename without the DOI/work-id/ in the path (but we preserve other path information if there is any)
96
    def filename_short(work, filename)
1✔
97
      prefix = "#{work.doi}/#{work.id}/"
172✔
98
      if filename.start_with?(prefix)
172✔
99
        filename[prefix.length..]
95✔
100
      else
101
        filename
77✔
102
      end
103
    end
104

105
    def filename_as_id(filename)
1✔
106
      # The full filename and path but only with alphanumeric characters
107
      # everything else becomes as dash.
108
      filename.gsub(/[^A-Za-z\d]/, "-")
172✔
109
    end
110
end
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