Ticket #17896: cachedfilesmixin_custom_file_hash.diff

File cachedfilesmixin_custom_file_hash.diff, 1.3 KB (added by mkai, 3 years ago)
  • django/contrib/staticfiles/storage.py

    diff --git a/django/contrib/staticfiles/storage.py b/django/contrib/staticfiles/storage.py
    index c000f97..e59b987 100644
    a b class CachedFilesMixin(object): 
    6565                compiled = re.compile(pattern)
    6666                self._patterns.setdefault(extension, []).append(compiled)
    6767
     68    def get_file_hash(self, name, content=None):
     69        """Gets the MD5 hash of the file."""
     70        md5 = hashlib.md5()
     71        for chunk in content.chunks():
     72            md5.update(chunk)
     73        return md5.hexdigest()[:12]
     74
    6875    def hashed_name(self, name, content=None):
    6976        parsed_name = urlsplit(unquote(name))
    7077        clean_name = parsed_name.path.strip()
    class CachedFilesMixin(object): 
    7986                return name
    8087        path, filename = os.path.split(clean_name)
    8188        root, ext = os.path.splitext(filename)
    82         # Get the MD5 hash of the file
    83         md5 = hashlib.md5()
    84         for chunk in content.chunks():
    85             md5.update(chunk)
    86         md5sum = md5.hexdigest()[:12]
     89        file_hash = self.get_file_hash(clean_name, content)
    8790        hashed_name = os.path.join(path, u"%s.%s%s" %
    88                                    (root, md5sum, ext))
     91                                   (root, file_hash, ext))
Back to Top