diff --git a/src/documents/management/commands/document_thumbnails.py b/src/documents/management/commands/document_thumbnails.py index c9928c7cc..595d8ba3b 100644 --- a/src/documents/management/commands/document_thumbnails.py +++ b/src/documents/management/commands/document_thumbnails.py @@ -1,6 +1,7 @@ import logging import multiprocessing import shutil +from pathlib import Path import tqdm from django import db @@ -11,7 +12,7 @@ from ...parsers import get_parser_class_for_mime_type def _process_document(doc_in): - document = Document.objects.get(id=doc_in) + document: Document = Document.objects.get(id=doc_in) parser_class = get_parser_class_for_mime_type(document.mime_type) if parser_class: @@ -21,6 +22,13 @@ def _process_document(doc_in): return try: + + existing_thumbnail = Path(document.thumbnail_path).resolve() + + # Remove an existing PNG format thumbnail, if it existed + if existing_thumbnail.exists() and existing_thumbnail.suffix == ".png": + existing_thumbnail.unlink() + thumb = parser.get_optimised_thumbnail( document.source_path, document.mime_type, @@ -69,7 +77,7 @@ class Command(BaseCommand): ids = [doc.id for doc in documents] # Note to future self: this prevents django from reusing database - # conncetions between processes, which is bad and does not work + # connections between processes, which is bad and does not work # with postgres. db.connections.close_all()