Class: S3File

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper
Defined in:
app/models/s3_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename:, last_modified:, size:, checksum:, work:, filename_display: nil, url: nil) ⇒ S3File

Returns a new instance of S3File.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/s3_file.rb', line 21

def initialize(filename:, last_modified:, size:, checksum:, work:, filename_display: nil, url: nil)
  @safe_id = filename_as_id(filename)
  @filename = filename
  @filename_display = filename_display || filename_short(work, filename)
  @last_modified = last_modified
  @last_modified_display = last_modified.in_time_zone.strftime("%m/%d/%Y %I:%M %p") # mm/dd/YYYY HH:MM AM
  @size = size
  @display_size = number_to_human_size(size)
  @checksum = checksum.delete('"')
  @url = url || work_download_path(work, filename:)
  @work = work
  @is_folder = filename.ends_with?("/")
end

Instance Attribute Details

#checksumObject

Returns the value of attribute checksum.



6
7
8
# File 'app/models/s3_file.rb', line 6

def checksum
  @checksum
end

#display_sizeObject

Returns the value of attribute display_size.



6
7
8
# File 'app/models/s3_file.rb', line 6

def display_size
  @display_size
end

#filenameObject Also known as: key, id

Returns the value of attribute filename.



6
7
8
# File 'app/models/s3_file.rb', line 6

def filename
  @filename
end

#filename_displayObject

Returns the value of attribute filename_display.



6
7
8
# File 'app/models/s3_file.rb', line 6

def filename_display
  @filename_display
end

#is_folderObject

Returns the value of attribute is_folder.



6
7
8
# File 'app/models/s3_file.rb', line 6

def is_folder
  @is_folder
end

#last_modifiedObject

Returns the value of attribute last_modified.



6
7
8
# File 'app/models/s3_file.rb', line 6

def last_modified
  @last_modified
end

#last_modified_displayObject

Returns the value of attribute last_modified_display.



6
7
8
# File 'app/models/s3_file.rb', line 6

def last_modified_display
  @last_modified_display
end

#safe_idObject

Returns the value of attribute safe_id.



6
7
8
# File 'app/models/s3_file.rb', line 6

def safe_id
  @safe_id
end

#sizeObject

Returns the value of attribute size.



6
7
8
# File 'app/models/s3_file.rb', line 6

def size
  @size
end

#urlObject

Returns the value of attribute url.



6
7
8
# File 'app/models/s3_file.rb', line 6

def url
  @url
end

Class Method Details

.from_json(json_string) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'app/models/s3_file.rb', line 10

def self.from_json(json_string)
  json = if json_string.is_a? String
           JSON.parse(json_string)
         else
           json_string
         end

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

Instance Method Details

#byte_sizeObject



39
40
41
# File 'app/models/s3_file.rb', line 39

def byte_size
  size
end

#create_snapshotUploadSnapshot

Create a new snapshot of the current upload

Returns:



64
65
66
67
68
69
70
# File 'app/models/s3_file.rb', line 64

def create_snapshot
  created = UploadSnapshot.create(url:, work: @work, files: [{ filename:, checksum: }])

  created.upload = self
  created.save
  created.reload
end

#created_atObject



35
36
37
# File 'app/models/s3_file.rb', line 35

def created_at
  last_modified
end

#directory?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/models/s3_file.rb', line 43

def directory?
  size == 0
end

#empty?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'app/models/s3_file.rb', line 47

def empty?
  size == 0
end

#globus_urlObject



51
52
53
54
# File 'app/models/s3_file.rb', line 51

def globus_url
  encoded_filename = filename.split("/").map { |name| ERB::Util.url_encode(name) }.join("/")
  File.join(Rails.configuration.globus["post_curation_base_url"], encoded_filename)
end

#s3_clientObject



58
59
60
# File 'app/models/s3_file.rb', line 58

def s3_client
  s3_query_service.client
end

#snapshotsUploadSnapshot

Returns:



73
74
75
76
77
78
# File 'app/models/s3_file.rb', line 73

def snapshots
  persisted = UploadSnapshot.where(key:, url:, work: @work)
  return [] if persisted.blank?

  persisted
end

#to_jsonObject



80
81
82
83
84
85
# File 'app/models/s3_file.rb', line 80

def to_json
  {
    filename:, last_modified:, size:,
    checksum:, work_id: @work.id, filename_display:, url:
  }.to_json
end