class MovieOrganizer::FileCopier

Attributes

filename[RW]
hostname[R]
remote_filename[R]
target_file[RW]
username[R]

Public Class Methods

new(filename, target_file) click to toggle source
# File lib/movie_organizer/file_copier.rb, line 10
def initialize(filename, target_file)
  @filename = filename
  @target_file = target_file
  @dry_run = MovieOrganizer.options[:dry_run]
  @verbose = MovieOrganizer.options[:verbose]
end

Public Instance Methods

copy!() click to toggle source
# File lib/movie_organizer/file_copier.rb, line 17
def copy!
  ssh? ? remote_copy : local_copy
end

Private Instance Methods

copy_file_to_remote() click to toggle source
# File lib/movie_organizer/file_copier.rb, line 89
def copy_file_to_remote
  Net::SCP.start(hostname, username) do |scp|
    scp.upload!(filename, remote_filename)
  end
  FileUtils.rm(filename, noop: @dry_run) unless MovieOrganizer.options[:copy]
rescue Net::SSH::ConnectionTimeout, Errno::EHOSTUNREACH, Errno::EHOSTDOWN
  Logger.instance.error("ConnectionTimeout: the host '#{hostname}' is unreachable.".red)
end
copy_file_to_remote_cmd() click to toggle source
# File lib/movie_organizer/file_copier.rb, line 77
def copy_file_to_remote_cmd
  "scp '#{filename}' '#{remote_filename}'"
end
create_remote_dir() click to toggle source
# File lib/movie_organizer/file_copier.rb, line 81
def create_remote_dir
  Net::SSH.start(hostname, username, timeout: 5) do |ssh|
    ssh.exec!(create_remote_dir_cmd)
  end
rescue Net::SSH::ConnectionTimeout, Errno::EHOSTUNREACH, Errno::EHOSTDOWN
  Logger.instance.error("ConnectionTimeout: the host '#{hostname}' is unreachable.".red)
end
create_remote_dir_cmd() click to toggle source
# File lib/movie_organizer/file_copier.rb, line 73
def create_remote_dir_cmd
  "mkdir -p \"#{target_dir}\""
end
do_remote_dry_run() click to toggle source
# File lib/movie_organizer/file_copier.rb, line 44
def do_remote_dry_run
  Logger.instance.info("Would remotely execute: [#{create_remote_dir_cmd}] on #{hostname}")
  Logger.instance.info("Would execute: [#{copy_file_to_remote_cmd}]")
end
local_copy() click to toggle source
# File lib/movie_organizer/file_copier.rb, line 23
def local_copy
  dir = File.dirname(target_file)
  FileUtils.mkdir_p(dir) unless File.exist?(dir)
  if File.exist?(target_file)
    Logger.instance.info("    already exists: [#{target_file.green.bold}]")
    return true
  end
  if MovieOrganizer.options[:copy]
    FileUtils.copy(filename, target_file, noop: @dry_run, verbose: @verbose)
  else
    FileUtils.move(filename, target_file, noop: @dry_run, verbose: @verbose)
  end
end
parse_target() click to toggle source
# File lib/movie_organizer/file_copier.rb, line 56
def parse_target
  return nil if @parse_target
  @parse_target = true
  temp ||= target_file.to_s.split('/')[2..99]
  md = temp.join('/').match(/([\w\-\.]+)@([^\/]+)(\/.+)$/)
  @username = md[1]
  @hostname = md[2]
  @remote_filename = md[3]
  if @username.nil? || @hostname.nil? || @remote_filename.nil?
    raise 'SSH path not formatted properly. Use [ssh://username@hostname/absolute/path]'
  end
end
remote_copy() click to toggle source
# File lib/movie_organizer/file_copier.rb, line 37
def remote_copy
  parse_target
  return do_remote_dry_run if @dry_run
  create_remote_dir
  copy_file_to_remote
end
ssh?() click to toggle source
# File lib/movie_organizer/file_copier.rb, line 69
def ssh?
  target_file.match?(/^ssh:/)
end
target_dir() click to toggle source
# File lib/movie_organizer/file_copier.rb, line 49
def target_dir
  @target_dir ||= begin
    parts = @remote_filename.split('/')
    parts[0..parts.length - 2].join('/').to_s
  end
end