Strange file upload error

Hi There,

So we are having this really strange file upload error. We can upload a
file to rails, store it in the DB, and download it again all fine.

We also have things set up to allow us to edit the file, and upload a
new one to replace the existing file. This functionality works just fine
for text files, but fails for microsoft word and PDF files.

It’s really strange; we’re checking the MD5Sum for the files, and when
we do a re-upload of a text file the MD5Sum stays exactly the same, but
for pdf and doc files it changes.

We’ve checked the temporary file that gets stored by apache and that
seems fine, and checked the SQL that active record generates and here a
different file data (of exactly the same length) is being inserted when
the update takes place. So it seems like between CGI.rb processing the
the temp file from Apache, and the creation of the insert by active
record something screws up, but only on a reload, and only for cetain
file types.

I’ve now written a unit test that checks both against a text file reload
and an msword file reload, and they both work fine, so we are kind of
stumped as to what’s causing this.

You can see the code we’re using for the record model (which maps to the
file table in the db) here:

http://lilt.ics.hawaii.edu/cgi-bin/cvstrac.cgi/prometheus/getfile/prometheus/app/models/record.rb?v=1.9

The key methods are Record.meta_data and Record.load. Record.load gets
called when a new file is uploaded, and it in turn calls
Record.meta_data to get all the meta data related to the file. When the
file is re-loaded only Record.meta_data gets called, but in both cases
we use update_attributes() to get the data into the db.

We’re really stuck trying to work out why this fails only for doc/pdf
files on a reload.

This is my check in of the test code:

http://lilt.ics.hawaii.edu/cgi-bin/cvstrac.cgi/prometheus/chngview?cn=737

which runs just fine. It’s just when we run through the rails app that
we run into trouble.

Many thanks in advance to anyone who can shed light on this problem.

CHEERS> SAM

Just a note to say we fixed this problem. Using a series of unit and
functional tests we worked out that the following code which attempted
to handle the different StringIO and FileIO coming from the file form

if !@params[‘tmp_file’].nil? and !@params[‘tmp_file’].read(1).nil?
@record.update_attributes(Record.meta_data(@params[‘tmp_file’]))
end

was screwing things up and that what we needed instead was:

if !@params[‘tmp_file’].nil? and !@params[‘tmp_file’].is_a?(StringIO)
@record.update_attributes(Record.meta_data(@params[‘tmp_file’]))
end

Took a while to track that down.

CHEERS> SAM

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs