14

I'm using ruby 1.9.2 along with Rails 3.1.4 and Paperclip 2.4.5.

My issue is trying to save a paperclip attachment from a URI loses the file extension and saves the file without one resulting in issues with things like fancybox that require an extension.

Some example code:

uri = "http://featherfiles.aviary.com/2012-06-13/bbe5f0de1/0c5a672b88ea47ecb4631ac173e27430.png"
open(uri)
#=> #<File:/var/folders/zc/d69gxhzx10x_bvjrkqgyjgxr0000gn/T/open-uri20120613-27204-i6cldv>

Because there is no extension on the temp file paperclip is saving the file without one resulting in issues.

Has anyone run into this issue? I've seen multiple answers about using paperclip to store images from a URI but none seem to address the same problem we're running

Jimmy
  • 9,686
  • 14
  • 59
  • 78

6 Answers6

6

Don't use the temporary file! It's there as a placeholder as the file is read from the port, and should be considered a private resource for OpenURI. Instead, use open(url).read and work with the resulting content by saving it.

Do something like:

require 'uri'
require 'open-uri'

url = 'http://www.iana.org/domains/example/index.html'
filename = File.basename(URI.parse(url).path)
File.open(filename, 'wb') do |fo|
  fo.write(open(url).read)
end

Temporarily spooling to disk during an operation, especially a network operation, is common. Once the file's content has been accumulated, then it is available to be passed off to the app. read is blocking, so your code will stop there until the file is returned to you. Then you can play with it.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
3

Extension isn't important for temporary file, but if you want use this file in code or save to another place. You can do it:

temp_file = open(params[:url])
def temp_file.original_filename; File.basename(base_uri.path); end

Now, you can save this temporary file to permanent space or use it in code; Original filename will be used automatically.

  • Doesn't work. Some additional magic seems to be necessary to make 'base_uri' accessible to the defined original_filename method here, fyi. – ericpeters0n Nov 11 '16 at 23:31
0

Im not sure if this will help in your case, but I was noticing similar issues in my project.

The issue turned out to be not caused by Paperclip nor open-uri, but the receiver of the paperclip file (in my case Spree Commerce). Check that you are assigning the paperclip object to the right object, and that it is being interpreted correctly.

The fix that worked for me was to change:

@product.images << Spree::Image.create({ 
    :attachment => open(image_url) 
}, :without_protection => true)

to

@product.master.images << Spree::Image.create({ 
    :attachment => open(image_url) 
}, :without_protection => true)

Good luck with your issue

fatty
  • 2,453
  • 2
  • 25
  • 24
0

Have you inclued the :extension in your path/url option?

For example:

has_attached_file :image,
  ...
  :url  => '/images/highlights/:id_partition/:style_:id.:extension',
  :path => ':rails_root/files/images/highlights/:id_partition/:style_:id.:extension'

This will probably solve your problem. You can force an extension there, but I don't think that's recommended.

Tiago
  • 2,966
  • 4
  • 33
  • 41
0

Update – Paperclip can do this on its own!

Posted by Aditya Sanghi (thanks a lot!):

current_comments.pictures.create!(file: URI.parse(image_url))

Although keep in mind, that you still need to handle 500, 404, etc errors (Paperclip can raise them).

Thanks to: https://mensfeld.pl/2013/12/rails-paperclip-open-uri-downloading-files-from-the-internet-and-saving-them-with-paperclip/

Magne
  • 16,401
  • 10
  • 68
  • 88
-1

Yes, it is a problem but we can get around this with fancybox.

In the link tag(for image) add :type => 'image'

- @images.each do |image|
 = link_to image_tag(image.attachment.url),   image.attachment.url, class: "fancybox", type: 'image'

By specifying 'type', Fancybox overrides the type as image

https://groups.google.com/forum/?fromgroups=#!topic/fancybox/QgjquBCLynU

Rostyslav Dzinko
  • 39,424
  • 5
  • 49
  • 62
user1286523
  • 39
  • 1
  • 4
  • 1
    this doesn't really get around much and adds a requirement for fancybox to display images – Jimmy Sep 06 '12 at 02:59