Get server file path with Paperclip
Assuming you had an attachment called avatar on an instance of a user, you can use user.avatar.path
to get the full path of the file on the filesystem, and you can use user.avatar.url
to give the path which you could use in image tags and whatnot.
Is that what you're meaning?
Updating Paperclip path file names from on server to s3
Say you have a model User with an attachment named profile_pic;
Go into the rails console eg. rails c
and then get an object for the model you have the attachment on, eg. u = User.find(100)
.
Now type u.profile_pic.url
to get the url or u.profile_pic_file_name
to get the filename.
To see the effect of other options (for example your old options) you can do;
p = u.profile_pic # gets the paperclip attachment for profile_pic
puts p.url # gets the current url
p.options.merge!(url: '/blah/:class/:attachment/:id_partition/:style/:filename')
puts p.url # now shows url with the new options
Similarly p.path
will show the local file path with whatever options you pick.
Long story short, something like;
User.where('created_at < some_date').map do |x|
"#{x.id} #{x.profile_pic_file_name} #{x.profile_pic.path}"
end
should give you what you want :)
Rails paperclip gem - Get file from private folder
The first thing we need to do is add a route to routes.rb for accessing the files.
Edit routes.rb and add the :member parameter in bold:
resources :users, :member => { :avatars => :get }
Now to get the avatar for user 7, for example, we can issue a URL like this:
localhost:3000/users/7/avatars
… and the request will be routed to the avatars
action in the users
controller (plural since a user might have more than one style of avatar).
So now let’s go right ahead and implement the avatars method and add some code to download a file to the client. The way to do that is to use ActionController::Streaming::send_file. It’s simple enough; we just need to pass the file’s path to send_file as well as the MIME content type which the client uses as a clue for deciding how to display the file, and that’s it! Let’s hard code these values for the better understanding (update the path here for your machine):
class UsersController < ApplicationController
def avatars
send_file '/path/to/non-public/system/avatars/7/original/mickey-mouse.jpg',
:type => 'image/jpeg'
end
end
Now if you type localhost:3000/users/7/avatars
into your browser you should see the mickey image.
Instead of hard coding the path in the avatars method, we obviously need to be able to handle requests for any avatar file attachment for any user record. To do this, configure Paperclip and tell it where the files are now stored on the file system, and which URL we have configured our routes.rb file to use.
To do this, we need to add a couple of parameters to our call to has_attached_file in our User model (user.rb),
has_attached_file :avatar,
:styles => { :thumb => "75x75>", :small => "150x150>" },
:path =>
':rails_root/non-public/system/:attachment/:id/:style/:basename.:extension',
:url => '/:class/:id/:attachment'
But now we can generalize our code in UserController to handle any user, like this:
def avatars
user = User.find(params[:id])
send_file user.avatar.path, :type => user.avatar_content_type
end
Now we can test localhost:3000/users/7/avatars
again to be sure that we haven’t broken anything.
Cheers!
How do you access the raw content of a file uploaded with Paperclip / Ruby on Rails?
Here's how I access the raw contents of my attachment:
class Document
has_attached_file :revision
def revision_contents
revision.copy_to_local_file.read
end
end
Please note, I've omitted my paperclip configuration options and any sort of error handling.
Paperclip, set path outside of rails root folder
The options ":path" and ":url" must be used together in your case. I believe what is missing is some configurations on your webserver and the :url configuration.
":path" --> tells paperclip where the files are inside the server file system.
":url" --> tells paperclip how to determine the url to be generated. It is relative to the website URL.
So through configurations on your WebServer you should map the server folder where ":path" to a virtual directory under the rails app folder structure.
This virtual directory should than reflect the configuration in the ":url" option.
for example lets say you did
:path => "/tmp/shared/:class/:id_partition/:style.:extension"
Step one configure a virtual folder under your rails app with the name:
MyNewVirtualFolder
and point it to "/tmp/shared"
Step two configure
:url => "/MyNewVirtualFolder/:class/:id_partition/:style.:extension"
and finally re-start your rails app.
Net::HTTP get a PDF file and save with paperclip
Assuming the PDF object you're getting is okay (I'm not 100% sure it is), then you could do this:
file = StringIO.new(attachment) #mimic a real upload file
file.class.class_eval { attr_accessor :original_filename, :content_type } #add attr's that paperclip needs
file.original_filename = "your_report.pdf"
file.content_type = "application/pdf"
then save the file with Paperclip.
(from "Save a Prawn PDF as a Paperclip attachment?")
Related Topics
Rails Previous Sunday in Relation to Any Datetime
Synchronized Method for Concurrency in Ruby
Displaying Only the First X Words of a String in Rails
Printing to Screen in a Rake Task
What's the Difference Between Ruby Core API and Standard Library API
How to Define a Ruby Singleton Method Using a Block
Resetting a Singleton Instance in Ruby
Ruby Regex - Gsub Only Captured Group
How to Route Controllers Without Crud Actions
Remove "Www", "Http://" from String
Simple_Form's Collection_Radio_Button and Custom Label Class
How to Generate PDF from Markdown Using Pure Ruby
Rails Console Default Environment
Executing Shell Command in Background from Ruby with Proper Argument Escaping
Convert a String Date Format from "17-Nov-2011" to "11/17/11"