Rails 5 - download a paperclip attachment - ruby-on-rails

In the app I am building to learn RoR, I want to automatically download an attached pdf. Attached with Paperclip. Then to get the pdf properties using pdfinfo.
In my annotation.rb model I attach using
has_attached_file :file,
styles: { large: "600x600>", medium: "500x500>", thumb: "150x150#" },
default_url: "/images/:style/missing.png"
In my AnnotationsController
require 'pdfinfo'
pdfinfo = Pdfinfo.new('#annotation.file.path')
page_count = pdfinfo.page_count
this throws an error
Pdfinfo::CommandFailed in AnnotationsController#pdf pdfinfo -f 0 -l -1
-enc UTF-8 #annotation.file.path
What does this error mean and how can I get the file to be read? I learned about downloading using send_file, but have not succeeded.

You are passing a literal string to the new method. You need to remove the quotes.
require 'pdfinfo'
pdfinfo = Pdfinfo.new(#annotation.file.path)
page_count = pdfinfo.page_count

Related

Paperclip image upload in rails

I am using paperclip for image uploading and in my model I have specified styles to store the images with various sizes, I have imagemagick installed, here are the codes
has_attached_file :image,
styles: { medium: '300x300>', thumb: '40x40>' },
path: 'images/:id/image/:basename.:extension',
default_url: 'default.png'
validates_attachment :image, content_type: { content_type: %w[image/jpeg image/png] }
It doesn't seems to store images with these specified styles, It only stores image with default style. Do I miss anything here?. Thanks in advance
I did a mistake by overriding the paperclip default path without :style specified. So it stores only the default size. The processed image (by ImageMagick) does not store as I didn't mention at override path.

Adding custom "styles" to Paperclip gem

Right now I have this
has_attached_file :cover_image,
styles: { square: "200x200#", small:"400x400>",medium:"500x600" }
I want to have this:
has_attached_file :cover_image,
styles: { thumb: "50x50#", square: "200x200#", small: "400x400>", medium: "500x600" }
When I add the "thumb" key I get a 404 error. Is there a way to create a new "style"?
Seems like there is no issue in your code.
You might be getting error while fetching previous record's thumb, which is not generated yet. Try create previous record thumbnail also and your error will gone.
Run below command in console.
rake paperclip:refresh CLASS=User ## change User with model name.
More information you can find here.

Ruby on rails gem paperclip How to generate missing image

I uploaded a missing.png to public/images/ folder. But the the missing.png not showing correctly.
The website requires images/small/missing.png
So I guest I should generate the missing.png for thumb small medium large size.
What should I do?
Update 1:
I manually create a folder public/images/small and put missing.png inside the small folder. The website shows the missing.png. But What's is the correct way to generate all size of missing.png?
Try to add this in your model:
has_attached_file :image, styles: { :small => "150x150>", medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"

Paperclip says content type is wrong when using an external URL as attachment

I'm using Paperclip 4.2 + Rails 4.1.6 with the following model:
class Post < ActiveRecord::Base
has_attached_file :featured_image, styles: { :medium => "300x300>", :thumb => "100x100>" }
validates_attachment :featured_image, :content_type => { :content_type => ["image/jpeg", "image/gif", "image/png"] }
def featured_image_from_url(url)
self.featured_image = URI.parse(url)
end
end
When I upload a file with the file uploader in my form, everything works fine. The attachment is set and the thumbnails are generated.
However, if I try to use a remote URL pointing to a jpeg image, like specified here, the Post cannot be saved because the attachment has the wrong content type: featured_image_content_type: "binary/octet-stream"
If I force the content type by setting it manually:
post.featured_image_content_type = "image/jpeg"
post.save
then the model is saved successfully.
Hi I don't know whether you found a workaround yet. I have used the following code (amended for your example) to stop Paperclip (4.2.1) raising an exception when a webserver returns an incorrect content-type:
def featured_image_from_url(url)
self.featured_image = URI.parse(url)
# deal with the case where the webserver
# returns an incorrect content-type
adapter = Paperclip.io_adapters.for(featured_image)
self.featured_image_content_type = Paperclip::ContentTypeDetector.new(adapter.path).detect
end
There may be a neater or more correct way!
here is a gem that will help you to download a url to Tempfile so this way you get around the issue with s3 sending stream mime type https://github.com/equivalent/pull_tempfile

Getting correct path/url from Paperclip Gem during after_post_process callback?

I have a fairly standard Paperclip setup, it's almost straight out of the readme. I have a simple method triggered via callback to get the primary colors out of an uploaded image, and save them to the corresponding instance.
class Image < ActiveRecord::Base
has_attached_file :file, :styles => { large: "800x>", :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :file, :content_type => /\Aimage\/.*\Z/
after_post_process :get_colors
def get_colors
colors = Miro::DominantColors.new(self.file.url)
colors = colors.to_hex.join(',')
self.colors = colors
self.save
end
end
As you can see, I have an after_post_process callback, and it does get triggered. The trouble is that when I call self.file.url I get a path that looks like this:
"/system/images/files//original/Peterson-Products-Wireframe-v01.jpg?1398443345".
It's missing the :id_partion portion. It's real path should look more like:
"/system/images/files/000/000/033/original/Peterson-Products-Wireframe-v01.jpg?1398443345"
Should I be using some other callback? I only want this triggered once per upload... Never again if the image is updated. Is this a bug in paperclip that I should be filing on Github?
Rails Version 4.1
Paperclip Version 4.1
Ruby 2.1.0
Thanks so much!
try queued_for_write
colors = Miro::DominantColors.new(file.queued_for_write[:original].path)

Resources