tag:blogger.com,1999:blog-35868762.post108193128319595005..comments2023-05-16T07:56:56.864-07:00Comments on Dead Programmer Society: Getting Your attachment_fu Back Out Of The DatabaseRon Evanshttp://www.blogger.com/profile/06754261780924273552noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-35868762.post-38377002481383148712008-12-03T07:09:00.000-08:002008-12-03T07:09:00.000-08:00Thx for your response. This time I tried by adding...Thx for your response. This time I tried by adding this method in my model,<BR/><BR/> def image_data(thumbnail = nil)<BR/> if thumbnail.nil?<BR/> current_data<BR/> else<BR/> thumbnails.find_by_thumbnail(thumbnail.to_s).current_data<BR/> end<BR/> end<BR/><BR/>and in controller I tried to access like,<BR/><BR/> def show<BR/> @photo = Photo.find(params[:id])<BR/><BR/> respond_to do |format|<BR/> format.html { render :action => 'show', :layout => false }<BR/> format.png { send_data(@photo.image_data(:thumb),<BR/> :type => @photo.content_type,<BR/> :filename => @photo.filename,<BR/> :disposition => 'inline') }<BR/><BR/> format.jpg { send_data(@photo.image_data(show_thumbnail),<BR/> :type => 'image/jpeg',<BR/> :filename => @photo.filename,<BR/> :disposition => 'inline') }<BR/> format.gif { send_data(@photo.image_data(:thumb),<BR/> :type => 'image/gif',<BR/> :filename => @photo.filename ,<BR/> :disposition => 'inline') }<BR/> end<BR/> #rescue<BR/> # flash[:warning] = 'Could not find image.'<BR/> # redirect_to '/'<BR/> end<BR/><BR/><BR/>Images are getting stored in the db without any issues. But when I try to view the image like this,<BR/><BR/>http://localhost/photos/3.gif (or)1.png<BR/><BR/>I get empty images. <BR/><BR/>when I access the corresponding 'thumb' image (2.png), I see,<BR/><BR/>nil.current_data Error.<BR/><BR/>Please help.L.J.Arthur Neilhttps://www.blogger.com/profile/01672312737284073248noreply@blogger.comtag:blogger.com,1999:blog-35868762.post-78588908877567965032008-12-02T09:32:00.000-08:002008-12-02T09:32:00.000-08:00ActiveRecord is already able to retrieve the BLOB ...ActiveRecord is already able to retrieve the BLOB data from database just by referring to the currect attribute. Attachment_fu is doing some little bit of magic as far as deciding which record to grab the data from (based on the requested size) but AR 'just works' at grabbing the binary data. Hence the example shows the image_data method using the 'current_data' method of the attachment object, instead to going directly to the db_files data field.<BR/><BR/>The other part is making your application serve up the correct mime type and return the binary data. You need a separate controller action or else use 'respond_to' to determine what type to return to the browser.Ron Evanshttps://www.blogger.com/profile/06754261780924273552noreply@blogger.comtag:blogger.com,1999:blog-35868762.post-4879751033266135422008-12-02T08:13:00.000-08:002008-12-02T08:13:00.000-08:00This comment has been removed by the author.L.J.Arthur Neilhttps://www.blogger.com/profile/01672312737284073248noreply@blogger.comtag:blogger.com,1999:blog-35868762.post-87203497606612240972008-12-02T08:12:00.000-08:002008-12-02T08:12:00.000-08:00Hi, Can you please explain in-detail how to retr...Hi,<BR/> Can you please explain in-detail how to retrieve the BLOB data from db_files table?L.J.Arthur Neilhttps://www.blogger.com/profile/01672312737284073248noreply@blogger.comtag:blogger.com,1999:blog-35868762.post-38997860146992476312008-01-14T03:35:00.000-08:002008-01-14T03:35:00.000-08:00I found that I needed to do a definition of the db...I found that I needed to do a definition of the db_files table like this:<BR/><BR/> create_table :db_files do |t|<BR/> end<BR/> execute 'ALTER TABLE db_files ADD COLUMN data LONGBLOB'<BR/><BR/>otherwise the column maxes out at 65535Phil Smyhttps://www.blogger.com/profile/02013604704411178928noreply@blogger.comtag:blogger.com,1999:blog-35868762.post-85254562590287427072007-12-14T08:44:00.000-08:002007-12-14T08:44:00.000-08:00I can upload the image to MySQL just fine; I have ...I can upload the image to MySQL just fine; I have two tables: photos and db_files, where I see the binary data.<BR/><BR/>I'm now trying to render the thumbnails as links to the original images, and I'm stuck. <BR/><BR/>Is there a complete sample somewhere that shows how to do this? I'm lost when it comes to getting the images back out of the db.Mark A. Richmanhttps://www.blogger.com/profile/12037499808213749544noreply@blogger.comtag:blogger.com,1999:blog-35868762.post-4297989506808327112007-07-04T03:26:00.000-07:002007-07-04T03:26:00.000-07:00Ron, I think you also need to mention this:for 'fo...Ron, I think you also need to mention this:<BR/>for 'format.jpg' to work in the RESTful controller, you have to register the MIME type in environment.rb:<BR/><BR/>Mime::Type.register "image/jpeg", :jpgNilesh Chttps://www.blogger.com/profile/07259023555461462913noreply@blogger.comtag:blogger.com,1999:blog-35868762.post-44858444377474163482007-06-27T13:39:00.000-07:002007-06-27T13:39:00.000-07:00Ron,Do you recall what the "small process" you had...Ron,<BR/><BR/>Do you recall what the "small process" you had to go through to get FreeImage and ImageScience working was? Ryan Raaum is off the grid lately, and I can't find any help for this. Much appreciated.Anonymoushttps://www.blogger.com/profile/11395906038913554785noreply@blogger.comtag:blogger.com,1999:blog-35868762.post-25461403228073319292007-05-10T07:26:00.000-07:002007-05-10T07:26:00.000-07:00You are correct, Luis, that attachment_fu should p...You are correct, Luis, that attachment_fu should probably already handle this. I will try to submit a patch to technoweenie this weekend.<BR/><BR/>With regards to the hardcoding for JPG format, I had actually already changed my controller implementation as follows:<BR/><BR/>format.jpg do<BR/> image = @picture.image_data(:thumb) <BR/> send_data (image, :type => @picture.content_type,<BR/> :filename => @picture.filename,<BR/> :disposition => 'inline')<BR/>end<BR/><BR/>This will return the correct content type for the image that was stored. If you do not like asking for a JPG and getting back an PNG, you could always create your own custom MIME type like "IMAGE" for something like that.Ron Evanshttps://www.blogger.com/profile/06754261780924273552noreply@blogger.comtag:blogger.com,1999:blog-35868762.post-68176991804269656542007-04-29T01:01:00.000-07:002007-04-29T01:01:00.000-07:00Nice workaround, but that should be handled by Att...Nice workaround, but that should be handled by AttachmentFu and not you.<BR/><BR/>You're hardcoding .jpg as format, so any other attached image (gif, png?) will require evaluate you the attachment format/extension. Not DRY :-P<BR/><BR/>Regarding TempFile, I have a few issues with it on Windows, still patching, but attachment_fu worth the effort ;-)<BR/><BR/>Regards,<BR/><BR/>L.Luishttps://www.blogger.com/profile/13019468621368759952noreply@blogger.com