Class: Scholarsphere::S3::UploadedFile

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(source:, checksum: nil) ⇒ UploadedFile

Returns a new instance of UploadedFile.

Parameters:

  • source (Pathname, File, IO, String)

    Object or path to the file

  • checksum (String) (defaults to: nil)

    Optional md5 checksum of the file



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

#sourcePathname (readonly)

Returns The file to be uploaded on the client's filesystem.

Returns:

  • (Pathname)

    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_md5String

Note:

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.

Examples:

The equivalent operation in bash would be:

openssl dgst -md5 -binary file.jpg | openssl enc -base64

Returns:

  • (String)

    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_urlString

Returns Pre-signed url used to upload the file into Scholarsphere's S3 instance.

Returns:

  • (String)

    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_paramHash

Returns Parameters required to add the file to a work in Scholarsphere.

Returns:

  • (Hash)

    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