Class: S3File
- Inherits:
-
Object
- Object
- S3File
- Includes:
- ActionView::Helpers::NumberHelper
- Defined in:
- app/models/s3_file.rb
Instance Attribute Summary collapse
-
#checksum ⇒ Object
Returns the value of attribute checksum.
-
#display_size ⇒ Object
Returns the value of attribute display_size.
-
#filename ⇒ Object
(also: #key, #id)
Returns the value of attribute filename.
-
#filename_display ⇒ Object
Returns the value of attribute filename_display.
-
#is_folder ⇒ Object
Returns the value of attribute is_folder.
-
#last_modified ⇒ Object
Returns the value of attribute last_modified.
-
#last_modified_display ⇒ Object
Returns the value of attribute last_modified_display.
-
#safe_id ⇒ Object
Returns the value of attribute safe_id.
-
#size ⇒ Object
Returns the value of attribute size.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
- #byte_size ⇒ Object
-
#create_snapshot ⇒ UploadSnapshot
Create a new snapshot of the current upload.
- #created_at ⇒ Object
- #directory? ⇒ Boolean
- #empty? ⇒ Boolean
- #globus_url ⇒ Object
-
#initialize(filename:, last_modified:, size:, checksum:, work:, filename_display: nil, url: nil) ⇒ S3File
constructor
A new instance of S3File.
- #s3_client ⇒ Object
- #snapshots ⇒ UploadSnapshot
- #to_json ⇒ Object
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
#checksum ⇒ Object
Returns the value of attribute checksum.
6 7 8 |
# File 'app/models/s3_file.rb', line 6 def checksum @checksum end |
#display_size ⇒ Object
Returns the value of attribute display_size.
6 7 8 |
# File 'app/models/s3_file.rb', line 6 def display_size @display_size end |
#filename ⇒ Object 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_display ⇒ Object
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_folder ⇒ Object
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_modified ⇒ Object
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_display ⇒ Object
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_id ⇒ Object
Returns the value of attribute safe_id.
6 7 8 |
# File 'app/models/s3_file.rb', line 6 def safe_id @safe_id end |
#size ⇒ Object
Returns the value of attribute size.
6 7 8 |
# File 'app/models/s3_file.rb', line 6 def size @size end |
#url ⇒ Object
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_size ⇒ Object
39 40 41 |
# File 'app/models/s3_file.rb', line 39 def byte_size size end |
#create_snapshot ⇒ UploadSnapshot
Create a new snapshot of the current upload
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_at ⇒ Object
35 36 37 |
# File 'app/models/s3_file.rb', line 35 def created_at last_modified end |
#directory? ⇒ Boolean
43 44 45 |
# File 'app/models/s3_file.rb', line 43 def directory? size == 0 end |
#empty? ⇒ Boolean
47 48 49 |
# File 'app/models/s3_file.rb', line 47 def empty? size == 0 end |
#globus_url ⇒ Object
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_client ⇒ Object
58 59 60 |
# File 'app/models/s3_file.rb', line 58 def s3_client s3_query_service.client end |
#snapshots ⇒ UploadSnapshot
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_json ⇒ Object
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 |