Ticket #15181: filepath_to_uri.patch

File filepath_to_uri.patch, 3.2 KB (added by e.generalov, 14 years ago)
  • tests/regressiontests/file_storage/tests.py

     
    204204        self.assertEqual(self.storage.url('test.file'),
    205205            '%s%s' % (self.storage.base_url, 'test.file'))
    206206
     207        # should encode special chars except ~!*()'
     208        # like encodeURIComponent() JavaScript function do
     209        self.assertEqual(self.storage.url(r"""~!*()'@#$%^&*abc`+=.file"""),
     210            """/test_media_url/~!*()'%40%23%24%25%5E%26*abc%60%2B%3D.file""")
     211
     212        # should stanslate os path separator(s) to the url path separator
     213        self.assertEqual(self.storage.url("""a/b\\c.file"""),
     214            """/test_media_url/a/b/c.file""")
     215
    207216        self.storage.base_url = None
    208217        self.assertRaises(ValueError, self.storage.url, 'test.file')
    209218
  • django/core/files/storage.py

     
    88from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation
    99from django.core.files import locks, File
    1010from django.core.files.move import file_move_safe
    11 from django.utils.encoding import force_unicode
     11from django.utils.encoding import force_unicode, filepath_to_uri
    1212from django.utils.functional import LazyObject
    1313from django.utils.importlib import import_module
    1414from django.utils.text import get_valid_filename
     
    240240    def url(self, name):
    241241        if self.base_url is None:
    242242            raise ValueError("This file is not accessible via a URL.")
    243         return urlparse.urljoin(self.base_url, name).replace('\\', '/')
     243        return urlparse.urljoin(self.base_url, filepath_to_uri(name))
    244244
    245245    def accessed_time(self, name):
    246246        return datetime.fromtimestamp(os.path.getatime(self.path(name)))
  • django/utils/encoding.py

     
    156156        return iri
    157157    return urllib.quote(smart_str(iri), safe="/#%[]=:;$&()+,!?*@'~")
    158158
     159def filepath_to_uri(path):
     160    """Convert an file system path to a URI portion that is suitable for
     161    inclusion in a URL.
    159162
     163    We are assuming input is either UTF-8 or unicode already.
     164   
     165    This method will encode certain chars that would normally be recognized as
     166    special chars for URIs.  Note that this method does not encode the '
     167    character, as it is a valid character within URIs.  See
     168    encodeURIComponent() JavaScript function for more details.
     169
     170    Returns an ASCII string containing the encoded result.
     171    """
     172    if path is None:
     173        return path
     174    # I know about `os.sep` and `os.altsep` but I want to leave
     175    # some flexibility for hardcoding separators.
     176    return urllib.quote(smart_str(path).replace("\\", "/"), safe="/~!*()'")
     177
    160178# The encoding of the default system locale but falls back to the
    161179# given fallback encoding if the encoding is unsupported by python or could
    162180# not be determined.  See tickets #10335 and #5846
Back to Top