views:

22

answers:

1

Hi, I'm using RubyZip to compress a set of images (uploaded using Paperclip) and allow the user to download them in one file, and all works fine until I come to open an image. It wont display, and trying on Ubuntu I get the error message:

 "Error interpreting JPEG image file (Not a JPEG file: starts with 0x89..."

So the user is downloading a folder, populated by files with the correct usernames, but which upon opening cannot be displayed because the computer can't display their "format".

controller:

 def zip

  @product = Product.find(params[:id])
  t = Tempfile.new(@product.random+rand(200).to_s)
  Zip::ZipOutputStream.open(t.path) do |z|
    @product.assets.each do |img|
        img_path = "#{RAILS_ROOT}"+"/public"+img.data.url(:original)
        file = File.open(img_path.split('?')[0])

        z.put_next_entry(img.id.to_s+"_original.jpg")
        z.print IO.read(file.path)
    end
  end
send_file t.path, :type => 'application/zip', :disposition => 'attachment', :filename => "#{@product.random}-#{rand(9999).to_s}.zip"

 end

Thanks!

A: 

0x89 means it's a PNG. Either it's being converted by your process, or it wasn't a JPEG to begin with.

Ignacio Vazquez-Abrams
I'm not sure if it is an 0x89 (I assume it is), but the error message is being cut off.The uploaded files are JPGs, but changing the file extension to PNG in the controller code gives a new error message: "PNG file corrupted by ASCII conversion"...
Steve F
And what does looking at the files on the server result in?
Ignacio Vazquez-Abrams
No, never mind, the file on the server is probably fine. You need to open the file for reading in binary mode or you will corrupt it.
Ignacio Vazquez-Abrams
No problems with displaying them on the server as JPGs, renders as per usual. Just the corruption error. Filesizes look pretty small as well...
Steve F
"As JPGs" or "at all"? How sure are you that they're JPEGs?
Ignacio Vazquez-Abrams
The file extension of the pictures on my machine and on the server display is JPG. I'm not sure if that's "proof" or whether the real filetype is obscured somehow.
Steve F
Now McAfee is blocking the downloaded images because of the Exploit-QtPICT trojan, with Ubuntu's error message being "Bogus Huffman table definition"...
Steve F