Class: NeocitiesRed::Services::File::FolderUploader

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

Overview

Uploads all files in a local directory to the remote site in parallel.

Uses a Common::WorkerPool to upload files concurrently up to MAX_THREADS threads. Each file is uploaded via Uploader.

Examples:

folder = NeocitiesRed::Services::File::FolderUploader.new(client, "./dist", "assets", display: display)
files = folder.files
folder.upload(files)

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(client, filepath, remote_path, display: NeocitiesRed::CliDisplay.new) ⇒ FolderUploader

Returns a new instance of FolderUploader.

Parameters:

  • client (NeocitiesRed::Client)

    authenticated API client

  • filepath (String)

    local directory path to upload

  • remote_path (String)

    remote destination directory

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

    output helper



31
32
33
34
35
36
# File 'lib/neocities_red/services/file/folder_uploader.rb', line 31

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

Instance Method Details

#filesArray<String>?

Recursively lists all files in the local directory.

Returns:

  • (Array<String>)

    relative file paths within the directory

  • (nil)

    if the path is a file (not a directory)

Raises:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/neocities_red/services/file/folder_uploader.rb', line 43

def files
  path = Pathname.new(::File.expand_path(@filepath))

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

  if path.file?
    @display.display_skip_file(path)
    return
  end

  Dir.chdir(path) do
    Dir.glob("**/*", ::File::FNM_DOTMATCH)
       .select { |f| ::File.file?(f) }
  end
end

#upload(files_list, threads = MAX_THREADS) ⇒ void

This method returns an undefined value.

Uploads all files in the list concurrently.

Parameters:

  • files_list (Array<String>)

    relative file paths to upload

  • threads (Integer) (defaults to: MAX_THREADS)

    maximum concurrent upload threads (default: MAX_THREADS)



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/neocities_red/services/file/folder_uploader.rb', line 64

def upload(files_list, threads = MAX_THREADS)
  base = ::File.expand_path(@filepath)

  worker_pool = Services::Common::WorkerPool.new(threads) do |file|
    local_path  = ::File.join(base, file)
    remote_path = ::File.join(@remote_path, file)

    Uploader.new(@client, local_path, remote_path, display: @display).upload
  end

  worker_pool.process(files_list)
end