Class: Factorix::Transfer::Uploader

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

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

Instance Method Summary collapse

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