• 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

39.53
/app/models/upload_snapshot.rb
1
# frozen_string_literal: true
2
class UploadSnapshot < ApplicationRecord
1✔
3
  belongs_to :work
1✔
4
  attr_writer :upload
1✔
5

6
  alias_attribute :existing_files, :files
1✔
7

8
  def snapshot_deletions(work_changes, s3_filenames)
1✔
9
    existing_files.each do |file|
×
10
      filename = file["filename"]
×
11
      unless s3_filenames.include?(filename)
×
12
        work_changes << { action: "removed", filename:, checksum: file["checksum"] }
×
13
      end
14
    end
15
  end
16

17
  def snapshot_modifications(work_changes, s3_files)
1✔
18
    # check for modifications
19
    s3_files.each do |s3_file|
×
20
      next if match?(s3_file)
×
21
      work_changes << if include?(s3_file)
×
22
                        { action: "replaced", filename: s3_file.filename, checksum: s3_file.checksum }
×
23
                      else
24
                        { action: "added", filename: s3_file.filename, checksum: s3_file.checksum }
×
25
                      end
26
    end
27
  end
28

29
  def upload
1✔
30
    @upload ||= uploads.find { |s3_file| filenames.include?(s3_file.filename) }
×
31
  end
32

33
  def uri
1✔
34
    URI.parse(url)
×
35
  end
36

37
  def filenames
1✔
38
    files.map { |file| file["filename"] }
×
39
  end
40

41
  def include?(s3_file)
1✔
42
    filenames.include?(s3_file.filename)
×
43
  end
44

45
  def index(s3_file)
1✔
46
    files.index { |file| file["filename"] == s3_file.filename && checksum_compare(file["checksum"], s3_file.checksum) }
×
47
  end
48

49
  def match?(s3_file)
1✔
50
    index(s3_file).present?
×
51
  end
52

53
  def store_files(s3_files)
1✔
54
    self.files = s3_files.map { |file| { "filename" => file.filename, "checksum" => file.checksum } }
×
55
  end
56

57
  def self.find_by_filename(work_id:, filename:)
1✔
58
    find_by("work_id = ? AND files @> ?", work_id, JSON.dump([{ filename: }]))
×
59
  end
60

61
  private
1✔
62

63
    def uploads
1✔
64
      work.uploads
×
65
    end
66

67
    # Compares two checksums. Accounts for the case in which one of them is
68
    # a plain MD5 value and the other has been encoded with base64.
69
    # See also
70
    #   https://ruby-doc.org/core-2.7.0/Array.html#method-i-pack
71
    #   https://ruby-doc.org/core-2.7.0/String.html#method-i-unpack
72
    def checksum_compare(checksum1, checksum2)
1✔
73
      if checksum1 == checksum2
×
74
        true
×
75
      elsif checksum1.nil? || checksum2.nil?
×
76
        false
×
77
      elsif checksum1.length < checksum2.length
×
78
        # Decode the first one and then compare
79
        checksum1.unpack("m0").first.unpack("H*").first == checksum2
×
80
      else
81
        # Decode the second one and then compare
82
        checksum1 == checksum2.unpack("m0").first.unpack("H*").first
×
83
      end
84
    rescue ArgumentError
85
      # One of the values was not properly encoded
86
      false
×
87
    end
88
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