Class: SdrClient::Deposit::UploadFiles

Inherits:
Object
  • Object
show all
Defined in:
lib/sdr_client/deposit/upload_files.rb

Overview

The file uploading part of a deposit

Constant Summary collapse

BLOB_PATH =
'/v1/direct_uploads'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_metadata:, filepath_map:, logger:, connection:) ⇒ UploadFiles

Returns a new instance of UploadFiles.

Parameters:

  • file_metadata (Hash<String,Files::DirectUploadRequest>)

    map of relative filepaths to file metadata

  • filepath_map (Hash<String,String>)

    map of relative filepaths to absolute filepaths

  • logger (Logger)

    the logger to use

  • connection (Connection)


23
24
25
26
27
28
# File 'lib/sdr_client/deposit/upload_files.rb', line 23

def initialize(file_metadata:, filepath_map:, logger:, connection:)
  @file_metadata = 
  @filepath_map = filepath_map
  @logger = logger
  @connection = connection
end

Class Method Details

.upload(file_metadata:, filepath_map:, logger:, connection:) ⇒ Object

Parameters:

  • file_metadata (Hash<String,Files::DirectUploadRequest>)

    map of relative filepaths to file metadata

  • filepath_map (Hash<String,String>)

    map of relative filepaths to absolute filepaths

  • logger (Logger)

    the logger to use

  • connection (Connection)


15
16
17
# File 'lib/sdr_client/deposit/upload_files.rb', line 15

def self.upload(file_metadata:, filepath_map:, logger:, connection:)
  new(file_metadata: , filepath_map: filepath_map, logger: logger, connection: connection).run
end

Instance Method Details

#runArray<Files::DirectUploadResponse>

Returns the responses from the server for the uploads.

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sdr_client/deposit/upload_files.rb', line 31

def run
  .map do |filepath, |
    direct_upload(.to_json).tap do |response|
      # ActiveStorage modifies the filename provided in response, so setting here with the relative filename
      response.filename = filepath
      upload_file(filename: filepath,
                  url: response.direct_upload.fetch('url'),
                  content_type: response.content_type,
                  content_length: response.byte_size)
      logger.info("Upload of #{filepath} complete")
    end
  end
end