Class: Scholarsphere::S3::UploadedFile
- Inherits:
-
Object
- Object
- Scholarsphere::S3::UploadedFile
- Defined in:
- lib/scholarsphere/s3/uploaded_file.rb
Overview
Represents a file on the client's file system to be uploaded into Scholarsphere. The object is constructed using a pathname for the file, and an optional checksum. Once initialized, the object is used by the client to make additional calls to the application, including uploading the file into S3, and adding the resulting uploaded file to a work in Scholarsphere.
Examples
The most common use case would be:
pathname = Pathname.new('path/to/your/file')
uploaded_file = UploadedFile.new(pathname)
If the file is large, then calculating a checksum could be time-intensive. Providing one can avoid that, or, if you already have a checksum from a trusted source, you can pass that along for the client to use:
uploaded_file = UploadedFile.new(pathname, checksum: '[md5 checksum hash]')
Checksum Verification
Checksums are always used. If you don't provide one, the client will calculate one for you and use it when uploading the file, such as with S3::Uploader. AWS uses the provided checksum to verify the file's integrity, and if that check fails, an exception is raised. This avoids the prospect that the file could be corrupted during its transfer from the local client's filesystem into S3.
Instance Attribute Summary collapse
-
#source ⇒ Pathname
readonly
The file to be uploaded on the client's filesystem.
Instance Method Summary collapse
-
#content_md5 ⇒ String
The md5 checksum encoded in base64.
-
#initialize(source:, checksum: nil) ⇒ UploadedFile
constructor
A new instance of UploadedFile.
-
#presigned_url ⇒ String
Pre-signed url used to upload the file into Scholarsphere's S3 instance.
-
#to_param ⇒ Hash
Parameters required to add the file to a work in Scholarsphere.
Constructor Details
#initialize(source:, checksum: nil) ⇒ UploadedFile
Returns a new instance of UploadedFile.
37 38 39 40 |
# File 'lib/scholarsphere/s3/uploaded_file.rb', line 37 def initialize(source:, checksum: nil) @source = Pathname.new(source) @checksum = checksum end |
Instance Attribute Details
#source ⇒ Pathname (readonly)
Returns The file to be uploaded on the client's filesystem.
33 34 35 |
# File 'lib/scholarsphere/s3/uploaded_file.rb', line 33 def source @source end |
Instance Method Details
#content_md5 ⇒ String
When sending the checksum to verify the file's integrity, Amazon requires that the value be base64 encoded.
Returns The md5 checksum encoded in base64. If you provided a checksum at initialization, that one will be encoded, if not, a checksum will be calculated and then encoded.
57 58 59 60 61 62 63 |
# File 'lib/scholarsphere/s3/uploaded_file.rb', line 57 def content_md5 @content_md5 ||= if checksum Base64.encode64([checksum].pack('H*')).strip else Digest::MD5.base64digest(source.read) end end |
#presigned_url ⇒ String
Returns Pre-signed url used to upload the file into Scholarsphere's S3 instance.
66 67 68 |
# File 'lib/scholarsphere/s3/uploaded_file.rb', line 66 def presigned_url upload.url end |
#to_param ⇒ Hash
Returns Parameters required to add the file to a work in Scholarsphere.
43 44 45 46 47 48 49 |
# File 'lib/scholarsphere/s3/uploaded_file.rb', line 43 def to_param { id: upload.id, storage: upload.prefix, metadata: } end |