|
@@ -7,9 +7,9 @@ Gzip cache
|
|
|
A plugin to create .gz cache files for optimization.
|
|
|
'''
|
|
|
|
|
|
-import gzip
|
|
|
import logging
|
|
|
import os
|
|
|
+import zlib
|
|
|
|
|
|
from pelican import signals
|
|
|
|
|
@@ -39,6 +39,17 @@ EXCLUDE_TYPES = [
|
|
|
'.mp4',
|
|
|
]
|
|
|
|
|
|
+COMPRESSION_LEVEL = 9 # Best Compression
|
|
|
+
|
|
|
+""" According to zlib manual: 'Add 16 to
|
|
|
+windowBits to write a simple gzip header and trailer around the
|
|
|
+compressed data instead of a zlib wrapper. The gzip header will
|
|
|
+have no file name, no extra data, no comment, no modification
|
|
|
+time (set to zero), no header crc, and the operating system
|
|
|
+will be set to 255 (unknown)'
|
|
|
+"""
|
|
|
+WBITS = zlib.MAX_WBITS | 16
|
|
|
+
|
|
|
|
|
|
def create_gzip_cache(pelican):
|
|
|
'''Create a gzip cache file for every file that a webserver would
|
|
@@ -73,13 +84,16 @@ def create_gzip_file(filepath):
|
|
|
compressed_path = filepath + '.gz'
|
|
|
|
|
|
with open(filepath, 'rb') as uncompressed:
|
|
|
- # Explicitly set mtime to 0 so gzip content is fully determined
|
|
|
- # by file content (0 = "no timestamp" according to gzip spec)
|
|
|
- with gzip.GzipFile(compressed_path, 'wb',
|
|
|
- compresslevel=9, mtime=0) as compressed:
|
|
|
+ gzip_compress_obj = zlib.compressobj(COMPRESSION_LEVEL,
|
|
|
+ zlib.DEFLATED, WBITS)
|
|
|
+
|
|
|
+ uncompressed_data = uncompressed.read()
|
|
|
+ gzipped_data = gzip_compress_obj.compress(uncompressed_data)
|
|
|
+ gzipped_data += gzip_compress_obj.flush()
|
|
|
+ with open(compressed_path, 'wb') as compressed:
|
|
|
logger.debug('Compressing: %s' % filepath)
|
|
|
try:
|
|
|
- compressed.writelines(uncompressed)
|
|
|
+ compressed.write(gzipped_data)
|
|
|
except Exception as ex:
|
|
|
logger.critical('Gzip compression failed: %s' % ex)
|
|
|
|