mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-07-28 18:24:38 -05:00
Entirely removes the optipng, updates ghostscript fall back to also use WebP. Updates the conversion to use a multiprocessing pool
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import logging
|
||||
import multiprocessing.pool
|
||||
import shutil
|
||||
import tempfile
|
||||
import time
|
||||
@@ -8,10 +9,44 @@ from django.core.management.base import BaseCommand
|
||||
from documents.models import Document
|
||||
from documents.parsers import run_convert
|
||||
|
||||
|
||||
logger = logging.getLogger("paperless.management.convert_thumbnails")
|
||||
|
||||
|
||||
def _do_convert(work_package):
|
||||
_, existing_thumbnail, converted_thumbnail = work_package
|
||||
try:
|
||||
|
||||
logger.info(f"Converting thumbnail: {existing_thumbnail}")
|
||||
|
||||
# Run actual conversion
|
||||
run_convert(
|
||||
density=300,
|
||||
scale="500x5000>",
|
||||
alpha="remove",
|
||||
strip=True,
|
||||
trim=False,
|
||||
auto_orient=True,
|
||||
input_file=f"{existing_thumbnail}[0]",
|
||||
output_file=str(converted_thumbnail),
|
||||
)
|
||||
|
||||
# Copy newly created thumbnail to thumbnail directory
|
||||
shutil.copy(converted_thumbnail, existing_thumbnail.parent)
|
||||
|
||||
# Remove the PNG version
|
||||
existing_thumbnail.unlink()
|
||||
|
||||
logger.info(
|
||||
"Conversion to WebP completed, "
|
||||
f"replaced {existing_thumbnail.name} with {converted_thumbnail.name}",
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Error converting thumbnail" f" (existing file unchanged): {e}",
|
||||
)
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
|
||||
help = """
|
||||
@@ -24,21 +59,19 @@ class Command(BaseCommand):
|
||||
|
||||
def handle(self, *args, **options):
|
||||
|
||||
self.stdout.write("Converting all PNG thumbnails to WebP")
|
||||
|
||||
logger.info("Converting all PNG thumbnails to WebP")
|
||||
start = time.time()
|
||||
|
||||
documents = Document.objects.all()
|
||||
|
||||
with tempfile.TemporaryDirectory() as tempdir:
|
||||
|
||||
work_packages = []
|
||||
|
||||
for document in documents:
|
||||
existing_thumbnail = Path(document.thumbnail_path).resolve()
|
||||
|
||||
if existing_thumbnail.suffix == ".png":
|
||||
|
||||
self.stdout.write(f"Converting thumbnail: {existing_thumbnail}")
|
||||
|
||||
# Change the existing filename suffix from png to webp
|
||||
converted_thumbnail_name = existing_thumbnail.with_suffix(
|
||||
".webp",
|
||||
@@ -49,46 +82,16 @@ class Command(BaseCommand):
|
||||
Path(tempdir) / Path(converted_thumbnail_name)
|
||||
).resolve()
|
||||
|
||||
try:
|
||||
# Run actual conversion
|
||||
run_convert(
|
||||
density=300,
|
||||
scale="500x5000>",
|
||||
alpha="remove",
|
||||
strip=True,
|
||||
trim=False,
|
||||
auto_orient=True,
|
||||
input_file=f"{existing_thumbnail}[0]",
|
||||
output_file=str(converted_thumbnail),
|
||||
)
|
||||
# Package up the necessary info
|
||||
work_packages.append(
|
||||
(document, existing_thumbnail, converted_thumbnail),
|
||||
)
|
||||
|
||||
if converted_thumbnail.exists():
|
||||
# Copy newly created thumbnail to thumbnail directory
|
||||
shutil.copy(converted_thumbnail, existing_thumbnail.parent)
|
||||
|
||||
# Remove the PNG version
|
||||
existing_thumbnail.unlink()
|
||||
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(
|
||||
"Conversion to WebP completed",
|
||||
),
|
||||
)
|
||||
else:
|
||||
# Highly unlike to reach here
|
||||
self.stderr.write(
|
||||
self.style.WARNING("Converted thumbnail doesn't exist"),
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
self.stderr.write(
|
||||
self.style.ERROR(
|
||||
f"Error converting thumbnail"
|
||||
f" (existing file unchanged): {e}",
|
||||
),
|
||||
)
|
||||
if len(work_packages):
|
||||
with multiprocessing.pool.Pool(processes=4, maxtasksperchild=4) as pool:
|
||||
pool.map(_do_convert, work_packages)
|
||||
|
||||
end = time.time()
|
||||
duration = end - start
|
||||
|
||||
self.stdout.write(f"Conversion completed in {duration:.3f}s")
|
||||
logger.info(f"Conversion completed in {duration:.3f}s")
|
||||
|
@@ -41,7 +41,7 @@ def handle_document(document_id):
|
||||
try:
|
||||
parser.parse(document.source_path, mime_type, document.get_public_filename())
|
||||
|
||||
thumbnail = parser.get_optimised_thumbnail(
|
||||
thumbnail = parser.get_thumbnail(
|
||||
document.source_path,
|
||||
mime_type,
|
||||
document.get_public_filename(),
|
||||
|
@@ -29,7 +29,7 @@ def _process_document(doc_in):
|
||||
if existing_thumbnail.exists() and existing_thumbnail.suffix == ".png":
|
||||
existing_thumbnail.unlink()
|
||||
|
||||
thumb = parser.get_optimised_thumbnail(
|
||||
thumb = parser.get_thumbnail(
|
||||
document.source_path,
|
||||
document.mime_type,
|
||||
document.get_public_filename(),
|
||||
|
Reference in New Issue
Block a user