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

pulibrary / pdc_describe / 9091a1ae-29be-458c-984a-339d213919c4

12 Dec 2024 07:41PM UTC coverage: 26.434% (-69.7%) from 96.113%
9091a1ae-29be-458c-984a-339d213919c4

Pull #2000

circleci

jrgriffiniii
Removing integration with ActiveStorage
Pull Request #2000: Bump actionpack from 7.2.1.1 to 7.2.2.1

945 of 3575 relevant lines covered (26.43%)

0.35 hits per line

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

38.1
/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
    s3_filenames_sorted = s3_filenames.sort
×
10
    existing_files.each do |file|
×
11
      filename = file["filename"]
×
12
      # Use Ruby's Binary Search functionality instead of a plain Ruby Array `.include?`
13
      # to detect missing values in the array because the binary search performs
14
      # much faster when the list of files is large. Notice that the binary search
15
      # requires that the list of files is sorted.
16
      # See https://ruby-doc.org/3.3.6/bsearch_rdoc.html
17
      if s3_filenames_sorted.bsearch { |s3_filename| filename <=> s3_filename }.nil?
×
18
        work_changes << { action: "removed", filename:, checksum: file["checksum"] }
×
19
      end
20
    end
21
  end
22

23
  def snapshot_modifications(work_changes, s3_files)
1✔
24
    # check for modifications
25
    s3_files.each do |s3_file|
×
26
      match = existing_files_sorted.bsearch { |file| s3_file.filename <=> file["filename"] }
×
27
      if match.nil?
×
28
        work_changes << { action: "added", filename: s3_file.filename, checksum: s3_file.checksum }
×
29
      elsif UploadSnapshot.checksum_compare(match["checksum"], s3_file.checksum) == false
×
30
        work_changes << { action: "replaced", filename: s3_file.filename, checksum: s3_file.checksum }
×
31
      end
32
    end
33
  end
34

35
  def upload
1✔
36
    @upload ||= uploads.find { |s3_file| filenames.include?(s3_file.filename) }
×
37
  end
38

39
  def uri
1✔
40
    URI.parse(url)
×
41
  end
42

43
  def filenames
1✔
44
    files.map { |file| file["filename"] }
×
45
  end
46

47
  def store_files(s3_files)
1✔
48
    self.files = s3_files.map { |file| { "filename" => file.filename, "checksum" => file.checksum } }
×
49
  end
50

51
  def self.find_by_filename(work_id:, filename:)
1✔
52
    find_by("work_id = ? AND files @> ?", work_id, JSON.dump([{ filename: }]))
×
53
  end
54

55
  class << self
1✔
56
    # Compares two checksums. Accounts for the case in which one of them is
57
    # a plain MD5 value and the other has been encoded with base64.
58
    # See also
59
    #   https://ruby-doc.org/core-2.7.0/Array.html#method-i-pack
60
    #   https://ruby-doc.org/core-2.7.0/String.html#method-i-unpack
61
    def checksum_compare(checksum1, checksum2)
1✔
62
      if checksum1 == checksum2
×
63
        true
×
64
      elsif checksum1.nil? || checksum2.nil?
×
65
        false
×
66
      elsif checksum1.length < checksum2.length
×
67
        # Decode the first one and then compare
68
        checksum1.unpack("m0").first.unpack("H*").first == checksum2
×
69
      else
70
        # Decode the second one and then compare
71
        checksum1 == checksum2.unpack("m0").first.unpack("H*").first
×
72
      end
73
    rescue ArgumentError
74
      # One of the values was not properly encoded
75
      false
×
76
    end
77
  end
78

79
  private
1✔
80

81
    def existing_files_sorted
1✔
82
      @existing_files_sorted ||= files.sort_by { |file| file["filename"] }
×
83
    end
84

85
    def uploads
1✔
86
      work.uploads
×
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