Let's says I have the following entry in my seeds.rb file :
Image.create(:id => 52, :asset_file_name => "somefile.jpg", :asset_file_size => 101668, :asset_content_type => "image/jpeg", :product_id => 52)
If I seed it, it tries to process the image specified, I get this error :
No such file or directory - {file path} etc...
My images are backed up, so I don't really need to create them; but I need the record though. I can't comment the paperclip directive in my model; then it works; but I guess there might be another solution.
Is there another pattern to follow in order to accomplish it ? Or a turnaround to tell paperclip not to process the image ?
Rather than setting the asset columns directly, try leveraging paperclip and setting it as ruby File object.
Image.create({
:id => 52,
:asset => File.new(Rails.root.join('path', 'to', 'somefile.jpg')),
:product_id => 52
})
The other answer here certainly works for most situations, but in some cases it may be better yet to provide an UploadedFile rather than a File. This more closely mimics what Paperclip would receive from a form and provides some additional functionality.
image_path = "#{Rails.root}/path/to/image_file.extension"
image_file = File.new(image_path)
Image.create(
:id => 52,
:product_id => 52,
:asset => ActionDispatch::Http::UploadedFile.new(
:filename => File.basename(image_file),
:tempfile => image_file,
# detect the image's mime type with MIME if you can't provide it yourself.
:type => MIME::Types.type_for(image_path).first.content_type
)
)
While this code is somewhat more complicated, it has the benefit of correctly interpreting Microsoft Office documents with .docx, .pptx, or .xlsx extensions which, if attached using a File object, will be uploaded as zip files.
This especially matters if your model permits Microsoft Office documents but does not allow zip files, because validations will otherwise fail and your object won't be created. It wouldn't have affected the OP's situation, but it affected mine, and so I wish to leave my solution in case anyone else needs it.
Related
I use paperclip to upload a users avatar. The image is stored correctly in the /public directory. However I cant figure out how I can get the image displayed. I played with the :url and :path settings for about an hour and cant match them in a way the image will be displayed in the browser.
There is always a 'images/localhost' in the GET-requests path that I can not get rid of.
Here is my code:
user.rb
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "missing.png",
:url => ':class/:id/:style.:extension',
:path => ':url'
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end
The path in the html-tag looked up by the request looks like this:
<img alt="Original" src="/images/localhost/users/1/original.png?1405249849" />
The correct request which returns the image would be
http://localhost:3000/users/1/original.png?1405248482.
How can I get the request match the correct file-system-path. Or: what is wrong with my settings?
I didnt change the application.rb or the development.rb
Best regards,
Kev
you should take url or path helpers of paperclip wherever possible. So to show the image use:
<%= image_tag #user.avatar.url(:medium) %>
Then:
the image url isn't a file system path. Depending on the storage you use in paperclip, images can reside in different places, see understanding storage of the paperclip gem.
If you use file storage, the files are store somewhere like
public/system/users/avatar/000/000/013/small/my_pic.png
Timestamp
What you're seeing is Paperclip's timestamp - the last time the file / object was updated.
Although I can't find any official reference to this number, it basically allows you to determine which files you're dealing with. According to the question referenced above, it's apparently there for if you want to ensure your visitors see the latest version of the file (IE never gets stored in the cache)
I'm not sure why there is a disparity between your stored image & your path. However, I would say the path is correct; you just need to be able to
--
Bottom line - if your image shows on the page, I don't think there's any systemic issue with your path; if it doesn't show on the page, can you provide logs / reference to the error?
This post, https://stackoverflow.com/a/26222093/1949363, which pointed to http://www.bwigg.com/2009/10/paperclip-customizing-paths-and-urls/ was the answer for me. I was having issues only in my test environment but I believe the fix should work in other environments as well.
Try the following settings:
:path => "public/system/:class/:id/:filename",
:url => "/system/:class/:id/:basename.:extension"
This question already has answers here:
How do you access the raw content of a file uploaded with Paperclip / Ruby on Rails?
(7 answers)
Closed 5 years ago.
I have paperclip running to upload and store files in a non-public directory on the server. Now I want to be able to read the files directly and or feed them into a gem such as axlsx. I'm struggling with even simply looping threw a text file and think I'm missing something basic (as is usually the case).
Here is my first attempt at opening the file:
Paperclip config in application.rb:
config.paperclip_defaults = {:storage => :fog, :fog_credentials => {:provider => "Local", :local_root => "#{Rails.root}/secured_storage"}, :fog_directory => "", :fog_host => "localhost"}
Model:
class Census < ActiveRecord::Base
has_attached_file :censusfile
validates_attachment_content_type :censusfile,
:content_type => ["application/octet-stream", "text/plain"]
end
In the Controller:
def processcensus
#census=Census.find(params[:id])
#file=#census.censusfile.path
end
In the View:
<% File.readlines(#file).read do |line| %>
<%= line %>
<% end %>
This fails because the 'path' returned by Paperclip is the path relative to its sotrage path, not the full path.
UPDATE: If I add the directory (in this case "secured_storage" in from of the path it work as expected. For example:
#file="secured_storage/" + #census.censusfile.path
Not sure if this is at all the way to address this. If it is, is there a way to ask Paperclip where it is storing the files??
I've read where I could use:
Paperclip.io_adapters.for(#census.censusfile).path
But that seems to read the file into an array unless I'm missing something completely. My goal is to be able to loop threw a text file as well as feed an Excel file to axlsx for processing. I would also like to be able to eventually feed these files directly to a user somehow to allow for secure downloads.
I have looked hard for some documentation on all this and haven't found anything that really explains it yet. I'm to that point of just randomly throwing code here or there and hoping something works, which rarely does. Any help/direction that could be provided would be GREATLY appreciated!!!
Mark
I think io adapter can support read
Paperclip.io_adapters.for(#census.censusfile).read
so
<% Paperclip.io_adapters.for(#census.censusfile).read %>
<%= line %>
<% end %>
Use the copy_to_local_file method. This returns a file object on which you can read like on a normal file`.
Im referring to this app link as a tutorial. After looking at it. Im curious to understand how the program (client or server)can accesse images. Since the program doesn't seem to be explicitely saving image urls in the server's database once they are uploaded to Amazon S3?
After implementing this app, I dont see a column for image_urls or path in the database. But the program magically loads images in the client!!
Something is going on under the hood, what is that something?
Here is the Photo model method:
Paperclip.interpolates :prefix do |attachment, style|
"#{attachment.instance.takenby}/#{Date.today.to_s }/#
{attachment.instance.image_file_name}"
end
has_attached_file :image,
:path => ":prefix/:style/:basename.:extension",
:styles => { :thumbnail => "57x57", :original => "300x300" },
:storage => :s3,
:s3_credentials => S3_CREDENTIALS
validates :image,:presence => true
validates :lat, :lng,:presence => true,:numericality => true
It's right there in the options for has_attached_file. You've told it how to construct the path for the image (which corresponds to its filename on s3) in the :path argument. So all paperclip needs to know to find the image again is:
The host name for your s3 bucket
The relevant information to re-construct the path - in this case, that looks like :prefix, :basename, :style, and :extension.
Most of that information is in your database. Presumably :basename comes from the original filename somehow, which ought to be saved in the image_file_name field of the model to which the image is attached. :style depends on which image size you're looking up at runtime. :extension can be determined from the image_content_type attribute.
:prefix is a little bit trickier. In fact, I worry that your interpolation rule will break your image lookup, because of the Date.today it uses to construct the prefix. Do you have any images more than one day old? If so, do they still work? I worry that when the image is uploaded, it will have a filename containing that day's date, and then when you go to find them in the future you'll reconstruct the url using the new current date, and wind up with a 403 error.
TL;DR paperclip constructs the image url when it uploads the images to s3, and reconstructs them later from the same parameters.
i've been using Paperclip, when deleting is done, it is just deleting the attachments, but the images saved in the file system is not being deleted.can anybody plz tell how to delete the images from the file system using paperclip itself.
the code i've been using is
has_attached_file :image_o_filename,
:styles => {
:tn => ["100x100#",:jpg ,:name => :tn],
:w => ["640x480>",:jpg, :name => :w],
:l_tn => ["200x150#",:jpg, :name => :l_tn]
},
:path => "/places/hotels/:image_pre_path/images/:basename_:style.:extension"
when i delete the photos from the view, images are getting deleted from the database but not from the file system with the above path, images remain without getting deleted. what might be the reason ??
and no errors generated in console also..
Paperclip has a very nice built in delete method, I was stuck on a very similar problem and was very well informed in this solution:
I created a method inside the controller called delete_image then from the view I could call this method.
def delete_image
#user.image.destroy
end
I had the image attached to a user instance and this worked perfect, don't forget to define your method in the routes file in your config/routes.
Good luck also you can look up more information here:
See: Rails Paperclip how to delete attachment?
This is tough one to explain so i'll try my best, and hopefully edit the question if people need more information. I am not providing exact code, but merely an example of the issue.
I am using rails 2.3.8. I am on Unix.
I have a bunch of files under a directory not Apache accessible. (i.e. /data/files/file.rpk)
I have the following in my view.
link_to "RPK File", :controller => 'mycontroller', :action=> 'myaction', :file => '/data/files/file.rpk'
I have the following in my controller.
def myaction
if FileTest.exists?(params[:file])
render :file => params[:file]
end
end
When i select the link on the page i get a download prompt for my desired file, but the name of the file is "myaction" instead of the filename.
Thoughts on how i could get it named correctly?
Sounds like a job for send_file. The x_sendfile option prevents that your workers keep busy while transferring the actual file. You can read more about that in this blogpost.
send_file path_to_file_on_filesystem, :type => "application/zip", :x_sendfile => true
You want to use send_data with the :filename option. See the API documentation.
You want to be extremely careful with this, though. Never ever trust the client/user! They will send file=../../../../etc/group or something in order to read arbitrary files on your system, so be very sure to sanitize that value before passing it to any file-reading methods.