Attachment_fu reports Size is not supported in the list on Windows Platforms

Source: Internet
Author: User

 

Migrate the rails application from the linux platform to windows2003, and convert the proxy server from lighttpd to iis. After trying it out, it is basically normal.

However, the background reports an error "Size is not supported ded in the list" When uploading images.

I checked it at noon. I thought it was because the size had exceeded the limit and found that it was not exceeded. I typed debugging information and found self. the size is 0, but the re-upload is successful. It should be because the file is saved incorrectly,

Google found that someone had solved the problem and used sleep 5 before the size. The solution was too casual.

 

There is another foreigner solution on blogpost, which is good. I hate gfw.

 

Size is not supported ded in the list

 

 

Fixing attachment_fu on Windows

Like developing others, I 've encountered issue when developing Rails applications using attachment_fu on Windows. After doing some research, I 've come up with the following solution to the problem.
The problem has two parts:

  1. Size is not supported ded in the listError message,
  2. Timeout error when uploading to S3.
Fixing "Size is not supported ded in the list"Error message

Some people have reported that there is a timing issue, when trying to get the file size, with Tempfile on Windows. it seems that the size of the file is not properly reported by Windows after writing data to it. proposed solutions for this problem include:

  1. Sleeping in a loop as long as the file size is 0,
  2. Reading back the entire file in memory.

I think I found a better and less patchy solution for this issue: forcing the OS to flush the file to disk before reading it's size.
Here is the code to do it:

 

require 'tempfile'class Tempfiledef sizeif @tmpfile@tmpfile.fsync # added this line@tmpfile.flush@tmpfile.stat.sizeelse0endendend

DoingFlushIs not enough...FlushWill flush the Ruby buffer but the file may not be immediately written to the disk by the OS. DoingFsyncEnsure that the file is written to disk by the OS before continuing. After that, Windows will properly report the actual file size.

Fixing the Timeout error when uploading to S3

This issue is related to opening files for reading on Windows. On Windows, you have to open the file in binary mode. So patchingAttachment_fuIs simple:

 
require 'technoweenie/attachment_fu/backends/s3_backend'module Technoweeniemodule AttachmentFumodule Backendsmodule S3Backendprotecteddef save_to_storageif save_attachment?S3Object.store(full_filename,(temp_path ? File.open(temp_path, "rb") : temp_data), # added , "rb"bucket_name,:content_type => content_type,:access => attachment_options[:s3_access])end@old_filename = niltrueendendendendendI've also included a fix from someone else (which was not enough in itself to solve my S3 upload problem):module Technoweeniemodule AttachmentFu# Gets the data from the latest temp file. This will read the file into memory.def temp_dataif save_attachment?f = File.new( temp_path )f.binmodereturn f.readelsereturn nilendendendend
Wrapping it up

So I put all this code inLib/attachment_fu_patch.rbAnd required it inEnvironment. rb.
Problem fixed!
Note, I did not test it on other OSes, but these fixes shocould not have any adverse effects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.