Code

Opened 16 months ago

Closed 16 months ago

Last modified 16 months ago

#20020 closed Bug (invalid)

ValueError for FileField if the file is closed in clean method

Reported by: whitews@… Owned by: nobody
Component: File uploads/storage Version: 1.4
Severity: Normal Keywords: FileField ValueError
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi,

Say you have a Model with a FileField, and you need to override clean to do some validation on the file before it is saved. If the file is closed within clean and the file is over 2.5MB (or larger than FILE_UPLOAD_MAX_MEMORY_SIZE) a "ValueError: I/O operation on closed file" will result. If you attempt to re-open the file in clean to avoid this error a different ValueError will result stating "The file cannot be reopened." This is particularly a problem if the verification of a file uses some other library that may open and close the file.

I'm using Django 1.4.3, and have seen the issue under the Django web development server as well as under Apache w/mod_wsgi. Here's an example models.py to illustrate the issue:

from string import join
from django.db import models
from FileUploadExample.settings import MEDIA_ROOT

def some_file_operation(file):
    file.open()
    file.close()

def upload_file_path(instance, filename):
    upload_dir = join([MEDIA_ROOT, str(filename)], '')
    return upload_dir

class UploadedFile(models.Model):
    uploaded_file = models.FileField(upload_to=upload_file_path)

    def clean(self):
        # some call to somewhere to validate the file
        some_file_operation(self.uploaded_file)

Attachments (0)

Change History (5)

comment:1 Changed 16 months ago by whitews@…

  • Keywords FileField ValueError added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Type changed from Uncategorized to Bug

comment:2 Changed 16 months ago by aaugustin

  • Resolution set to invalid
  • Status changed from new to closed

TemporaryUploadedFile relies on Python's tempfile.NamedTemporaryFile to make sure the temporary file will be eventually deleted; in fact, it's deleted as soon as it's closed.

That's why you cannot reopen it, and that isn't going to change — unless you come up with another way to guarantee that the temporary file gets deleted eventually.

At least on Unix it seems that the file can be re-opened by name. Use self.uploaded_file.temporary_file_path.

comment:3 Changed 16 months ago by whitews@…

I'm not quite following, if it is deleted how can the file be re-opened?

This is the issue I am seeing, once the file is closed there is no way to proceed to save the model instance.

comment:4 Changed 16 months ago by aaugustin

Sorry I wasn't clear -- you can re-open it by name as long as you haven't closed the original.

comment:5 Changed 16 months ago by whitews@…

Got it, this works. I just create another file handle to give to a routine that closes files. Thanks

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.