So, like others on this list, I have started a picture store app. I have followed the directions in the "how to" section for file uploads, as well as the previous posters here. Basically, it seems as though it should be working, but the images become corrupt. I can view them, but they look awful. I tried resizing and formatting with RMagick (really cool extention), but it complained about the files I was sending it. So now I''m pretty sure that either ruby/rails is corrupting the files, or mysql. Anyone having similar issues? Thanks, Berndt MySql.dump CREATE TABLE `photos` ( `id` int(11) unsigned NOT NULL auto_increment, `name` varchar(127) default NULL, `size` int(11) default NULL, `mime` varchar(127) default NULL, `picture` mediumblob NOT NULL, PRIMARY KEY (`id`), KEY `name` (`name`) ) TYPE=MyISAM rails code: def create #render_text @params[''photo''][''tmp_file''].local_path @photo = Photo.new do |p| p.name = @params[''photo''][''tmp_file''].original_filename.gsub(/[^a-zA-Z0-9.]/, ''_'') p.size = @params[''photo''][''tmp_file''].size p.mime = @params[''photo''][''tmp_file''].content_type p.picture = @params[''photo''][''tmp_file''].read end @params.delete("photo" => "tmp_file") if @photo.save redirect_to :action => "show/" + @photo.id.to_s else render "photo/edit" end end def show #@image = Photo.find(@params[''id'']) #send_data @image.picture, :filename => @image.name, :type => @image.mime, :disposition => "inline" @image = Photo.find(@params["id"]) @response.headers["Content-type"] = @image.mime render_text @image.picture end
I can''t take a look at it now but have a look at what I have. It may solve your probem. I don''t get corrupted files. I use it for any type of document not only images. ==== CREATE TABLE `documents` ( `id` int(11) NOT NULL auto_increment, `name` varchar(100) NOT NULL default '''', `size` float NOT NULL default ''0'', `last_mod` timestamp NOT NULL, `mime` varchar(100) NOT NULL default '''', `document` blob NOT NULL, PRIMARY KEY (`id`) ) TYPE=MyISAM DEFAULT CHARSET=latin1; ==== require ''abstract_application'' require ''document'' class DocumentController < AbstractApplicationController def index @page_title="Documents" @documents=Document.find_all end def create @document=Document.new do |d| d.name=@params["document"].original_filename.gsub(/[^a-zA-Z0-9.]/, ''_'') d.size=@params["document"].size d.mime=@params["document"].content_type d.document=@params["document"].read end @params.delete("document") @document.save redirect_to :action=>"index" end def destroy Document.find(@params["id"]).destroy redirect_to :action=>"index" end def download @document=Document.find(@params["id"]) @response.headers["Pragma"]="" @response.headers["Cache-Control"]="" @response.headers["Content-type"]=@document.mime @response.headers["Content-Disposition"]="attachment; filename=#{@document.name}" @response.headers["Accept-Ranges"]="bytes" @response.headers["Content-Length"]=@document.document.length @response.headers["Content-Transfer-Encoding"]="binary" @response.headers["Content-Description"]="File Transfer" render_text @document.document end def render_image @document=Document.find(@params["id"]) @response.headers["Content-type"]=@document.mime render_text @document.document end end ==== Berndt Jung wrote:> So, like others on this list, I have started a picture store app. I > have followed the directions in the "how to" section for file uploads, > as well as the previous posters here. Basically, it seems as though > it should be working, but the images become corrupt. I can view them, > but they look awful. I tried resizing and formatting with RMagick > (really cool extention), but it complained about the files I was > sending it. So now I''m pretty sure that either ruby/rails is > corrupting the files, or mysql. > > Anyone having similar issues? > > Thanks, > > Berndt > > MySql.dump > > CREATE TABLE `photos` ( > `id` int(11) unsigned NOT NULL auto_increment, > `name` varchar(127) default NULL, > `size` int(11) default NULL, > `mime` varchar(127) default NULL, > `picture` mediumblob NOT NULL, > PRIMARY KEY (`id`), > KEY `name` (`name`) > ) TYPE=MyISAM > > rails code: > > def create > #render_text @params[''photo''][''tmp_file''].local_path > @photo = Photo.new do |p| > p.name = @params[''photo''][''tmp_file''].original_filename.gsub(/[^a-zA-Z0-9.]/, > ''_'') > p.size = @params[''photo''][''tmp_file''].size > p.mime = @params[''photo''][''tmp_file''].content_type > p.picture = @params[''photo''][''tmp_file''].read > end > @params.delete("photo" => "tmp_file") > if @photo.save > redirect_to :action => "show/" + @photo.id.to_s > else > render "photo/edit" > end > end > > def show > #@image = Photo.find(@params[''id'']) > #send_data @image.picture, :filename => @image.name, :type => > @image.mime, :disposition => "inline" > @image = Photo.find(@params["id"]) > @response.headers["Content-type"] = @image.mime > render_text @image.picture > end > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
MCollins-zDcO7zQdTYmFJhaAjo67XZ9G+ZOsUmrO@public.gmane.org
2005-Jan-04 20:41 UTC
Re: Image upload problems
i''m new at rails and ruby so i haven''t really seen this how-to on file uploads; however, on a different subject away from the corrupted files, it appears you''re storing the actual images in the database as a blob? may i ask why? i''m used to storing the images on the filesystem and putting the path to that image in the database. tends to save on the database size Berndt Jung wrote:> So, like others on this list, I have started a picture store app. I > have followed the directions in the "how to" section for file uploads, > as well as the previous posters here. Basically, it seems as though > it should be working, but the images become corrupt. I can view them, > but they look awful. I tried resizing and formatting with RMagick > (really cool extention), but it complained about the files I was > sending it. So now I''m pretty sure that either ruby/rails is > corrupting the files, or mysql. > > Anyone having similar issues? > > Thanks, > > Berndt > > MySql.dump > > CREATE TABLE `photos` ( > `id` int(11) unsigned NOT NULL auto_increment, > `name` varchar(127) default NULL, > `size` int(11) default NULL, > `mime` varchar(127) default NULL, > `picture` mediumblob NOT NULL, > PRIMARY KEY (`id`), > KEY `name` (`name`) > ) TYPE=MyISAM > > rails code: > > def create > #render_text @params[''photo''][''tmp_file''].local_path > @photo = Photo.new do |p| > p.name =@params[''photo''][''tmp_file''].original_filename.gsub(/[^a-zA-Z0-9.]/,> ''_'') > p.size = @params[''photo''][''tmp_file''].size > p.mime = @params[''photo''][''tmp_file''].content_type > p.picture = @params[''photo''][''tmp_file''].read > end > @params.delete("photo" => "tmp_file") > if @photo.save > redirect_to :action => "show/" + @photo.id.to_s > else > render "photo/edit" > end > end > > def show > #@image = Photo.find(@params[''id'']) > #send_data @image.picture, :filename => @image.name, :type => > @image.mime, :disposition => "inline" > @image = Photo.find(@params["id"]) > @response.headers["Content-type"] =@image.mime> render_text @image.picture > end > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails --****Florida has a very broad Public Records Law. Virtually all written communications to or from State and Local Officials and employees are public records available to the public and media upon request. Seminole County policy does not differentiate between personal and business emails. E-mail sent on the County system will be considered public and will only be withheld from disclosure if deemed confidential pursuant to State Law.**** _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
In my case, I just wanted to keep it all together in the db. No particular reason for this. MCollins-zDcO7zQdTYmFJhaAjo67XZ9G+ZOsUmrO@public.gmane.org wrote:> > i''m new at rails and ruby so i haven''t really seen this how-to on file > uploads; however, on a different subject away from the corrupted files, > it appears you''re storing the actual images in the database as a blob? > may i ask why? i''m used to storing the images on the filesystem and > putting the path to that image in the database. tends to save on the > database size > > > > > > Berndt Jung wrote: > > So, like others on this list, I have started a picture store app. I > > have followed the directions in the "how to" section for file uploads, > > as well as the previous posters here. Basically, it seems as though > > it should be working, but the images become corrupt. I can view them, > > but they look awful. I tried resizing and formatting with RMagick > > (really cool extention), but it complained about the files I was > > sending it. So now I''m pretty sure that either ruby/rails is > > corrupting the files, or mysql. > > > > Anyone having similar issues? > > > > Thanks, > > > > Berndt > > > > MySql.dump > > > > CREATE TABLE `photos` ( > > `id` int(11) unsigned NOT NULL auto_increment, > > `name` varchar(127) default NULL, > > `size` int(11) default NULL, > > `mime` varchar(127) default NULL, > > `picture` mediumblob NOT NULL, > > PRIMARY KEY (`id`), > > KEY `name` (`name`) > > ) TYPE=MyISAM > > > > rails code: > > > > def create > > #render_text @params[''photo''][''tmp_file''].local_path > > @photo = Photo.new do |p| > > p.name = > @params[''photo''][''tmp_file''].original_filename.gsub(/[^a-zA-Z0-9.]/, > > ''_'') > > p.size = @params[''photo''][''tmp_file''].size > > p.mime = @params[''photo''][''tmp_file''].content_type > > p.picture = @params[''photo''][''tmp_file''].read > > end > > @params.delete("photo" => "tmp_file") > > if @photo.save > > redirect_to :action => "show/" + @photo.id.to_s > > else > > render "photo/edit" > > end > > end > > > > def show > > #@image = Photo.find(@params[''id'']) > > #send_data @image.picture, :filename => @image.name, :type => > > @image.mime, :disposition => "inline" > > @image = Photo.find(@params["id"]) > > @response.headers["Content-type"] = > @image.mime > > render_text @image.picture > > end > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > --****Florida has a very broad Public Records Law. Virtually all written > communications to or from State and Local Officials and employees are > public records available to the public and media upon request. Seminole > County policy does not differentiate between personal and business > emails. E-mail sent on the County system will be considered public and > will only be withheld from disclosure if deemed confidential pursuant to > State Law.**** > > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
I have seen cases where images (JPEGs, specifically) from some digital cameras have troubles being read by various open source image packages (in my case it was libgd). Something about the image''s footer not being quite right. I''m not sure this is even remotely related to your problem, but it might be something to check out. Here''s some code that fixed it in PHP (which should be relatively easy to port to ruby -- I just haven''t done it): function fixJPEG($jpegFile) { $fp = fopen($jpegFile, ''r+b''); if (fread($fp, 2) == (chr(255) . chr(216))) { fseek($fp, -2, SEEK_END); if (fread($fp, 2) == (chr(255) . chr(217))) { fclose($fp); } else { fwrite($fp, (chr(255) . chr(217))); fclose($fp); } } } If you don''t know PHP well enough to understand what that does, write me back and I''ll do a rewrite in ruby (although not until later... don''t have time to write and test it right now). Cheers, Ben On Tue, 4 Jan 2005 11:41:47 -0800, Berndt Jung <berndtj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So, like others on this list, I have started a picture store app. I > have followed the directions in the "how to" section for file uploads, > as well as the previous posters here. Basically, it seems as though > it should be working, but the images become corrupt. I can view them, > but they look awful. I tried resizing and formatting with RMagick > (really cool extention), but it complained about the files I was > sending it. So now I''m pretty sure that either ruby/rails is > corrupting the files, or mysql. > > Anyone having similar issues? > > Thanks, > > Berndt > > MySql.dump > > CREATE TABLE `photos` ( > `id` int(11) unsigned NOT NULL auto_increment, > `name` varchar(127) default NULL, > `size` int(11) default NULL, > `mime` varchar(127) default NULL, > `picture` mediumblob NOT NULL, > PRIMARY KEY (`id`), > KEY `name` (`name`) > ) TYPE=MyISAM > > rails code: > > def create > #render_text @params[''photo''][''tmp_file''].local_path > @photo = Photo.new do |p| > p.name = @params[''photo''][''tmp_file''].original_filename.gsub(/[^a-zA-Z0-9.]/, > ''_'') > p.size = @params[''photo''][''tmp_file''].size > p.mime = @params[''photo''][''tmp_file''].content_type > p.picture = @params[''photo''][''tmp_file''].read > end > @params.delete("photo" => "tmp_file") > if @photo.save > redirect_to :action => "show/" + @photo.id.to_s > else > render "photo/edit" > end > end > > def show > #@image = Photo.find(@params[''id'']) > #send_data @image.picture, :filename => @image.name, :type => > @image.mime, :disposition => "inline" > @image = Photo.find(@params["id"]) > @response.headers["Content-type"] = @image.mime > render_text @image.picture > end > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Hi Berndt,> So, like others on this list, I have started a picture store app. I > have followed the directions in the "how to" section for file uploads, > as well as the previous posters here. Basically, it seems as though > it should be working, but the images become corrupt. I can view them,There as until recently (0.9.2, I believe) a bug in the MySQL driver, which would cause binary data to be corrupted when transferred to MySQL. Make sure you''re on the latest version. Another thing to check: Make sure your BLOB field is big enough. Mediumblob holds 16Mb, so that should be alright. Finally, I noticed this in your code:> @params.delete("photo" => "tmp_file")This shouldn''t do any harm, but I''m not sure what good it does, either. What did you intend it to do?> redirect_to :action => "show/" + @photo.id.to_sThis can be tightened up a bit, by saying redirect_to :action => "show", :id => @photo.id Good luck, /Lars
Its the mysql ruby driver and its fixed in the 0.9.3 you should do gem update to get the latest rails and then gem install mysql to get the native c based mysql extensions which are faster. Cheers! On Tue, 4 Jan 2005 11:41:47 -0800, Berndt Jung <berndtj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I tried resizing and formatting with RMagick > (really cool extention), but it complained about the files I was > sending it. So now I''m pretty sure that either ruby/rails is > corrupting the files, or mysql.-- Tobi http://www.hieraki.org - Open source book authoring http://blog.leetsoft.com - Technical weblog
Thanks for all the help. I am indeed running rails 0.9.1, so I''m going to go ahead and blame it on the driver for now. I thought about just storing the path, and saving the file to a directory, but I couldn''t see any reason not to just store the whole file. Is there a common practice rule? I do have code writen in PHP that works as well, but it wasn''t helping me out. I tried a couple different routes with no luck. It could very well be the driver. I just like writing ruby code more than PHP, so I''m trying to stay exclusively Ruby. Now I just need to get it sped up. This was my first post. Thanks for all the help. It''s good to know that there is a community around this. Berndt On Tue, 4 Jan 2005 20:53:33 -0500, Tobias Luetke <tobias.luetke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Its the mysql ruby driver and its fixed in the 0.9.3 > > you should do gem update to get the latest rails and then gem install > mysql to get the native c based mysql extensions which are faster. > > Cheers! > > On Tue, 4 Jan 2005 11:41:47 -0800, Berndt Jung <berndtj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I tried resizing and formatting with RMagick > > (really cool extention), but it complained about the files I was > > sending it. So now I''m pretty sure that either ruby/rails is > > corrupting the files, or mysql. > > > -- > Tobi > http://www.hieraki.org - Open source book authoring > http://blog.leetsoft.com - Technical weblog >
I''m storing the files in the DB in my own application and much like you I run them through RMagick after the upload. I added some new unit tests to AR which verify that binary support works so you can be pretty sure that binary will always be supported well from this point on. Please update to the latest rails version, you can read about it here: http://weblog.rubyonrails.com/archives/2005/01/04/rails-093-optimistic-locking-dynamic-finders-ruby-182/ As described in this weblog post you will have to do some minor changes to your environment files but 0.9.3 is a whole lot more solid than 0.9.1. Hope this helps. -- Tobi http://www.hieraki.org - Open source book authoring http://blog.leetsoft.com - Technical weblog
Thanks to everyone! I just updated to 0.9.3, and everything works like a charm. It was indeed the binary file bug in 0.9.2. On Wed, 5 Jan 2005 00:11:03 -0500, Tobias Luetke <tobias.luetke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m storing the files in the DB in my own application and much like you I run > them through RMagick after the upload. > > I added some new unit tests to AR which verify that binary support works so you > can be pretty sure that binary will always be supported well from this point on. > > Please update to the latest rails version, you can read about it here: > http://weblog.rubyonrails.com/archives/2005/01/04/rails-093-optimistic-locking-dynamic-finders-ruby-182/ > > As described in this weblog post you will have to do some minor > changes to your environment files but 0.9.3 is a whole lot more solid > than 0.9.1. > > Hope this helps. > -- > Tobi > http://www.hieraki.org - Open source book authoring > http://blog.leetsoft.com - Technical weblog >
On 5.1.2005, at 03:53, Tobias Luetke wrote:> Its the mysql ruby driver and its fixed in the 0.9.3 > > you should do gem update to get the latest rails and then gem install > mysql to get the native c based mysql extensions which are faster.I get this when trying to install the mysql driver: Jarkko-Laines-Computer:~/ jarkko$ sudo gem install mysql Attempting local installation of ''mysql'' Local gem file not found: mysql*.gem Attempting remote installation of ''mysql'' Building native extensions. This could take a while... ERROR: While executing gem ... (RuntimeError) ERROR: Failed to build gem native extension. Gem files will remain installed in /usr/local/lib/ruby/gems/1.8/gems/mysql-2.5.1 for inspection. ruby extconf.rb install mysql\nchecking for mysql_query() in -lmysqlclient... no checking for main() in -lm... yes checking for mysql_query() in -lmysqlclient... no checking for main() in -lz... yes checking for mysql_query() in -lmysqlclient... no checking for main() in -lsocket... no checking for mysql_query() in -lmysqlclient... no checking for main() in -lnsl... no checking for mysql_query() in -lmysqlclient... no Results logged to /usr/local/lib/ruby/gems/1.8/gems/mysql-2.5.1/gem_make.out Any ideas what''s causing this? //jarkko -- Jarkko Laine http://jlaine.net _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Just an fyi, I''m also getting the exact same problem. -evan On Wed, 5 Jan 2005 11:19:10 +0200, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote:> > On 5.1.2005, at 03:53, Tobias Luetke wrote: > > > Its the mysql ruby driver and its fixed in the 0.9.3 > > > > you should do gem update to get the latest rails and then gem install > > mysql to get the native c based mysql extensions which are faster. > > I get this when trying to install the mysql driver: > > Jarkko-Laines-Computer:~/ jarkko$ sudo gem install mysql > Attempting local installation of ''mysql'' > Local gem file not found: mysql*.gem > Attempting remote installation of ''mysql'' > Building native extensions. This could take a while... > ERROR: While executing gem ... (RuntimeError) > ERROR: Failed to build gem native extension. > Gem files will remain installed in > /usr/local/lib/ruby/gems/1.8/gems/mysql-2.5.1 for inspection. > ruby extconf.rb install mysql\nchecking for mysql_query() in > -lmysqlclient... no > checking for main() in -lm... yes > checking for mysql_query() in -lmysqlclient... no > checking for main() in -lz... yes > checking for mysql_query() in -lmysqlclient... no > checking for main() in -lsocket... no > checking for mysql_query() in -lmysqlclient... no > checking for main() in -lnsl... no > checking for mysql_query() in -lmysqlclient... no > > Results logged to > /usr/local/lib/ruby/gems/1.8/gems/mysql-2.5.1/gem_make.out > > Any ideas what''s causing this? > > //jarkko > > -- > Jarkko Laine > http://jlaine.net > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Jan 5, 2005, at 1:19 AM, Jarkko Laine wrote:> On 5.1.2005, at 03:53, Tobias Luetke wrote: > >> Its the mysql ruby driver and its fixed in the 0.9.3 >> you should do gem update to get the latest rails and then gem install >> mysql to get the native c based mysql extensions which are faster. > > I get this when trying to install the mysql driver: > > Jarkko-Laines-Computer:~/ jarkko$ sudo gem install mysql > Attempting local installation of ''mysql'' > Local gem file not found: mysql*.gem > Attempting remote installation of ''mysql'' > Building native extensions. This could take a while... > ERROR: While executing gem ... (RuntimeError) > ERROR: Failed to build gem native extension. > Gem files will remain installed in > /usr/local/lib/ruby/gems/1.8/gems/mysql-2.5.1 for inspection. > ruby extconf.rb install mysql\nchecking for mysql_query() in > -lmysqlclient... no > checking for main() in -lm... yes > checking for mysql_query() in -lmysqlclient... no > checking for main() in -lz... yes > checking for mysql_query() in -lmysqlclient... no > checking for main() in -lsocket... no > checking for mysql_query() in -lmysqlclient... no > checking for main() in -lnsl... no > checking for mysql_query() in -lmysqlclient... noIt looks like it''s not finding where you have mysql libs and h. Maybe try? gem install mysql -- --with-mysql-lib=/path/to/lib - --with-mysql-include=/path/to/include I used to get a similar error with sqlite and it just took passing the paths to it, gem install sqlite -- --with-sqlite-lib=/usr/local/lib - --with-sqlite-include=/usr/local/include I''ve never installed the mysql extensions from gems but with the old fashion way, it only takes when passing lib and include paths to it: ruby extconf.rb --with-mysql-include=/usr/local/include/mysql - --with-mysql-lib=/usr/local/lib/mysql Same with the fcgi extension: ruby install.rb config -- --with-fcgi-include=/usr/local/include - --with-fcgi-lib=/usr/local/lib - - Jason -----BEGIN PGP SIGNATURE----- Version: PGP 8.1 iQA/AwUBQdu4jVUyB+ajXkCLEQKOaACfRYY9MAodQxk49Eua4oYt8dGMcWwAn1S7 nGNPsmFaiF/lHECywQHFVjxA =0lOf -----END PGP SIGNATURE-----
Thanks, Jason, It did the trick: Jarkko-Laines-Computer:/sw/include/mysql jarkko$ sudo gem install mysql -- --with-mysql-lib=/sw/lib --with-mysql-include=/sw/include/mysql //jarkko -- Jarkko Laine http://jlaine.net _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails