Move to gs to generate thumbnails

This commit is contained in:
augustin64 2024-02-25 15:53:51 +01:00
parent b9a5f92a56
commit c702cb714e
2 changed files with 23 additions and 6 deletions

View File

@ -3,7 +3,9 @@
Albums module Albums module
""" """
import os import os
import pypdf
import shutil import shutil
from uuid import uuid4 from uuid import uuid4
from typing import TypeVar from typing import TypeVar
@ -263,6 +265,12 @@ def add_partition(album_uuid):
else: else:
partition_type = "file" partition_type = "file"
try:
pypdf.PdfReader(request.files["file"])
request.files["file"].seek(0)
except (pypdf.errors.PdfReadError, pypdf.errors.PdfStreamError):
error = _("Invalid PDF file")
if error is not None: if error is not None:
flash(error) flash(error)
return redirect(request.referrer) return redirect(request.referrer)

View File

@ -2,6 +2,7 @@
Thumbnails Thumbnails
""" """
import os import os
import pypdf
from flask import current_app, abort, Blueprint, send_file from flask import current_app, abort, Blueprint, send_file
@ -14,13 +15,18 @@ def generate_thumbnail(source, dest):
""" """
Generates a thumbnail with 'convert' (ImageMagick) Generates a thumbnail with 'convert' (ImageMagick)
""" """
os.system( try:
f'/usr/bin/convert -thumbnail\ pypdf.PdfReader(source) # Check if file is really a PDF
"178^>" -background white -alpha \ except (pypdf.errors.PdfReadError, pypdf.errors.PdfStreamError):
remove -crop 178x178+0+0 \ return
{source}[0] \
{dest}' command = (
f"gs -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 \
-dAlignToPixels=0 -dGridFitTT=2 -sDEVICE=png16m -dBackgroundColor=16#FFFFFF -dTextAlphaBits=4 \
-dGraphicsAlphaBits=4 -r72x72 -dPrinted=false -dFirstPage=1 -dPDFFitPage -g356x356 \
-dLastPage=1 -sOutputFile={dest} {source}"
) )
os.system(command)
def serve_thumbnail(partition_file, thumbnail_file): def serve_thumbnail(partition_file, thumbnail_file):
""" """
@ -32,6 +38,9 @@ def serve_thumbnail(partition_file, thumbnail_file):
if not os.path.exists(thumbnail_file): if not os.path.exists(thumbnail_file):
generate_thumbnail(partition_file, thumbnail_file) generate_thumbnail(partition_file, thumbnail_file)
if not os.path.exists(thumbnail_file):
abort(404)
return send_file(thumbnail_file) return send_file(thumbnail_file)