Ticket #3119: store_filename_as_punycode_for_trunk_4462.diff

File store_filename_as_punycode_for_trunk_4462.diff, 4.0 KB (added by makoto tsuyuki <mtsuyuki@…>, 18 years ago)
  • django/conf/global_settings.py

     
    199199# Example: "/home/media/media.lawrence.com/"
    200200MEDIA_ROOT = ''
    201201
     202# If you set this to True, Django stores filename as punycode.
     203STORE_FILENAME_AS_PUNYCODE = False
     204
    202205# URL that handles the media served from MEDIA_ROOT.
    203206# Example: "http://media.lawrence.com"
    204207MEDIA_URL = ''
     
    315318# The name of the database to use for testing purposes.
    316319# If None, a name of 'test_' + DATABASE_NAME will be assumed
    317320TEST_DATABASE_NAME = None
     321
  • django/db/models/fields/__init__.py

     
    656656        return os.path.normpath(datetime.datetime.now().strftime(self.upload_to))
    657657
    658658    def get_filename(self, filename):
    659         from django.utils.text import get_valid_filename
     659        if settings.STORE_FILENAME_AS_PUNYCODE :
     660            from django.utils.text import get_valid_filename_as_puny as get_valid_filename
     661        else :
     662            from django.utils.text import get_valid_filename
    660663        f = os.path.join(self.get_directory_name(), get_valid_filename(os.path.basename(filename)))
    661664        return os.path.normpath(f)
    662665
  • django/utils/text.py

     
     1#encoding: utf-8
     2
    13import re
    24
    35from django.conf import settings
     
    5355    s = s.strip().replace(' ', '_')
    5456    return re.sub(r'[^-A-Za-z0-9_.]', '', s)
    5557
     58def get_valid_filename_as_puny(s):
     59    """
     60    Encodes the given string in punycode except the extension.
     61    Then generate a clean file name by calling get_valid_filename and return it.
     62    >>> get_valid_filename_as_puny("développement image.jpg")
     63    'dveloppement_image-kwa33c.jpg'
     64    >>> get_valid_filename_as_puny("日本語ファイル名.jpg")
     65    'caabc4cr5ao9bu0bzbf5exag15gcacc8bdc4c.jpg'
     66    >>> get_valid_filename_as_puny("multidot.tar.gz")
     67    'multidot.tar.gz'
     68    >>> get_valid_filename_as_puny("developpement image.jpg")
     69    'developpement_image.jpg'
     70    >>> get_valid_filename_as_puny("john's portrait in 2004.jpg")
     71    'johns_portrait_in_2004.jpg'
     72    >>> get_valid_filename_as_puny("multidot.tar.gz")
     73    'multidot.tar.gz'
     74    >>> get_valid_filename_as_puny("複数ドット.tar.gz")
     75    'daaaaqkd8g7f4e7ycc7a1a.tar.gz'
     76    """
     77    try:
     78        dot_index = s.index('.')
     79    except ValueError: # filename has no dot
     80        filename = s.encode('punycode')
     81        if len(filename) > 0 and filename[:-1] == s:
     82            filenamme = s
     83    else:
     84        filename = s[:dot_index].encode('punycode') + s[dot_index:]
     85        if len(filename[:filename.index('.')]) > 0 and filename[:filename.index('.') - 1] == s[:dot_index]:
     86            filename = s
     87    return get_valid_filename(filename)
     88
    5689def get_text_list(list_, last_word='or'):
    5790    """
    5891    >>> get_text_list(['a', 'b', 'c', 'd'])
  • docs/settings.txt

     
    711711
    712712.. _site framework docs: ../sites/
    713713
     714STORE_FILENAME_AS_PUNYCODE
     715--------------------------
     716
     717Default: False
     718
     719Whether to encode filename with punycode.
     720By default, FileField and its subtype calls django.utils.text.get_valid_filename() to
     721remove all "filename-unsafe" characters from given filename.
     722The resulting filename consists of alphabets, numbers, hyphens and underscores.
     723Set this True, if you might use multi-byte charcters for your filename.
     724FileField encode their filename with punycode except extension.
     725Then it remove all "filename-unsafe" characters.
     726
     727
    714728TEMPLATE_CONTEXT_PROCESSORS
    715729---------------------------
    716730
Back to Top