# Looking for a good resource on open-uri and FileUtils

**URL:** <https://www.ruby-forum.com/t/looking-for-a-good-resource-on-open-uri-and-fileutils/176630>\
**Category:** Rails\
**Created:** [October 3, 2009, 4:15pm UTC](https://www.ruby-forum.com/t/looking-for-a-good-resource-on-open-uri-and-fileutils/176630 "2009-10-03T16:15:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [October 3, 2009, 4:15pm UTC](https://www.ruby-forum.com/t/looking-for-a-good-resource-on-open-uri-and-fileutils/176630/1 "2009-10-03T16:15:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [October 4, 2009, 7:06pm UTC](https://www.ruby-forum.com/t/looking-for-a-good-resource-on-open-uri-and-fileutils/176630/2 "2009-10-04T19:06:51Z")

</div>

You’re probably better off starting by looking at Paperclip’s S3  
support, which is logically closer to what you want to do.

–Matt J.

On Oct 3, 10:15 am, James W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [October 4, 2009, 7:37pm UTC](https://www.ruby-forum.com/t/looking-for-a-good-resource-on-open-uri-and-fileutils/176630/3 "2009-10-04T19:37:00Z")

</div>

Matt J. 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 J.
> 
> On Oct 3, 10:15ï¿½am, James W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

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
