#11027: Storage backends should know about the max_length attribute of the
FileFields
------------------------------------+--------------------------------------
               Reporter:  apollo13  |          Owner:  nobody
                   Type:  Bug       |         Status:  new
              Milestone:  1.3       |      Component:  File uploads/storage
                Version:  SVN       |       Severity:  Normal
             Resolution:            |       Keywords:
           Triage Stage:  Accepted  |      Has patch:  0
    Needs documentation:  0         |    Needs tests:  0
Patch needs improvement:  0         |  Easy pickings:  0
                  UI/UX:  0         |
------------------------------------+--------------------------------------

Comment (by ishalyapin):

 Hey, it is not very difficult problem. I wrote a solution and use it in my
 projects.
 It is not perfect solution, but it is much better then unhandled
 exception.
 I hope you take it in django.

 {{{#!python
 def filefield_maxlength_validator(value):
     """"
         Check if absolute file path can fit in database table
         and filename length can fit in filesystem.
     """

     FILESYSTEM_FILENAME_MAXLENGTH = 255 # ext4, for example
     RESERVE = 50 # We need reserve for generating thumbnails and for
 suffix if filename already exists, example - filename_85x85_1.jpg

     # For the initial file saving value.path contains not the same path,
 which will be used to save file
     filename = value.name
     filepath = os.path.join(
         settings.MEDIA_ROOT,
         value.field.generate_filename(value.instance, filename)
     )
     bytes_filename = len(filename.encode('utf-8')) # filename length in
 bytes
     bytes_filepath = len(filepath.encode('utf-8')) # path length in bytes

     # File path length should fit in table cell and filename lenth should
 fit in in filesystem
     if bytes_filepath > value.field.max_length or bytes_filename >
 FILESYSTEM_FILENAME_MAXLENGTH - RESERVE:
         if os.path.isfile(value.path):
             os.remove(value.path)
         raise exceptions.ValidationError(_(u"File name too long."))
     return value

 FileField.default_validators = FileField.default_validators[:] +
 [filefield_maxlength_validator]
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/11027#comment:14>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to