lib/fake_ftp/server.rb in fake_ftp-0.1.1 vs lib/fake_ftp/server.rb in fake_ftp-0.2.0

- old
+ new

@@ -20,10 +20,11 @@ pass pasv port pwd quit + size stor retr rnfr rnto type @@ -144,27 +145,18 @@ def _cdup(*args) '250 OK!' end def _list(*args) - wildcards = [] - args.each do |arg| - next unless arg.include? '*' - wildcards << arg.gsub('*', '.*') - end - respond_with('425 Ain\'t no data port!') && return if active? && @active_connection.nil? respond_with('150 Listing status ok, about to open data connection') data_client = active? ? @active_connection : @data_server.accept - files = @files - if not wildcards.empty? - files = files.select do |f| - wildcards.any? { |wildcard| f.name =~ /#{wildcard}/ } - end - end + wildcards = build_wildcards(args) + files = matching_files(@files, wildcards) + files = files.map do |f| "-rw-r--r--\t1\towner\tgroup\t#{f.bytes}\t#{f.created.strftime('%b %d %H:%M')}\t#{f.name}\n" end data_client.write(files.join) data_client.close @@ -185,11 +177,18 @@ respond_with('425 Ain\'t no data port!') && return if active? && @active_connection.nil? respond_with('150 Listing status ok, about to open data connection') data_client = active? ? @active_connection : @data_server.accept - data_client.write(files.join("\n")) + wildcards = build_wildcards(args) + files = matching_files(@files, wildcards) + + files = files.map do |file| + "#{file.name}\n" + end + + data_client.write(files.join) data_client.close @active_connection = nil '226 List information transferred' end @@ -269,10 +268,15 @@ @rename_from = nil '550 File not found.' end end + def _size(filename) + file_size = file(filename).bytes + respond_with("213 #{file_size}") + end + def _stor(filename = '') respond_with('425 Ain\'t no data port!') && return if active? && @active_connection.nil? respond_with('125 Do it!') data_client = active? ? @active_connection : @data_server.accept @@ -333,8 +337,27 @@ end rescue Timeout::Error end return false + end + + def build_wildcards(args) + wildcards = [] + args.each do |arg| + next unless arg.include? '*' + wildcards << arg.gsub('*', '.*') + end + wildcards + end + + def matching_files(files, wildcards) + if not wildcards.empty? + files.select do |f| + wildcards.any? { |wildcard| f.name =~ /#{wildcard}/ } + end + else + files + end end end end