class Pione::Util::FTPServer

Attributes

auth_info[RW]
fs[RW]
port[RW]
thread[R]

Public Class Methods

make_location(path) click to toggle source
# File lib/pione/util/ftp-server.rb, line 310
def make_location(path)
  Location["ftp://%s:%s@localhost:%i%s" % [@auth_info.user, @auth_info.password, @port, path]]
end
start(fs) click to toggle source

Start FTP server.

# File lib/pione/util/ftp-server.rb, line 295
def start(fs)
  @fs = fs
  @thread = Thread.new do
    EventMachine.run do
      @sig = EventMachine.start_server("0.0.0.0", @port, EM::FTPD::Server, self)
    end
  end
end
stop() click to toggle source
# File lib/pione/util/ftp-server.rb, line 304
def stop
  EventMachine.stop_event_loop
  EventMachine.reactor_thread.join
  @thread.kill if @thread
end

Public Instance Methods

authenticate(user, password) { |user == user && password == password| ... } click to toggle source

Authenticate the user.

# File lib/pione/util/ftp-server.rb, line 342
def authenticate(user, password, &b)
  yield auth_info.user == user && auth_info.password == password
end
bytes(path) { |get_size| ... } click to toggle source

Get byte size of the path.

# File lib/pione/util/ftp-server.rb, line 347
def bytes(path, &b)
  path = Pathname.new(path).cleanpath
  if fs.file?(path)
    yield fs.get_size(path)
  elsif fs.directory?(path)
    yield -1
  else
    yield false
  end
end
change_dir(path) { |directory?(path)| ... } click to toggle source

Change directory.

# File lib/pione/util/ftp-server.rb, line 318
def change_dir(path, &b)
  path = Pathname.new(path).cleanpath
  yield fs.directory?(path)
end
delete_dir(path) { |true| ... } click to toggle source

Delete the directory.

# File lib/pione/util/ftp-server.rb, line 425
def delete_dir(path, &b)
  path = Pathname.new(path).cleanpath
  if fs.directory?(path) and fs.entries(path).empty?
    fs.rmdir(path)
    yield true
  else
    yield false
  end
end
delete_file(path) { |true| ... } click to toggle source

Delete file of the path.

# File lib/pione/util/ftp-server.rb, line 382
def delete_file(path, &b)
  path = Pathname.new(path).cleanpath
  dir = path.dirname
  filename = path.basename
  if fs.directory?(dir) and fs.entries(dir).include?(filename)
    fs.delete_file(path)
    yield true
  else
    yield false
  end
end
dir_contents(path) { |entries| ... } click to toggle source

Return entries of the directory.

# File lib/pione/util/ftp-server.rb, line 324
def dir_contents(path, &b)
  path = Pathname.new(path).cleanpath
  if fs.directory?(path)
    entries = fs.entries(path).map do |entry|
      entry_path = path + entry
      if fs.directory?(entry_path)
        dir_item(entry)
      else
        file_item(entry, fs.get_size(entry_path))
      end
    end
    yield entries
  else
    yield Set.new
  end
end
get_file(path) { |get_file| ... } click to toggle source

Get file content of the path.

# File lib/pione/util/ftp-server.rb, line 359
def get_file(path, &block)
  path = Pathname.new(path).cleanpath
  if fs.file?(path)
    yield fs.get_file(path)
  else
    yield false
  end
end
make_dir(path) { |false| ... } click to toggle source

Make the directory.

# File lib/pione/util/ftp-server.rb, line 413
def make_dir(path, &b)
  path = Pathname.new(path).cleanpath
  dir = path.dirname
  if fs.exist?(path) or not(fs.directory?(dir))
    yield false
  else
    fs.mkdir(path)
    yield true
  end
end
mtime(path) { |mtime| ... } click to toggle source

Return the mtime.

# File lib/pione/util/ftp-server.rb, line 436
def mtime(path)
  path = Pathname.new(path).cleanpath
  if mtime = fs.get_mtime(path)
    yield mtime
  else
    yield false
  end
end
put_file(path, data) { |size| ... } click to toggle source

Put data to the path.

# File lib/pione/util/ftp-server.rb, line 369
def put_file(path, data, &b)
  path = Pathname.new(path).cleanpath
  dir = path.dirname
  filename = path.basename
  if fs.directory?(dir) and filename
    fs.put_file(path, data)
    yield data.size
  else
    yield false
  end
end
rename(from_path, to_path) { |true| ... } click to toggle source
# File lib/pione/util/ftp-server.rb, line 445
def rename(from_path, to_path, &b)
  from_path = Pathname.new(from_path).cleanpath
  to_path = Pathname.new(to_path).cleanpath
  if fs.file?(from_path) and fs.directory?(to_path.dirname)
    fs.mv(from_path, to_path)
    yield true
  else
    yield false
  end
end
rename_file(from, to) { |true| ... } click to toggle source

Rename the file.

# File lib/pione/util/ftp-server.rb, line 395
def rename_file(from, to, &b)
  from_path = Pathname.new(from).cleanpath
  from_dir = from_path.dirname
  from_filename = from_path.basename
  to_path = Pathname.new(to).cleanpath
  to_dir = to_path.dirname
  to_filename = to_path.basename
  if fs.file?(from_path) && fs.directory?(to_dir)
    data = fs.get_file(from_path)
    fs.delete_file(from_path)
    fs.put_file(to_path, data)
    yield true
  else
    yield false
  end
end

Private Instance Methods

dir_item(name) click to toggle source
# File lib/pione/util/ftp-server.rb, line 458
def dir_item(name)
  EM::FTPD::DirectoryItem.new(:name => name, :directory => true, :size => 0)
end
file_item(name, bytes) click to toggle source
# File lib/pione/util/ftp-server.rb, line 462
def file_item(name, bytes)
  EM::FTPD::DirectoryItem.new(:name => name, :directory => false, :size => bytes)
end