]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Corrects the functionality of the webp conversion script
authorTrenton Holmes <holmes.trenton@gmail.com>
Fri, 10 Jun 2022 15:56:25 +0000 (08:56 -0700)
committerTrenton Holmes <holmes.trenton@gmail.com>
Fri, 10 Jun 2022 15:56:25 +0000 (08:56 -0700)
docker/install_management_commands.sh
src/documents/management/commands/convert_thumbnails.py
src/documents/models.py

index bf8bbeb937a9d5e096062aca8336b97d48d0ceae..beb600fdbc28ac8bfde48898b1d43cf6b84dac59 100755 (executable)
@@ -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;
 do
        echo "installing $command..."
        sed "s/management_command/$command/g" management_script.sh > /usr/local/bin/$command
index 249209580c750a53f890135aaf18f7594f1896a4..0be4cb70219fc34839ec80505b55bba4412e6f32 100644 (file)
@@ -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 - start
 
         self.stdout.write(f"Conversion completed in {duration:.3f}s")
index d889ef2c53cb4f53503fe886c289717249535f69..221086ca2b79823f95ced60c8cceea32da1e80f9 100644 (file)
@@ -305,10 +305,9 @@ class Document(models.Model):
         # Hence why this looks a little weird
 
         webp_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 PNG
             if not os.path.exists(png_file_path):
@@ -316,11 +315,11 @@ class Document(models.Model):
                 thumb = webp_file_path
             else:
                 # 2.1 - PNG file exists, return path to it
-                thumb = png_file_name
+                thumb = png_file_path
         else:
             # 1.1 - WebP file exists, return path to it
             thumb = webp_file_path
-        return thumb
+        return os.path.normpath(thumb)
 
     @property
     def thumbnail_file(self):