Class: SdrClient::Deposit::Files::DirectUploadRequest

Inherits:
Struct
  • Object
show all
Defined in:
lib/sdr_client/deposit/files/direct_upload_request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#byte_sizeObject

Returns the value of attribute byte_size

Returns:

  • (Object)

    the current value of byte_size



6
7
8
# File 'lib/sdr_client/deposit/files/direct_upload_request.rb', line 6

def byte_size
  @byte_size
end

#checksumObject

Returns the value of attribute checksum

Returns:

  • (Object)

    the current value of checksum



6
7
8
# File 'lib/sdr_client/deposit/files/direct_upload_request.rb', line 6

def checksum
  @checksum
end

#content_typeObject

Returns the value of attribute content_type

Returns:

  • (Object)

    the current value of content_type



6
7
8
# File 'lib/sdr_client/deposit/files/direct_upload_request.rb', line 6

def content_type
  @content_type
end

#filenameObject

Returns the value of attribute filename

Returns:

  • (Object)

    the current value of filename



6
7
8
# File 'lib/sdr_client/deposit/files/direct_upload_request.rb', line 6

def filename
  @filename
end

Class Method Details

.clean_and_translate_content_type(content_type) ⇒ Object

Invalid JSON files with a content type of application/json will trigger 400 errors in sdr-api since they are parsed and rejected (not clear why and what part of the stack is doing this). The work around is to change the content_type for any JSON files to a custom stand-in and specific to avoid the parsing, and then have this translated back to application/json after . upload is complete. There is a corresponding change in sdr-api to translate the content_type back before the Cocina is saved. See github.com/sul-dlss/happy-heron/issues/3075 for the original bug report See github.com/sul-dlss/sdr-api/pull/585 for the change in sdr-api



34
35
36
37
38
39
40
41
# File 'lib/sdr_client/deposit/files/direct_upload_request.rb', line 34

def self.clean_and_translate_content_type(content_type)
  return 'application/octet-stream' if content_type.blank?

  # ActiveStorage is expecting "application/x-stata-dta" not "application/x-stata-dta;version=14"
  content_type = content_type.split(';').first

  content_type == 'application/json' ? 'application/x-stanford-json' : content_type
end

.from_file(path, file_name:, content_type:) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/sdr_client/deposit/files/direct_upload_request.rb', line 7

def self.from_file(path, file_name:, content_type:)
  checksum = Digest::MD5.file(path).base64digest
  new(checksum: checksum,
      byte_size: ::File.size(path),
      content_type: clean_and_translate_content_type(content_type),
      filename: file_name)
end

Instance Method Details

#as_jsonObject



15
16
17
18
19
20
# File 'lib/sdr_client/deposit/files/direct_upload_request.rb', line 15

def as_json
  {
    blob: { filename: filename, byte_size: byte_size, checksum: checksum,
            content_type: self.class.clean_and_translate_content_type(content_type) }
  }
end

#to_json(*_args) ⇒ Object



22
23
24
# File 'lib/sdr_client/deposit/files/direct_upload_request.rb', line 22

def to_json(*_args)
  JSON.generate(as_json)
end