How to store images in db_files in attachment_fu plugin? - ruby-on-rails

In my rails application I have used attachment_fu plugin for image upload.
I am new on rails, I want to store file in attachment_fu db_files table.
How to store images in db_files and which path i want to specify to display images?
Thanks

Unfortunately attachment_fu does not support the same attachment "interface" for :storage => :db_file as it does for :storage => :file_system, but there's a plugin that fills the void here :
https://github.com/kares/attachment_fx
Extends the file interface for the
:db_file backend (#see attachment_fu
:storage option). The database backend
interface mimics the :file_system
storage, the db data is on-demand
downloaded into the public directory
(the target path prefix is
customizable with the :path_prefix
option).
You basically use the :file_system methods like You're used to with the :db_file backend e.g. photo.public_filename(:small). As a bonus You can enjoy some useful helpers from the owning model :
user.has_photo?
user.photo_path(:small)
user.photo_full_path

Related

How and where paperclip via aws-sdk store URLs?

Sorry for maybe dumb question, but I have some misunderstanding about how Paperclip works. If I use it with aws-sdk gem, where image urls are stored?
My model has only fields like image_name, image_size, but not something like image_url. I also tried to list all tables from my db in sql console, but there wasn't any special image-related tables. I know what I can access url by calling my_model.image.url, but I need understanding, where this url is located.
Thanks.
The urls are auto generated based on the ruleset you have in your paperclip config file. They usually include the name and id of your model unless you have changed the defaults. Set the :default_url option when you call has_attached_file if you want to change it:
:default_url => '/images/:attachment/missing_:style.png',
:path => "avatars/:id/:style/:filename"
More background: rails paperclip default image with S3

show image using absolute path rails

For Ruby on Rails I want to use
<img src =../../../photo/1.PNG >
to show some images stored outside my app folder. I use this and use .. to point out where image it it , But it does not work。 I use Win7, Is there anyone could help me ?
I have a MVC named "showcase" generated by scaffold, and I wanna to have the same showcase in two apps.
In app1: showcase (where I wanna to share the images)model: house is the image name.
class Showcase < ActiveRecord::Base
attr_accessible :house,:sequence
has_attached_file :house, :whiny => false,
:styles => { :large => "350x300>",
:medium => "120x100>",
:thumb => "50x50>"}
end
then the images will be uploaded in the folder: public/systems/showcases/houses/000/000/0001/1.PNG
in app2. I create a showcase model with migration. and use ActiveResources share the database between app1 and app2. but app2 can not configure paperclip type.
class Showcase < ActiveResource::Base
self.site = "http://localhost:3000"
end
how can I in app2, call images uploaded from app1.
If your trying to really share images between applications, and they are uploads and not static assets then create a systems folder somewhere else, then make the system folders in each app a symlink to the share folder. That way your not having crazy url paths, and they both can asses it, and you wont end up with dupes or mismatches.
You should really use the rails assets pipeline if you are using rails 3 or above. Put your images in app/assets/images and call the rails functions that gives you url to the assets e.g. <%= image_tag "rails.png" %>.
For more detail read the following guideline:
http://guides.rubyonrails.org/asset_pipeline.html

Rails 3.2 Paperclip S3 Not Loading in Localhost

I just upgraded to Rails 3.2 and I'm using Amazon S3 with Paperclip to upload photos to my app.
Before my Image Urls would be:
http://s3.amazonaws.com/dealphotos.website.com/photos/428/large/Sandisk120Drive?1334754504
Now my Image Urls on Localhost are:
http://s3.amazonaws.com/dealphotos.website.com/deals/photos/000/000/428/large/Sandisk120Drive?1334754504
Notice the additional 000/000's - even if I take them out and visit the link it says:
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>"Long String of Numbers"</RequestId>
<HostId>
"Gives me a really long string of letters and numbers"
</HostId>
My Deal Model:
has_attached_file :photo, :styles => { :small =>"268x160>", :large =>"350x250>" },
:storage => :s3,
:bucket => 'dealphotos.website.com',
:s3_credentials => {
:access_key_id => ENV['S3_KEY_SPICY'],
:secret_access_key => ENV['S3_SECRET_SPICY']
}
And the images dont show up!
Before all I had to do was pull from Heroku and all my images and files would go to my development. What's going on?
You are most likely using the latest version of Paperclip (version >= 3).
Since this is a major version of paperclip there are documented incompatibilities with older versions of paperclip.
One MAJOR (and in my books good) change is that the default storage location of assets has changed.
Earlier it used to be /system/:attachment/:id/:style/:filename.:extension (from memory). This would be disastrous if you had 2 attachments with the same name (Company logo and Product logo) for example.
Now the new path used by paperclip is /system/:class/:attachment/:id_partition/:style/:filename.:extension
This means all your assets will be stored in the directory specified by above. :id_partitions are used so that the number of nodes (files/directories) in one directory doesnt exceed 1000.
I hope i've been able to explain the reason WHY you are seeing the problem. The default path has changed.
You have 2 options now -
Move old assets into the correct place in the new directory scheme (painful but recommended).
Move new assets into the old directory structure and add a :url + :path option in your attachment definition in the model to continue using the older scheme. (See https://github.com/thoughtbot/paperclip/blob/master/UPGRADING ).
Since it appears that you have images in BOTH the new structure and the old one. So no matter what you decide files will need to be moved from one structure to another. Unless the assets are bookmarked in some way, I suggest you move the older assets into the new directory structure.
Check out CarrierWave gem to file uploads. It works with 3.2 without any problems ;)
http://railscasts.com/episodes/253-carrierwave-file-uploads

Paperclip won't display/detect file size of anything that is not an image

I'm working on an app that allows users to upload videos, pictures, audio clips, and documents. The files are being uploaded with the Paperclip gem and being stored on s3.
Right now I am running some processing on the file sizes after they are uploaded, but paperclip continues to save the file to the database with a file size of '0' on any file that is not .jpg, .gif, .png. I've tried inspecting the file size myself, the upload's length, and several other hair brained ideas.
Is there anyway to monkeypatch, or hijack the request from paperclip to find out what the file size is? Has anyone run into this issue before?
By default, Paperclip is built to process images. If you need to be able to handle other file types, you'll have to look into writing your own Processor: https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/processor.rb. This will allow you to process files in whatever way you need when declared in your model:
has_attached_file :scan, :styles => { :text => { :quality => :better } },
:processors => [:ocr]
Here's an important point from the README docs:
NOTE: Because processors operate by turning the original attachment
into the styles, no processors will be run if there are no styles
defined.
This means you'll need to specify some sort of styles in order for your processor or any other processor to get run. For a basic custom processor something as simple as :styles => { :default => true } should be enough to get you going.

Uploading a file to a directory outside of RAILS ROOT with attachment_fu

I'm trying to use attachment_fu to upload files to a directory outside of the RAILS_ROOT. I want the files to be saved to ~/APP_NAME/uploads/ so that they can be approved/rejected before becoming publicly available. I've tried the following configuration for has_attachment:
has_attachment :storage => :file_system,
:path_prefix => "~/APP_NAME/uploads/",
:max_size => 5.megabytes
Unfortunately, this configuration simply creates the ~/APP_NAME/uploads/ directory structure in RAILS_ROOT. Any way to save the file outside of RAILS_ROOT?
This probably isn't an Attachment-Fu issue, but rather how Ruby handles File I/O as well as how files are stored in Unix.
So for instance if your app lives in, say, ~/Users/ron/APP_NAME
If you change the above code:
:path_prefix => "~/APP_NAME/uploads/"
To:
:path_prefix => "../#{RAILS_ROOT}/uploads"
The files would be stored in a folder called "uploads" in ~/Users/ron/uploads. The "../" means one directory above the current Rails root. If you want to go up two directories, it would be "../../" and so on.
But that only addresses hierarchical navigation. If you wanted to tell Attachment-Fu to store files in a hardcoded directory in your filesystem, you could give it a file path such as "~/Users/ron/APP_NAME/uploads", but keep in mind hardcoding in a file path this way is brittle and could be a pain point in the future should your file storage requirements change.
Hope that helps.
I found an alternative method that suits me better than using relative pathnames. I added a method called full_filename to my attachment class:
class attachment < ActiveRecord::Base
def full_filename
return "/Users/ron/attachments/#{id}.#{file_format}"
end
end

Resources