如何在Django中组合两个STATICFILES_STORAGE类?

时间:2016-02-07 18:41:43

标签: python django

我在collectstatic期间写了一个小类来压缩.svg文件到.svgz,当从文件系统提供静态时,它运行良好。

class SvgzCompressor(storage.StaticFilesStorage):
    def __init__(self, *args, **kw):
        super(SvgzCompressor, self).__init__(*args, **kw)

    def url(self, name, force=False):
        if name.endswith('.svg') and not settings.DEBUG:
            name += 'z'
        res = unquote(super(SvgzCompressor, self).url(name))
        return res

    def post_process(self, paths, **options):
        for name, (strage, path) in paths.items():
            processed = False
            dstname = name
            if name.endswith('.svg'):
                dstname = name + 'z'
                dstname, processed = self.compress_file(name, dstname, strage)
            yield name, dstname, processed

    def compress_file(self, src, dst, src_storage):
        src_path = src_storage.path(src)
        dst_path = self.path(dst)
        if self.exists(dst_path):
            self.delete(dst_path)
        with src_storage.open(src_path, 'rb') as input_file:
            buf = BytesIO()
            with GzipFile(dst, 'wb', fileobj=buf) as gf:
                gf.write(input_file.read())
        output_fname = self._save(dst_path, ContentFile(buf.getvalue(), dst))
        return output_fname, True

并安装在settings.py

STATICFILES_STORAGE = 'mystorage.SvgzCompressor'

我想在静态存储在S3(使用S3BotoStorage后端)时也这样做,但STATICFILES_STORAGE设置只需要一个后端类,所以我不清楚如何将它们链接在一起.. < / p>

这可能吗?

0 个答案:

没有答案