Module: EM::FTPD::Files

Included in:
Server
Defined in:
lib/em-ftpd/files.rb

Instance Method Summary collapse

Instance Method Details

#cmd_dele(param) ⇒ Object

delete a file



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/em-ftpd/files.rb', line 7

def cmd_dele(param)
  send_unauthorised and return unless logged_in?
  send_param_required and return if param.nil?

  path = build_path(param)

  @driver.delete_file(path) do |result|
    if result
      send_response "250 File deleted"
    else
      send_action_not_taken
    end
  end
end

#cmd_rest(param) ⇒ Object

resume downloads



23
24
25
# File 'lib/em-ftpd/files.rb', line 23

def cmd_rest(param)
  send_response "500 Feature not implemented"
end

#cmd_retr(param) ⇒ Object

send a file to the client



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/em-ftpd/files.rb', line 28

def cmd_retr(param)
  send_unauthorised and return unless logged_in?
  send_param_required and return if param.nil?

  path = build_path(param)

  @driver.get_file(path) do |data|
    if data
      send_response "150 Data transfer starting #{data.size} bytes"
      send_outofband_data(data)
    else
      send_response "551 file not available"
    end
  end
end

#cmd_rnfr(param) ⇒ Object

rename a file



45
46
47
48
49
50
51
# File 'lib/em-ftpd/files.rb', line 45

def cmd_rnfr(param)
  send_unauthorised and return unless logged_in?
  send_param_required and return if param.nil?

  @from_filename = build_path(param)
  send_response "350 Requested file action pending further information."
end

#cmd_rnto(param) ⇒ Object

rename a file



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/em-ftpd/files.rb', line 54

def cmd_rnto(param)
  send_unauthorised and return unless logged_in?
  send_param_required and return if param.nil?

  @driver.rename(@from_filename, build_path(param)) do |result|
    if result
      send_response "250 File renamed."
    else
      send_action_not_taken
    end
  end
end

#cmd_size(param) ⇒ Object

return the size of a file in bytes



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/em-ftpd/files.rb', line 68

def cmd_size(param)
  send_unauthorised and return unless logged_in?
  send_param_required and return if param.nil?

  @driver.bytes(build_path(param)) do |bytes|
    if bytes
      send_response "213 #{bytes}"
    else
      send_response "450 file not available"
    end
  end
end

#cmd_stor(param) ⇒ Object

save a file from a client



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/em-ftpd/files.rb', line 82

def cmd_stor(param)
  send_unauthorised and return unless logged_in?
  send_param_required and return if param.nil?

  path = build_path(param)

  if @driver.respond_to?(:put_file_streamed)
    cmd_stor_streamed(path)
  elsif @driver.respond_to?(:put_file)
    cmd_stor_tempfile(path)
  else
    raise "driver MUST respond to put_file OR put_file_streamed"
  end
end

#cmd_stor_streamed(target_path) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/em-ftpd/files.rb', line 97

def cmd_stor_streamed(target_path)
  wait_for_datasocket do |datasocket|
    if datasocket
      send_response "150 Data transfer starting"
      @driver.put_file_streamed(target_path, datasocket) do |bytes|
        if bytes
          send_response "200 OK, received #{bytes} bytes"
        else
          send_action_not_taken
        end
      end
    else
      send_response "425 Error establishing connection"
    end
  end
end

#cmd_stor_tempfile(target_path) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/em-ftpd/files.rb', line 114

def cmd_stor_tempfile(target_path)
  tmpfile = Tempfile.new("em-ftp")

  wait_for_datasocket do |datasocket|
    datasocket.on_stream { |chunk|
      tmpfile.write chunk
    }
    send_response "150 Data transfer starting"
    datasocket.callback {
      puts "data transfer finished"
      tmpfile.flush
      @driver.put_file(target_path, tmpfile.path) do |bytes|
        if bytes
          send_response "200 OK, received #{bytes} bytes"
        else
          send_action_not_taken
        end
      end
      tmpfile.unlink
    }
    datasocket.errback {
      tmpfile.unlink
    }
  end
end