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

pulibrary / pdc_describe / 7ae96b6b-0a64-4479-9fa1-8ba6526e2c87

20 Mar 2024 12:42PM UTC coverage: 30.068% (-66.2%) from 96.266%
7ae96b6b-0a64-4479-9fa1-8ba6526e2c87

Pull #1701

circleci

leefaisonr
makes it so that links open in new window
Pull Request #1701: Update language on submission form

1019 of 3389 relevant lines covered (30.07%)

0.4 hits per line

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

35.29
/app/models/s3_file.rb
1
# frozen_string_literal: true
2
class S3File
1✔
3
  include Rails.application.routes.url_helpers
1✔
4
  include ActionView::Helpers::NumberHelper
1✔
5

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

10
  def self.from_json(json_string)
1✔
11
    json = if json_string.is_a? String
×
12
             JSON.parse(json_string)
×
13
           else
14
             json_string
×
15
           end
16

17
    new(filename: json["filename"], last_modified: DateTime.parse(json["last_modified"]), size: json["size"],
×
18
        checksum: json["checksum"], work: Work.find(json["work_id"]), filename_display: json["filename_display"], url: json["url"])
19
  end
20

21
  def initialize(filename:, last_modified:, size:, checksum:, work:, filename_display: nil, url: nil)
1✔
22
    @safe_id = filename_as_id(filename)
×
23
    @filename = filename
×
24
    @filename_display = filename_display || filename_short(work, filename)
×
25
    @last_modified = last_modified
×
26
    @last_modified_display = last_modified.in_time_zone.strftime("%m/%d/%Y %I:%M %p") # mm/dd/YYYY HH:MM AM
×
27
    @size = size
×
28
    @display_size = number_to_human_size(size)
×
29
    @checksum = checksum.delete('"')
×
30
    @url = url || work_download_path(work, filename:)
×
31
    @work = work
×
32
  end
33

34
  def created_at
1✔
35
    last_modified
×
36
  end
37

38
  def byte_size
1✔
39
    size
×
40
  end
41

42
  def directory?
1✔
43
    size == 0
×
44
  end
45

46
  def empty?
1✔
47
    size == 0
×
48
  end
49

50
  def globus_url
1✔
51
    encoded_filename = filename.split("/").map { |name| ERB::Util.url_encode(name) }.join("/")
×
52
    File.join(Rails.configuration.globus["post_curation_base_url"], encoded_filename)
×
53
  end
54

55
  def create_blob(key: nil)
1✔
56
    key ||= filename
×
57
    params = { filename:, content_type: "", byte_size: size, checksum: }
×
58

59
    blob = ActiveStorage::Blob.create_before_direct_upload!(**params)
×
60
    blob.key = key
×
61
    blob
×
62
  end
63

64
  def to_blob
1✔
65
    existing_blob = ActiveStorage::Blob.find_by(key: filename)
×
66

67
    if existing_blob.present?
×
68
      Rails.logger.warn("There is a blob existing for #{filename}, which we are not expecting!  It will be reattached #{existing_blob.inspect}")
×
69
      return existing_blob
×
70
    end
71

72
    create_blob
×
73
  end
74

75
  delegate :s3_query_service, to: :@work
1✔
76
  delegate :bucket_name, to: :s3_query_service
1✔
77
  def s3_client
1✔
78
    s3_query_service.client
×
79
  end
80

81
  # Create a new snapshot of the current upload
82
  # @return [UploadSnapshot]
83
  def create_snapshot
1✔
84
    created = UploadSnapshot.create(url:, work: @work, files: [{ filename:, checksum: }])
×
85

86
    created.upload = self
×
87
    created.save
×
88
    created.reload
×
89
  end
90

91
  # @return [UploadSnapshot]
92
  def snapshots
1✔
93
    persisted = UploadSnapshot.where(key:, url:, work: @work)
×
94
    return [] if persisted.blank?
×
95

96
    persisted
×
97
  end
98

99
  def to_json
1✔
100
    {
101
      filename:, last_modified:, size:,
×
102
      checksum:, work_id: @work.id, filename_display:, url:
103
    }.to_json
104
  end
105

106
  private
1✔
107

108
    # Filename without the DOI/work-id/ in the path (but we preserve other path information if there is any)
109
    def filename_short(work, filename)
1✔
110
      prefix = "#{work.doi}/#{work.id}/"
×
111
      if filename.start_with?(prefix)
×
112
        filename[prefix.length..]
×
113
      else
114
        filename
×
115
      end
116
    end
117

118
    def filename_as_id(filename)
1✔
119
      # The full filename and path but only with alphanumeric characters
120
      # everything else becomes as dash.
121
      filename.gsub(/[^A-Za-z\d]/, "-")
×
122
    end
123
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