Class: SdrClient::RedesignedClient::DirectUploadRequest

Inherits:
Struct
  • Object
show all
Defined in:
lib/sdr_client/redesigned_client/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



7
8
9
# File 'lib/sdr_client/redesigned_client/direct_upload_request.rb', line 7

def byte_size
  @byte_size
end

#checksumObject

Returns the value of attribute checksum

Returns:

  • (Object)

    the current value of checksum



7
8
9
# File 'lib/sdr_client/redesigned_client/direct_upload_request.rb', line 7

def checksum
  @checksum
end

#content_typeObject

Returns the value of attribute content_type

Returns:

  • (Object)

    the current value of content_type



7
8
9
# File 'lib/sdr_client/redesigned_client/direct_upload_request.rb', line 7

def content_type
  @content_type
end

#filenameObject

Returns the value of attribute filename

Returns:

  • (Object)

    the current value of filename



7
8
9
# File 'lib/sdr_client/redesigned_client/direct_upload_request.rb', line 7

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



35
36
37
38
39
40
41
42
# File 'lib/sdr_client/redesigned_client/direct_upload_request.rb', line 35

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



8
9
10
11
12
13
14
# File 'lib/sdr_client/redesigned_client/direct_upload_request.rb', line 8

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

#to_hObject



16
17
18
19
20
21
# File 'lib/sdr_client/redesigned_client/direct_upload_request.rb', line 16

def to_h
  {
    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



23
24
25
# File 'lib/sdr_client/redesigned_client/direct_upload_request.rb', line 23

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