Hi all, I''m needing to extend the Paperclip Storage module to handle external URL''s so that I can upload my doc''s/images to an external source rather than the app''s local file system. The FileSystem module reads module Filesystem def self.extended base end def exists?(style = default_style) if original_filename File.exist?(path(style)) else false end end # Returns representation of the data of the file assigned to the given # style, in the format most representative of the current storage. def to_file style = default_style @queued_for_write[style] || (File.new(path(style)) if exists?(style)) end alias_method :to_io, :to_file def flush_writes #:nodoc: @queued_for_write.each do |style, file| FileUtils.mkdir_p(File.dirname(path(style))) result = file.stream_to(path(style)) file.close result.close end @queued_for_write = {} end def flush_deletes #:nodoc: @queued_for_delete.each do |path| begin FileUtils.rm(path) if File.exist?(path) rescue Errno::ENOENT => e # ignore file-not-found, let everything else pass end end @queued_for_delete = [] end end Now I figured that I should be able to make use of Open-URI to emulate the above functionality so that Paperclips expected behaviour is fully implemented. What I could do with is a good resource that can show me how I can emulate the same functionality with Open-URI. Any pointers to good examples, ideas, code snippets, examples, help etc... would be greatly appreciated -- Posted via http://www.ruby-forum.com/.
Matt Jones
2009-Oct-04 17:05 UTC
Re: Looking for a good resource on open-uri and FileUtils
You''re probably better off starting by looking at Paperclip''s S3 support, which is logically closer to what you want to do. --Matt Jones On Oct 3, 10:15 am, James West <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi all, > I''m needing to extend the Paperclip Storage module to handle external > URL''s so that I can upload my doc''s/images to an external source rather > than the app''s local file system. > > The FileSystem module reads > > module Filesystem > def self.extended base > end > > def exists?(style = default_style) > if original_filename > File.exist?(path(style)) > else > false > end > end > > # Returns representation of the data of the file assigned to the > given > # style, in the format most representative of the current storage. > def to_file style = default_style > @queued_for_write[style] || (File.new(path(style)) if > exists?(style)) > end > alias_method :to_io, :to_file > > def flush_writes #:nodoc: > @queued_for_write.each do |style, file| > FileUtils.mkdir_p(File.dirname(path(style))) > result = file.stream_to(path(style)) > file.close > result.close > end > @queued_for_write = {} > end > > def flush_deletes #:nodoc: > @queued_for_delete.each do |path| > begin > FileUtils.rm(path) if File.exist?(path) > rescue Errno::ENOENT => e > # ignore file-not-found, let everything else pass > end > end > @queued_for_delete = [] > end > end > > Now I figured that I should be able to make use of Open-URI to emulate > the above functionality so that Paperclips expected behaviour is fully > implemented. > > What I could do with is a good resource that can show me how I can > emulate the same functionality with Open-URI. > > Any pointers to good examples, ideas, code snippets, examples, help > etc... would be greatly appreciated > -- > Posted viahttp://www.ruby-forum.com/.
James West
2009-Oct-04 17:37 UTC
Re: Looking for a good resource on open-uri and FileUtils
Matt Jones wrote:> You''re probably better off starting by looking at Paperclip''s S3 > support, which is logically closer to what you want to do. > > --Matt Jones > > > On Oct 3, 10:15�am, James West <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>Thank you Matt I''m struggling to understand how the right_aws functionality works so I can emulate it. I have so far been able to implement the file_exist? function using Net::FTP as follows def exists?(style = default_style) if original_filename result = false Net::FTP.open(domain_name) do |ftp| #Need to implement domain_name, username and password params. #Probably best to emulate the way S3 module does this with a yml file? ftp.login(''myusername'', ''mypassword'') ftp.chdir(path(style)) begin ftp.list(file_name) true rescue #Need to extend this to raise all errors except file not found #Need to add specific check for file not found exception result = false # puts $! end end end end I''m now looking at how I can code the to_file method def to_file style = default_style #This adds some kind of file stream (I think) to the @queued_for_write hash array. I need to understand how to do this for a NET::FTP connection. @queued_for_write[style] || (File.new(path(style), ''rb'') if exists?(style)) end I need to understand exactly what that is doing first though lol! The flush_writes method should be pretty easy to implement. (I''ve worked out how to use NET::FTP to make a directory and I''m sure the move and chmod functions shouldn''t be too difficult once I have the syntax for those. It''s a huge learning curve for me but I''m getting there (slowly lol) Still, any help or pointers would be really really appreciated -- Posted via http://www.ruby-forum.com/.