Class: TestFtpd::FileSystemProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/testftpd/file_system_provider.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, server) ⇒ FileSystemProvider

Returns a new instance of FileSystemProvider.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/testftpd/file_system_provider.rb', line 6

def initialize(path, server)
  @path = path
  @server = server
  @ftp_name = path.split('/').last
  @ftp_name = '/' unless @ftp_name
  @ftp_dir = File.directory?(path)
  @ftp_size = File.size?(path)
  @ftp_size = 0 unless @ftp_size
  @ftp_date = Time.now
  @ftp_date = File.mtime(path) if File.exists?(path)
end

Instance Attribute Details

#ftp_dateObject (readonly)

Returns the value of attribute ftp_date.



4
5
6
# File 'lib/testftpd/file_system_provider.rb', line 4

def ftp_date
  @ftp_date
end

#ftp_nameObject (readonly)

Returns the value of attribute ftp_name.



4
5
6
# File 'lib/testftpd/file_system_provider.rb', line 4

def ftp_name
  @ftp_name
end

#ftp_sizeObject (readonly)

Returns the value of attribute ftp_size.



4
5
6
# File 'lib/testftpd/file_system_provider.rb', line 4

def ftp_size
  @ftp_size
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/testftpd/file_system_provider.rb', line 4

def path
  @path
end

#serverObject (readonly)

Returns the value of attribute server.



4
5
6
# File 'lib/testftpd/file_system_provider.rb', line 4

def server
  @server
end

Class Method Details

.format_list_entry(file_system_provider) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/testftpd/file_system_provider.rb', line 81

def self.format_list_entry(file_system_provider)
  raw = (file_system_provider.directory?) ? 'd': '-'
  raw += 'rw-rw-rw- 1 ftp ftp '
  raw += file_system_provider.ftp_size.to_s + ' '
  raw += file_system_provider.ftp_date.strftime('%b %d %H:%M') + ' '
  raw += file_system_provider.ftp_name
  raw += "\r\n"
  raw
end

Instance Method Details

#directory?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/testftpd/file_system_provider.rb', line 18

def directory?
  @ftp_dir
end

#ftp_create(name, dir = false) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/testftpd/file_system_provider.rb', line 46

def ftp_create(name, dir = false)
  return FileSystemProvider.new(path + '/' + name, server) unless dir
  Dir.mkdir(path + '/' + name)
  FileSystemProvider.new(path + '/' + name, server)
rescue
  return false
end

#ftp_delete(dir = false) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/testftpd/file_system_provider.rb', line 72

def ftp_delete(dir = false)
  if dir
    FileUtils.remove_dir(path)
  else
    FileUtils.remove_file(path)
  end
  true
end

#ftp_list(filter = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/testftpd/file_system_provider.rb', line 30

def ftp_list(filter = nil)
  if filter
    if File.directory?(File.join(path, filter))
      entries = Dir.entries(File.join(path, filter)).map { |name| File.join(filter, name) }
    else
      entries = Dir.glob(File.join(path, filter)).map { |name| File.basename(name) }
    end
  else
    entries = Dir.entries(path)
  end
  entries = entries.reject { |name| %w{. ..}.include?(File.basename(name)) } unless server.config.fetch(:include_dot_folders, false)
  entries.map do |name|
    FileSystemProvider.new(File.join(path, name), server)
  end
end

#ftp_parent(root = nil) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/testftpd/file_system_provider.rb', line 22

def ftp_parent(root = nil)
  return root if root && root.path == path
  path_parts = path.split('/')
  return nil unless path_parts.pop
  return nil if path_parts.size <= 1
  FileSystemProvider.new(path_parts.join('/'), server)
end

#ftp_rename(to_name) ⇒ Object



66
67
68
69
70
# File 'lib/testftpd/file_system_provider.rb', line 66

def ftp_rename(to_name)
  to_path = File.join(File.dirname(path), to_name)
  FileUtils.mv(path, to_path)
  true
end

#ftp_retrieve(output) ⇒ Object



54
55
56
# File 'lib/testftpd/file_system_provider.rb', line 54

def ftp_retrieve(output)
  File.open(path, 'r') { |io| output << io.read }
end

#ftp_store(input) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/testftpd/file_system_provider.rb', line 58

def ftp_store(input)
  return false unless File.open(path, 'w') do |f|
    f.write input.read
  end
  @ftp_size = File.size?(path)
  @ftp_date = File.mtime(path) if File.exists?(path)
end