Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: add nginx mod_zip output feature #391

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion sigal/plugins/zip_gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@
See :ref:`compatibility with the encrypt plugin <compatibility-with-encrypt>`.
"""

import io
import logging
import os
import zipfile
from os.path import isfile, join, splitext
import zlib

from os.path import getsize, isfile, join, normpath, splitext
from urllib.parse import quote

from sigal import signals
from sigal.gallery import Album
Expand Down Expand Up @@ -81,6 +85,38 @@ def _generate_album_zip(album):

return False

def _compute_crc32(fpath, block_size=65536):
crc = 0

with open(fpath, 'rb') as fh:
chunk = fh.read(block_size)

while chunk:
crc = zlib.crc32(chunk, crc)
chunk = fh.read(block_size)

return "{:08x}".format(crc)

def _generate_album_list(album):
attr = 'src_path' if album.settings['zip_media_format'] == 'orig' else 'dst_path'
zip_fname = '{}.zip'.format(album.name)
list_path = join(album.dst_path, zip_fname)

logger.info('Generating %s (list)', list_path)

with open(list_path, 'w') as fh:
for p in album:
img_path = getattr(p, attr)
img_crc32 = _compute_crc32(img_path)
img_size = getsize(img_path)
zip_path = join(album.name, p.filename)
http_path = quote(join('/', p.path, p.filename))

fh.write('{} {} {} {}\n'.format(
img_crc32, img_size, http_path, zip_path))

return zip_fname

def generate_album_zip(album):
"""Checks for .nozip_gallery file in album folder.
If this file exists, no ZIP archive is generated.
Expand All @@ -89,6 +125,8 @@ def generate_album_zip(album):
If the ``zip_gallery`` setting is set,it contains the location of a zip
archive with all original images of the corresponding directory.
"""
if album.settings.get('zip_gallery', False) == "nginx":
return _generate_album_list(album)

# check if ZIP file generation as been disabled by .nozip_gallery file
if not _should_generate_album_zip(album):
Expand Down