A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
Corrects the functionality of the webp conversion script
Trenton Holmes34192349be82
diff --git a/docker/install_management_commands.sh b/docker/install_management_commands.shindex bf8bbeb93..beb600fdb 100755--- a/docker/install_management_commands.sh+++ b/docker/install_management_commands.sh@@ -2,7 +2,19 @@set -eu-for command in document_archiver document_exporter document_importer mail_fetcher document_create_classifier document_index document_renamer document_retagger document_thumbnails document_sanity_checker manage_superuser;+for command in convert_thumbnails \+ decrypt_documents \+ document_archiver \+ document_exporter \+ document_importer \+ mail_fetcher \+ document_create_classifier \+ document_index \+ document_renamer \+ document_retagger \+ document_thumbnails \+ document_sanity_checker \+ manage_superuser;doecho "installing $command..."sed "s/management_command/$command/g" management_script.sh > /usr/local/bin/$commanddiff --git a/src/documents/management/commands/convert_thumbnails.py b/src/documents/management/commands/convert_thumbnails.pyindex 249209580..0be4cb702 100644--- a/src/documents/management/commands/convert_thumbnails.py+++ b/src/documents/management/commands/convert_thumbnails.py@@ -30,47 +30,65 @@ class Command(BaseCommand):documents = Document.objects.all()- for document in documents:- existing_thumbnail = Path(document.thumbnail_path)-- if existing_thumbnail.suffix == "png":-- self.stdout.write(f"Converting thumbnail: {existing_thumbnail}")-- converted_thumbnail = Path(tempfile.mkstemp(suffix=".webp"))-- try:- 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),- )-- self.stdout.write("Replacing existing thumbnail")-- if converted_thumbnail.exists():- shutil.copy(converted_thumbnail, existing_thumbnail)-- self.stdout.write(- self.style.SUCCESS("Conversion to WebP completed"),- )-- except Exception as e:- self.stderr.write(- self.style.ERROR(- f"Error converting thumbnail (existing will be kept): {e}",- ),- )- finally:- if converted_thumbnail.exists():- converted_thumbnail.unlink()-- end = time.time()- duration = end - start+ with tempfile.TemporaryDirectory() as tempdir:++ 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",+ ).name++ # Create the expected output filename in the tempdir+ converted_thumbnail = (+ 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),+ )++ 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}",+ ),+ )++ end = time.time()+ duration = end - startself.stdout.write(f"Conversion completed in {duration:.3f}s")diff --git a/src/documents/models.py b/src/documents/models.pyindex d889ef2c5..221086ca2 100644--- a/src/documents/models.py+++ b/src/documents/models.py@@ -305,10 +305,9 @@ class Document(models.Model):# Hence why this looks a little weirdwebp_file_path = os.path.join(settings.THUMBNAIL_DIR, webp_file_name)- png_file_path = thumb = os.path.join(settings.THUMBNAIL_DIR, png_file_name)+ png_file_path = os.path.join(settings.THUMBNAIL_DIR, png_file_name)# 1. Assume the thumbnail is WebP-if not os.path.exists(webp_file_path):# 2. If WebP doesn't exist, check PNGif not os.path.exists(png_file_path):@@ -316,11 +315,11 @@ class Document(models.Model):thumb = webp_file_pathelse:# 2.1 - PNG file exists, return path to it- thumb = png_file_name+ thumb = png_file_pathelse:# 1.1 - WebP file exists, return path to itthumb = webp_file_path- return thumb+ return os.path.normpath(thumb)@propertydef thumbnail_file(self):