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

pulibrary / pdc_describe / 9dbcf7a4-1c56-4510-9614-74ad5a22cff6

31 Jul 2024 02:46PM UTC coverage: 1.08% (-95.1%) from 96.17%
9dbcf7a4-1c56-4510-9614-74ad5a22cff6

push

circleci

jrgriffiniii
wip

52 of 4814 relevant lines covered (1.08%)

0.01 hits per line

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

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

6
  alias_attribute :existing_files, :files
×
7

8
  def snapshot_deletions(work_changes, s3_filenames)
×
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)
×
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
×
30
    @upload ||= uploads.find { |s3_file| filenames.include?(s3_file.filename) }
×
31
  end
×
32

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

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

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

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

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

53
  def store_files(s3_files)
×
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:)
×
58
    find_by("work_id = ? AND files @> ?", work_id, JSON.dump([{ filename: }]))
×
59
  end
×
60

61
  private
×
62

63
    def uploads
×
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)
×
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