class MyLocalPutio::Downloader

Attributes

configuration[R]
logger[R]

Public Class Methods

new(configuration) click to toggle source
# File lib/my-local-putio/downloader.rb, line 5
def initialize(configuration)
  @configuration = configuration
  @logger = configuration.logger
end

Public Instance Methods

download(url, path) click to toggle source
# File lib/my-local-putio/downloader.rb, line 10
def download(url, path)
  logger.debug "Download file from #{url}"
  logger.log "Downloading: #{path}"
  command = download_command(url, path)
  fetch_result = system(*command)
  raise "Unable to download #{path}" unless fetch_result
  logger.log "Finishing the download..."
  move_downloaded_file!(path)
end

Private Instance Methods

download_command(url, path) click to toggle source
# File lib/my-local-putio/downloader.rb, line 39
def download_command(url, path)
  temp_destination = File.join(configuration.temp_destination, path)

  command = [
    "curl", "--create-dirs", "-L", "--retry", "5", "-S", "-C", "-", "-o", temp_destination, url.to_s
  ]

  command.push("--progress-bar") unless configuration.detailed_progress

  command.push("--silent") if logger.silent?

  if configuration.socks_enabled?
    command.push("--socks5-hostname", "#{configuration.socks_host}:#{configuration.socks_port}")
  end

  return command
end
move_downloaded_file!(path) click to toggle source
# File lib/my-local-putio/downloader.rb, line 22
def move_downloaded_file!(path)
  temp_file = File.join(configuration.temp_destination, path)
  local_file = File.join(configuration.local_destination, path)

  # Create destination folder
  FileUtils.mkdir_p(File.dirname(local_file))

  logger.debug "Moving file from #{temp_file} to #{local_file}"
  FileUtils.mv(temp_file, local_file)

  # Delete temporary file
  FileUtils.rm_rf(temp_file)

  # Delete temporary folder if empty
  Dir.rmdir(File.dirname(temp_file)) rescue Errno::ENOTEMPTY
end