mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	Corrects the functionality of the webp conversion script
This commit is contained in:
		| @@ -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 | ||||
|   | ||||
| @@ -30,47 +30,65 @@ class Command(BaseCommand): | ||||
|  | ||||
|         documents = Document.objects.all() | ||||
|  | ||||
|         for document in documents: | ||||
|             existing_thumbnail = Path(document.thumbnail_path) | ||||
|         with tempfile.TemporaryDirectory() as tempdir: | ||||
|  | ||||
|             if existing_thumbnail.suffix == "png": | ||||
|             for document in documents: | ||||
|                 existing_thumbnail = Path(document.thumbnail_path).resolve() | ||||
|  | ||||
|                 self.stdout.write(f"Converting thumbnail: {existing_thumbnail}") | ||||
|                 if existing_thumbnail.suffix == ".png": | ||||
|  | ||||
|                 converted_thumbnail = Path(tempfile.mkstemp(suffix=".webp")) | ||||
|                     self.stdout.write(f"Converting thumbnail: {existing_thumbnail}") | ||||
|  | ||||
|                 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), | ||||
|                     ) | ||||
|                     # Change the existing filename suffix from png to webp | ||||
|                     converted_thumbnail_name = existing_thumbnail.with_suffix( | ||||
|                         ".webp", | ||||
|                     ).name | ||||
|  | ||||
|                     self.stdout.write("Replacing existing thumbnail") | ||||
|                     # Create the expected output filename in the tempdir | ||||
|                     converted_thumbnail = ( | ||||
|                         Path(tempdir) / Path(converted_thumbnail_name) | ||||
|                     ).resolve() | ||||
|  | ||||
|                     if converted_thumbnail.exists(): | ||||
|                         shutil.copy(converted_thumbnail, existing_thumbnail) | ||||
|                     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), | ||||
|                         ) | ||||
|  | ||||
|                     self.stdout.write( | ||||
|                         self.style.SUCCESS("Conversion to WebP completed"), | ||||
|                     ) | ||||
|                         if converted_thumbnail.exists(): | ||||
|                             # Copy newly created thumbnail to thumbnail directory | ||||
|                             shutil.copy(converted_thumbnail, existing_thumbnail.parent) | ||||
|  | ||||
|                 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() | ||||
|                             # Remove the PNG version | ||||
|                             existing_thumbnail.unlink() | ||||
|  | ||||
|         end = time.time() | ||||
|         duration = end - start | ||||
|                             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") | ||||
|   | ||||
| @@ -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): | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Trenton Holmes
					Trenton Holmes