Class: BackgroundUploadSnapshot

Inherits:
UploadSnapshot show all
Defined in:
app/models/background_upload_snapshot.rb

Direct Known Subclasses

ApprovedUploadSnapshot

Instance Attribute Summary

Attributes inherited from UploadSnapshot

#upload

Instance Method Summary collapse

Methods inherited from UploadSnapshot

checksum_compare, #filenames, find_by_filename, #snapshot_deletions, #snapshot_modifications, #uri

Instance Method Details

#existing_filesObject



32
33
34
# File 'app/models/background_upload_snapshot.rb', line 32

def existing_files
  super.select { |file| file["upload_status"].nil? || file["upload_status"] == "complete" }
end

#finalize_uploadObject

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/background_upload_snapshot.rb', line 40

def finalize_upload
  new_files.each do |file|
    work.track_change(:added, file["filename"])
  end

  raise(ArgumentError, "Upload failed with empty files.") if new_files.empty?
  first_new_file = new_files.first

  user_id = first_new_file["user_id"]
  raise(ArgumentError, "Failed to resolve the user ID from #{first_new_file['filename']}") if user_id.nil?

  work.log_file_changes(user_id)
end

#mark_complete(filename, checksum) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/background_upload_snapshot.rb', line 15

def mark_complete(filename, checksum)
  index = files.index { |file| file["filename"] == prefix_filename(filename) }
  if index.nil?
    Rails.logger.error("Uploaded a file that was not part of the orginal Upload: #{id} for work #{work_id}: #{filename}")
    Honeybadger.notify("Uploaded a file that was not part of the orginal Upload: #{id} for work #{work_id}: #{filename}")
  else
    files[index]["upload_status"] = "complete"
    files[index]["checksum"] = checksum
  end
  finalize_upload if upload_complete?
  save
end

#new_filesObject



36
37
38
# File 'app/models/background_upload_snapshot.rb', line 36

def new_files
  files.select { |file| file["snapshot_id"] == id }
end

#prefix_filename(filename) ⇒ Object



54
55
56
57
58
59
60
# File 'app/models/background_upload_snapshot.rb', line 54

def prefix_filename(filename)
  if filename.include?(work.prefix)
    filename
  else
    "#{work.prefix}#{filename}"
  end
end

#store_files(uploaded_files, pre_existing_files: [], current_user: nil) ⇒ Object

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
11
12
13
# File 'app/models/background_upload_snapshot.rb', line 3

def store_files(uploaded_files, pre_existing_files: [], current_user: nil)
  save # needed so I can point to this snapshot in the files to distinguish new files from existing ones froma past snapshot

  raise(ArgumentError, "Failed to resolve the user ID from #{self}") if current_user.nil?
  user_id = current_user&.id
  self.files = uploaded_files.map do |file|
    { "filename" => prefix_filename(file.original_filename),
      "upload_status" => "started", user_id:, snapshot_id: id }
  end
  files.concat pre_existing_files if pre_existing_files.present?
end

#upload_complete?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'app/models/background_upload_snapshot.rb', line 28

def upload_complete?
  files.select { |file| file.keys.include?("upload_status") }.map { |file| file["upload_status"] }.uniq == ["complete"]
end