Class: NeocitiesRed::Services::File::Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/neocities_red/services/file/uploader.rb

Overview

Uploads a single file to the Neocities site.

Handles display feedback for the upload lifecycle: progress, success, already-exists, and directory-skip scenarios.

Examples:

uploader = NeocitiesRed::Services::File::Uploader.new(client, "index.html", display: display)
uploader.upload

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(client, filepath, remote_path = nil, display: NeocitiesRed::CliDisplay.new) ⇒ Uploader

Returns a new instance of Uploader.

Parameters:

  • client (NeocitiesRed::Client)

    authenticated API client

  • filepath (String)

    local file path to upload

  • remote_path (String, nil) (defaults to: nil)

    remote destination path; defaults to the file's basename

  • display (NeocitiesRed::CliDisplay) (defaults to: NeocitiesRed::CliDisplay.new)

    output helper



24
25
26
27
28
29
# File 'lib/neocities_red/services/file/uploader.rb', line 24

def initialize(client, filepath, remote_path = nil, display: NeocitiesRed::CliDisplay.new)
  @client = client
  @filepath = filepath
  @remote_path = remote_path
  @display = display
end

Instance Method Details

#uploadHash

Uploads the file to the remote site.

Skips directories with a display message. Computes the SHA1 hash and skips the upload if the remote file is identical.

Returns:

  • (Hash)

    API response with :result key

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/neocities_red/services/file/uploader.rb', line 38

def upload
  path = Pathname.new(@filepath)

  raise NeocitiesRed::FileNotFoundError, "#{path} does not exist locally." unless path.exist?

  if path.directory?
    @display.display_skip_directory(path)
    return
  end

  @display.display_upload_progress(path, @remote_path)

  response = @client.upload(path, @remote_path)

  if response[:result] == "success"
    @display.display_upload_success
  elsif response[:result] == "error" && response[:error_type] == "file_exists"
    @display.display_upload_exists
  else
    @display.display_response(response)
  end

  response
end