Topic: Size is not included in the list error with attachment_fu in Windows

I get "Size is not included in the list" error when i try to upload an image with attachment_fu even when the image size falls within the right size range.

Thoughts

thanks
Madhan

Re: Size is not included in the list error with attachment_fu in Windows

how are you specifiying the size?

this is from somthing I'm working on.

  has_attachment :content_type => :image, 
                 :storage => :s3,
                 :processor => 'Rmagick',
                 :max_size => 6.megabytes,
                 :resize_to => '500x500>',
                 :thumbnails => { :thumb => '150x150>' }

validates_as_attachment

--

Re: Size is not included in the list error with attachment_fu in Windows

Here is the code we have:
  has_attachment :content_type => :image,
                 :storage => :file_system,
                 :min_size => 1.kilobytes,
                 :max_size => 3.megabytes,
                 :resize_to => '640x480>',
                 :thumbnails => { :thumb => '80x80>', :tiny => '40x40>' },
                 :processor => :MiniMagick
                 
  validates_as_attachment

Re: Size is not included in the list error with attachment_fu in Windows

I've had the same issue on a UNIX system. Note that size in attachment_fu means file size, not image size.

If I leave out the standard "validates_as_attachment" I get no errors. The problem seems to be that using file_system storage, the file size according to the model always is zero until the actual save. I guess that attachment_fu tries to check the size versus the final file, whereas only a temporary file exists which isn't checked for the size (or something like that). So the validation fails before the file gets saved.

I haven't tracked this down in the attachement_fu code, this is simply from experimentation and observation...

Do other people use file_system storage and "validates_as_attachment" together successfully?

Last edited by Railspotter (2007-06-08 08:07:11)

Re: Size is not included in the list error with attachment_fu in Windows

I think you are right with the source of the issue. I have hacked the code in attachment_fu in couple of areas and things are working fine.

Here are the changes i have done:
      In uploaded_data i am setting the size based on the size of the blob thats passed in
     

      def uploaded_data=(file_data)
        return nil if file_data.nil? || file_data.size == 0
        self.size = file_data.size
        self.content_type = file_data.content_type
        self.filename     = file_data.original_filename if respond_to?(:filename)
        if file_data.is_a?(StringIO)
          file_data.rewind
          self.temp_data = file_data.read
        else
          self.temp_path = file_data.path
        end
      end

and added conditionals for the size
     
      def set_size_from_temp_path
          self.size = File.size(temp_path) if save_attachment? && (self.size == 0 || self.size.nil?)
      end

Re: Size is not included in the list error with attachment_fu in Windows

Yes, that made it work (even without the conditionals in set_size_from_temp_path).

I get the same error if I exceed :max_size, which is less than optimal (I'd have liked a more specific validation error). But I can live with that for now.

Thanks for sharing your solution!

Re: Size is not included in the list error with attachment_fu in Windows

Hi,

Other than the attachment_fu code fix mentioned above, I also noticed in your controller that handles image uploading, if you have your server run on Windows, it is essential to give a relatively long period of sleep time. I gave it a "sleep 5".

After trying out attachment_fu fix as well as tweaking the model a few times, I still once in a while ran into the same error message: Size is not included in the list. Then after experiment, I found out it occured when dealing with large image, say a 1600X1200 of more than 500k. On Windows, the server needs more time to catch the breadth. Unfortunately Rails doesn't wait if you don't tell it to do so. When time runs out, image saving operation fails as Rails sees it, which results in a nil size that causes the error message.

Just my 2 cents.

jay

Re: Size is not included in the list error with attachment_fu in Windows

Hi!
I'm on Windows, Ruby 1.8.6, Rails 2.2.2; I tried to apply your solution for attachment_fu and still have the "size zero" error; Here is my model:

class Document < ActiveRecord::Base
  has_one :db_file, :dependent=>:destroy
  has_attachment :size=> 0.kilobytes..10.megabytes
  validates_as_attachment
end

Migrations:
class CreateDocuments < ActiveRecord::Migration
  def self.up
    create_table :documents do |t|
      t.string :filename
      t.string :content_type
      t.integer :size
      t.integer :db_file_id

      t.timestamps
    end
  end

  def self.down
    drop_table :documents
  end
end
class CreateDbFiles < ActiveRecord::Migration
  def self.up
    create_table :db_files do |t|
      t.binary :data

      t.timestamps
    end
  end

  def self.down
    drop_table :db_files
  end
end


Controller:
class DocumentsController < ApplicationController

  def load_file
    document = Document.find(params[:id])
    db_file = document.db_file
    file_data = db_file.data
    send_data(file_data, :type => document.content_type, :filename => document.filename)
  end
def new
    @document = Document.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @document }
    end
  end
def create
    @document = Document.new(params[:document])

    respond_to do |format|
      if @document.save
        flash[:notice] = 'Document was successfully created.'
        format.html { redirect_to(documents_url) }
        format.xml  { render :xml => @document, :status => :created, :location => @document }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @document.errors, :status => :unprocessable_entity }
      end
    end
  end


View to create a new Document:
<h1>New document</h1>

<% form_for(@document, :html => { :multipart => true }) do |f| %>
  <%= f.error_messages %>

  <%= f.file_field :uploaded_data %>
  <p>
    <%= f.submit "Create" %>
  </p>
<% end %>

<%= link_to 'Back', documents_path %>


In vendor\plugins\attachment_fu\lib\technoweenie\attachment_fu.rb:
def uploaded_data=(file_data)
        return nil if file_data.nil? || file_data.size == 0
        self.content_type = file_data.content_type
        self.filename     = file_data.original_filename if respond_to?(:filename)
        if file_data.is_a?(StringIO)
          file_data.rewind
          self.temp_data = file_data.read
        else
          self.temp_path = file_data.path
        end
      end
def set_size_from_temp_path
          self.size = File.size(temp_path) if save_attachment? && (self.size == 0 || self.size.nil?)
        end

When I tries to save a PDF file of 2.4 Mb, it was truncated to 25 kB and it was saved its size set to zero. So when I tried to download it and open - no way ! Any idea?

Re: Size is not included in the list error with attachment_fu in Windows

Hello everyone,

I too kindda struck in the same problem, even after googling and implementing you rsolutions the problem is kindda same.
NoMethodError: undefined method `size' for #<File:#04353E> I tried all if and but cases but still the problem is the same and surpeisingly its working fine from browser. I guess uploaded_data is setting some more attributes when a file path is sent to it. My requirement is to upload thousand files right through the backend itself. But this issue is not letting me gear up.

i m trying the same way.
P = Photo.new(:uploaded_data => File.open("<path to imag>"))
NoMethodError: undefined method `size' for #<File:ds2934>

any ideas?? thoughts what can be the problem?? and of course whats the fix smile

Re: Size is not included in the list error with attachment_fu in Windows

I have the same problem.  Have you guys considered a different plugin like paperclip?

Re: Size is not included in the list error with attachment_fu in Windows

On Windows it used to be necessary to patch the tempfile class to fix a size zero error, not sure if this is still the case as I've been a Paperclip man for a long time now smile

require 'tempfile'
class Tempfile
  def size
    if @tmpfile
      @tmpfile.fsync
      @tmpfile.flush
      @tmpfile.stat.size
    else
      0
    end
  end
end
Rob Anderton
TheWebFellas

Re: Size is not included in the list error with attachment_fu in Windows

Darn.  I wish I had read that post before I switched to paperclip lol.

Re: Size is not included in the list error with attachment_fu in Windows

I'm trying to do the same kind of thing - pass files in that don't originate in a form. It works fine in Paperclip to just set the param to File.open(path) - ach!  why does attachment_fu have to complicate that. When I post something in from a makeshift form, the param looks like a regular File object like you'd get from File.open(path) - but it has the file and content_type methods already magically added.


I think I can patch in the file size, but it's taking a while to figure out where content type comes from. Unfortunately, it's a long established project, so I can't easily switch to Paperclip to make the problem go away.

If anyone figures out a concise way to include an attachment that didn't come from a form, please post it.  THANKS

Re: Size is not included in the list error with attachment_fu in Windows

gakshay wrote:

NoMethodError: undefined method `size' for #<File:#04353E>

Next was my solution:

      Image.create!(
        :owner_id => object.id,
        :owner_type => object.class.name,
        :type => 'Image',
        :filename => filename,
        :content_type => File.mime_type?(filepath),
        :temp_data => File.open(filepath).read
     )
So instead of "uploaded_data" I sent :filename, :content_type and :temp_data manualy

Last edited by elias (2010-12-25 10:32:31)

Re: Size is not included in the list error with attachment_fu in Windows

looks like the problem is that one has

validates_as_attachment

I'm not sure what that implies, but it has to do with size smile

I got around it by creating this fake upload object:


               new_image = Image.new
               fake_upload = Pathname.new('/tmp/music.gif')
               def fake_upload.content_type
                'image/gif'
               end
               def fake_upload.original_filename
                'music.gif'
               end
               new_image.uploaded_data = fake_upload

Re: Size is not included in the list error with attachment_fu in Windows

The above issue is very known and has been already solved by implementing tempfile patch.

Re: Size is not included in the list error with attachment_fu in Windows

No, this was caused for me *with* the tempfile patch.

It turns out (for followers) that this can also mean that the size is too small or too large (by default 0 bytes is too small, > 1 MB is too large or what not).

Re: Size is not included in the list error with attachment_fu in Windows

Fixing "Size is not included in the list" error message. Some people have reported that there is a timing issue, when trying to get the file  size.

Last edited by gregorv (2012-06-19 09:57:31)