Class: RestFtpDaemon::RemoteFTP

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

Overview

Handles FTP and FTPeS transfers for Remote class

Instance Attribute Summary collapse

Attributes inherited from Remote

#log_context, #logger

Instance Method Summary collapse

Constructor Details

#initialize(url, log_context, options = {}) ⇒ RemoteFTP

Returns a new instance of RemoteFTP.



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

def initialize url, log_context, options = {}
  # Call super
  super

  # Use debug ?
  @debug = (Settings.at :debug, :ftp) == true

  # Create FTP object
  if options[: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_info "RemoteFTP.initialize chunk_size:#{@chunk_size}"
end

Instance Attribute Details

#ftpObject (readonly)

Returns the value of attribute ftp.



7
8
9
# File 'lib/rest-ftp-daemon/remote_ftp.rb', line 7

def ftp
  @ftp
end

Instance Method Details

#chdir_or_create(directory, mkdir = false) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rest-ftp-daemon/remote_ftp.rb', line 69

def chdir_or_create directory, mkdir = false
  # Init, extract my parent name and my own name
  log_info "RemoteFTP.chdir_or_create mkdir[#{mkdir}] dir[#{directory}]"
  parent, _current = Helpers.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 "/#{directory}"

    # Finally retry the chdir
    retry
  else
    return true
  end
end

#closeObject



113
114
115
116
117
118
119
# File 'lib/rest-ftp-daemon/remote_ftp.rb', line 113

def close
  # Close init
  super

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

#connectObject



32
33
34
35
36
37
38
39
# File 'lib/rest-ftp-daemon/remote_ftp.rb', line 32

def connect
  # Connect init
  super

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

#connected?Boolean

Returns:

  • (Boolean)


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

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

#mkdir(directory) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/rest-ftp-daemon/remote_ftp.rb', line 61

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

  rescue
    raise JobTargetPermissionError
end

#present?(target) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
# File 'lib/rest-ftp-daemon/remote_ftp.rb', line 41

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

rescue Net::FTPPermError
  # log_info "RemoteFTP.present? [#{target.name}] NOT_FOUND"
  return false
else
  return size
end

#push(source, target, tempname = nil, &callback) ⇒ Object



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

def push source, target, tempname = nil, &callback
  # Push init
  raise RestFtpDaemon::JobAssertionFailed, "push/1" if @ftp.nil?

  # Temp file if provided
  destination = target.clone
  destination.name = tempname if tempname

  # Do the transfer
  log_info "RemoteFTP.push to [#{destination.name}]"

  @ftp.putbinaryfile source.full, target.name, @chunk_size do |data|
    # Update the worker activity marker
    # FIXME: worker_is_still_active

    # Update job status after this block transfer
    yield data.bytesize, destination.name
  end
end

#remove!(target) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/rest-ftp-daemon/remote_ftp.rb', line 52

def remove! target
  log_info "RemoteFTP.remove! [#{target.name}]"
  @ftp.delete target.full
rescue Net::FTPPermError
  log_info "#{LOG_INDENT}[#{target.name}] file not found"
else
  log_info "#{LOG_INDENT}[#{target.name}] removed"
end