Class: Factorix::Transfer::Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/factorix/transfer/uploader.rb,
sig/factorix/transfer/uploader.rbs

Overview

File uploader with automatic retry

Uploads files to given URLs using HTTP multipart/form-data. Uses Transfer::HTTP for the actual upload with event-driven progress notification.

Defined Under Namespace

Classes: CombinedIO, ProgressIO

Constant Summary collapse

URI =

Returns:

  • (:HTTPS, String url,)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Uploader

Returns a new instance of Uploader.

Parameters:

  • deps (Object)


11
# File 'sig/factorix/transfer/uploader.rbs', line 11

def initialize: (**untyped deps) -> void

Instance Attribute Details

#client ⇒ HTTP::Client (readonly)

Returns the value of attribute client.

Returns:



9
10
11
# File 'sig/factorix/transfer/uploader.rbs', line 9

def client
  @client
end

#logger ⇒ Dry::Logger::Dispatcher (readonly)

Returns the value of attribute logger.



8
9
10
# File 'sig/factorix/transfer/uploader.rbs', line 8

def logger
  @logger
end

Instance Method Details

#upload(url, file_path, field_name: "file", fields: {}, content_type: :auto) ⇒ void

This method returns an undefined value.

Upload a file to the given URL with optional form fields

Parameters:

  • url (URI::HTTPS, String) —

    URL to upload to (HTTPS only)

  • file_path (Pathname) —

    path to the file to upload

  • field_name (String) (defaults to: "file") —

    form field name for the file (default: "file")

  • fields (Hash<String, String>) (defaults to: {}) —

    additional form fields (e.g., metadata)

  • content_type (:auto, String) (defaults to: :auto) —

    MIME type (:auto detects from extension)

Raises:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/factorix/transfer/uploader.rb', line 49

def upload(url, file_path, field_name: "file", fields: {}, content_type: :auto)
  url = URI(url) if url.is_a?(String)
  unless url.is_a?(URI::HTTPS)
    logger.error("Invalid URL: must be HTTPS", url: url.to_s)
    raise URLError, "URL must be HTTPS"
  end

  unless file_path.exist?
    logger.error("File does not exist", path: file_path.to_s)
    raise ConfigurationError, "File does not exist: #{file_path}"
  end

  resolved_content_type = content_type == :auto ? detect_content_type(file_path) : content_type
  upload_file_with_progress(url, file_path, field_name, fields, resolved_content_type)
end