Class: RestFtpDaemon::RemoteFTP

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

Instance Attribute Summary collapse

Attributes inherited from Remote

#job, #log_prefix, #logger

Instance Method Summary collapse

Methods inherited from Remote

#initialize

Constructor Details

This class inherits a constructor from RestFtpDaemon::Remote

Instance Attribute Details

#ftpObject (readonly)

Class options



9
10
11
# File 'lib/rest-ftp-daemon/remote_ftp.rb', line 9

def ftp
  @ftp
end

Instance Method Details

#chdir_or_create(directory, mkdir = false) ⇒ 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
# File 'lib/rest-ftp-daemon/remote_ftp.rb', line 62

def chdir_or_create directory, mkdir = false
  # Init, extract my parent name and my own name
  log_debug "RemoteFTP.chdir_or_create mkdir[#{mkdir}] dir[#{directory}]"
  parent, current = extract_parent(directory)

  #dirname, _current = extract_parent(directory)


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

  rescue Net::FTPPermError => _e
    # If not allowed to create path, that's over, we're stuck
    return false unless mkdir
    chdir_or_create parent, mkdir

    # Now I was able to chdir into my parent, create the current directory
    mkdir current

    # Finally retry the chdir
    retry
  else
    return true
  end
end

#closeObject



117
118
119
120
121
122
123
# File 'lib/rest-ftp-daemon/remote_ftp.rb', line 117

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)


125
126
127
# File 'lib/rest-ftp-daemon/remote_ftp.rb', line 125

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 "RemoteFTP.mkdir [#{directory}]"
  @ftp.mkdir directory

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

#prepareObject



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

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

  # Config
  @chunk_size = DEFAULT_FTP_CHUNK.to_i * 1024

  # Announce object
  log_debug "RemoteFTP.prepare chunk_size:#{@chunk_size}"
end

#present?(target) ⇒ Boolean

Returns:

  • (Boolean)


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

def present? target
  size = @ftp.size target.path
  log_debug "RemoteFTP.present? [#{target.name}]"

rescue Net::FTPPermError
  return false
else
  return size
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
rescue Net::FTPPermError
  log_debug "RemoteFTP.remove! [#{target.name}] not found"
else
  log_debug "RemoteFTP.remove! [#{target.name}] removed"
end

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rest-ftp-daemon/remote_ftp.rb', line 89

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 "RemoteFTP.upload chdir [#{dest.dir}]"
  @ftp.chdir "/#{dest.dir}"

  # Do the transfer
  log_debug "RemoteFTP.upload putbinaryfile [#{dest.name}]"
  @ftp.putbinaryfile source.path, dest.name, @chunk_size 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 "RemoteFTP.upload rename [#{dest.name}] > [#{target.name}]"
    @ftp.rename dest.name, target.name
  end
end