Class: RestFtpDaemon::Remote::RemoteFTP

Inherits:
RemoteBase
  • Object
show all
Defined in:
lib/rest-ftp-daemon/remote/ftp.rb

Instance Attribute Summary collapse

Attributes inherited from RemoteBase

#job, #log_prefix

Instance Method Summary collapse

Methods inherited from RemoteBase

#initialize

Methods included from CommonHelpers

#dashboard_url, #exception_to_error, #format_bytes, #identifier, #underscore

Constructor Details

This class inherits a constructor from RestFtpDaemon::Remote::RemoteBase

Instance Attribute Details

#ftpObject (readonly)

Class options



10
11
12
# File 'lib/rest-ftp-daemon/remote/ftp.rb', line 10

def ftp
  @ftp
end

Instance Method Details

#chdir_or_create(thedir, mkdir = true) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rest-ftp-daemon/remote/ftp.rb', line 62

def chdir_or_create thedir, mkdir = true
  # Init, extract my parent name and my own name
  parent, current = split_path(thedir)
  log_debug "chdir_or_create mkdir[#{mkdir}] dir[#{thedir}] parent[#{parent}] current[#{current}]"

  # Access this directory
  begin
    @ftp.chdir "/#{thedir}"

  rescue Net::FTPPermError => _e

    # If not allowed to create path, that's over, we're stuck
    unless mkdir
      log_debug "  [#{thedir}] failed > no mkdir > over"
      return false
    end

    # Try to go into my parent directory
    log_debug "  [#{thedir}] failed > chdir_or_create [#{parent}]"
    chdir_or_create parent, mkdir

    # Now I was able to chdir into my parent, create the current directory
    log_debug "  [#{thedir}] failed > mkdir [#{current}]"
    mkdir current

    # Finally retry the chdir
    retry
  else
    return true
  end

end

#closeObject



124
125
126
127
128
129
130
# File 'lib/rest-ftp-daemon/remote/ftp.rb', line 124

def close
  # Close init
  super

  # Close FTP connexion and free up memory
  @ftp.close
end

#connectObject



28
29
30
31
32
33
34
# File 'lib/rest-ftp-daemon/remote/ftp.rb', line 28

def connect
  super

  # Connect remote server
  @ftp.connect @target.host, @target.port
  @ftp. @target.user, @target.password
end

#connected?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/rest-ftp-daemon/remote/ftp.rb', line 132

def connected?
  !@ftp.welcome.nil?
end

#mkdir(directory) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/rest-ftp-daemon/remote/ftp.rb', line 54

def mkdir directory
  log_debug "mkdir [#{directory}]"
  @ftp.mkdir directory

rescue StandardError => ex
  raise TargetPermissionError, ex.message
end

#prepareObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rest-ftp-daemon/remote/ftp.rb', line 12

def prepare
  super
  
  # Create FTP object
  if @ftpes
    prepare_ftpes
  else
    prepare_ftp
  end
  @ftp.passive = true
  @ftp.debug_mode = @debug

  # Announce object
  log_debug "prepare chunk_size:#{format_bytes(JOB_FTP_CHUNKMB, "B")}"
end

#remove!(target) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/rest-ftp-daemon/remote/ftp.rb', line 46

def remove! target
  @ftp.delete target.path_fs
rescue Net::FTPPermError
  log_debug "remove! [#{target.name}] not found"
else
  log_debug "remove! [#{target.name}] removed"
end

#size_if_exists(target) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/rest-ftp-daemon/remote/ftp.rb', line 36

def size_if_exists target
  log_debug "size_if_exists [#{target.name}]"
  size = @ftp.size target.path_fs

rescue Net::FTPPermError
  return false
else
  return size
end

#upload(source, target, use_temp_name = false, &callback) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rest-ftp-daemon/remote/ftp.rb', line 95

def upload source, target, use_temp_name = false, &callback
  # Push init
  raise RestFtpDaemon::AssertionFailed, "upload/ftp" if @ftp.nil?

  # Temp file if needed
  dest = target.clone
  if use_temp_name
    dest.generate_temp_name!
  end

  # Move to the directory
  log_debug "upload chdir [#{dest.dir_fs}]"
  @ftp.chdir dest.dir_fs

  # Do the transfer
  log_debug "upload putbinaryfile [#{source.path_fs}] [#{dest.name}]"
  @ftp.putbinaryfile source.path_fs, dest.name, JOB_FTP_CHUNKMB do |data|
    # Update job status after this block transfer
    yield data.bytesize, dest.name
  end

  # Move the file back to its original name
  if use_temp_name
    log_debug "upload rename [#{dest.name}] > [#{target.name}]"
    @ftp.rename dest.name, target.name
  end

end