Class: NeocitiesRed::Services::FolderUploader

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

Instance Method Summary collapse

Constructor Details

#initialize(client, filepath, remote_path) ⇒ FolderUploader

Returns a new instance of FolderUploader.



15
16
17
18
19
20
# File 'lib/neocities_red/services/folder_uploader.rb', line 15

def initialize(client, filepath, remote_path)
  @client = client
  @filepath = filepath
  @remote_path = remote_path
  @pastel = Pastel.new(eachline: "\n")
end

Instance Method Details

#filesObject

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/neocities_red/services/folder_uploader.rb', line 22

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

  raise FileIsNotExists, "#{path} does not exist locally." unless path.exist?

  if path.file?
    puts @pastel.bold("#{path} is not a directory, skipping")
    return
  end

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

#upload(files_list, threads = MAX_THREADS) ⇒ Object



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

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

  queue = Queue.new
  files_list.each { |file| queue << file }

  workers = Array.new(threads) do
    Thread.new do
      loop do
        begin
          file = queue.pop(true)
        rescue ThreadError
          break
        end

        local_path  = File.join(base, file)
        remote_path = File.join(@remote_path, file)

        FileUploader.new(@client, local_path, remote_path).upload
      end
    end
  end

  workers.each(&:join)
end