lib/fake_ftp/server.rb in fake_ftp-0.0.1 vs lib/fake_ftp/server.rb in fake_ftp-0.0.2
- old
+ new
@@ -1,29 +1,34 @@
require 'socket'
-require "thread"
+require 'thread'
module FakeFtp
class Server
- attr_accessor :directory, :port, :passive_port
- attr_reader :files
+ attr_accessor :port, :passive_port
CMDS = %w[acct cwd cdup pass pasv port pwd quit stor type user]
LNBK = "\r\n"
def initialize(control_port = 21, data_port = nil, options = {})
self.port = control_port
self.passive_port = data_port
raise(Errno::EADDRINUSE, "#{port}") if is_running?
raise(Errno::EADDRINUSE, "#{passive_port}") if passive_port && is_running?(passive_port)
@connection = nil
- @data = nil
@options = options
@files = []
- self.directory = "#{Rails.root}/tmp/ftp" rescue '/tmp'
end
+ def files
+ @files.map(&:name)
+ end
+
+ def file(name)
+ @files.detect { |file| file.name == name}
+ end
+
def start
@started = true
@server = ::TCPServer.new('127.0.0.1', port)
@thread = Thread.new do
while @started
@@ -115,24 +120,26 @@
# '200 Okay'
'500 Not implemented yet'
end
def _pwd(*args)
- "257 \"#{self.directory}\" is current directory"
+ "257 \"/pub\" is current directory"
end
def _quit(*args)
respond_with '221 OMG bye!'
@client.close if @client
@client = nil
end
def _stor(filename)
- @files << File.basename(filename.to_s)
respond_with('125 Do it!')
data_client = @data_server.accept
- @data = data_client.recv(1024)
+ data = data_client.recv(1024)
+
+ file = FakeFtp::File.new(::File.basename(filename.to_s), data.length)
+ @files << file
data_client.close
'226 Did it!'
end
\ No newline at end of file