Module: Paperclip::Storage::Ftp
- Defined in:
- lib/paperclipftp.rb
Defined Under Namespace
Classes: FtpTimeout
Class Method Summary collapse
Instance Method Summary collapse
- #exists?(style = default_style) ⇒ Boolean
- #flush_deletes ⇒ Object
- #flush_writes ⇒ Object
- #to_file(style = default_style) ⇒ Object (also: #to_io)
Class Method Details
.extended(base) ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/paperclipftp.rb', line 6 def self.extended base require 'net/ftp' base.instance_eval do @ftp_credentials = parse_credentials(@options[:ftp_credentials]) @passive_mode = !!@options[:ftp_passive_mode] @debug_mode = !!@options[:ftp_debug_mode] @verify_size = !!@options[:ftp_verify_size_on_upload] @timeout = @options[:ftp_timeout] || 3600 end end |
Instance Method Details
#exists?(style = default_style) ⇒ Boolean
17 18 19 20 21 |
# File 'lib/paperclipftp.rb', line 17 def exists?(style = default_style) Timeout::timeout(@timeout, FtpTimeout) do file_size(ftp_path(style)) > 0 end end |
#flush_deletes ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/paperclipftp.rb', line 70 def flush_deletes @queued_for_delete.each do |path| Timeout::timeout(@timeout, FtpTimeout) do begin log("deleting #{path}") ftp.delete('/' + path) rescue Net::FTPPermError, Net::FTPReplyError end end end @queued_for_delete = [] rescue Net::FTPReplyError => e raise e rescue Net::FTPPermError => e raise e ensure close_ftp_connection end |
#flush_writes ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/paperclipftp.rb', line 35 def flush_writes @queued_for_write.each do |style, file| Timeout::timeout(@timeout, FtpTimeout) do file.close remote_path = ftp_path(style) log("uploading #{remote_path}") first_try = true begin ftp.putbinaryfile(file.path, remote_path) rescue Net::FTPPermError => e if first_try first_try = false create_parent_folder_for(remote_path) retry else raise e end end if @verify_size # avoiding those weird occasional 0 file sizes by not using instance method file.size local_file_size = File.size(file.path) remote_file_size = file_size(remote_path) raise Net::FTPError.new "Uploaded #{remote_file_size} bytes instead of #{local_file_size} bytes" unless remote_file_size == local_file_size end end end @queued_for_write = {} rescue Net::FTPReplyError => e raise e rescue Net::FTPPermError => e raise e ensure close_ftp_connection end |
#to_file(style = default_style) ⇒ Object Also known as: to_io
23 24 25 26 27 28 29 30 31 |
# File 'lib/paperclipftp.rb', line 23 def to_file style = default_style return @queued_for_write[style] if @queued_for_write[style] Timeout::timeout(@timeout, FtpTimeout) do file = Tempfile.new(ftp_path(style)) ftp.getbinaryfile(ftp_path(style), file.path) file.rewind return file end end |