mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	webp thumbnail support with png fallback
This commit is contained in:
		| @@ -293,11 +293,16 @@ class Document(models.Model): | |||||||
|  |  | ||||||
|     @property |     @property | ||||||
|     def thumbnail_path(self): |     def thumbnail_path(self): | ||||||
|         file_name = f"{self.pk:07}.png" |         file_name = f"{self.pk:07}.webp" | ||||||
|         if self.storage_type == self.STORAGE_TYPE_GPG: |         if self.storage_type == self.STORAGE_TYPE_GPG: | ||||||
|             file_name += ".gpg" |             file_name += ".gpg" | ||||||
|  |  | ||||||
|         return os.path.join(settings.THUMBNAIL_DIR, file_name) |         thumb = os.path.join(settings.THUMBNAIL_DIR, file_name) | ||||||
|  |  | ||||||
|  |         if os.path.exists(thumb): | ||||||
|  |             return thumb | ||||||
|  |         else: | ||||||
|  |             return os.path.splitext(thumb)[0] + ".png" | ||||||
|  |  | ||||||
|     @property |     @property | ||||||
|     def thumbnail_file(self): |     def thumbnail_file(self): | ||||||
|   | |||||||
| @@ -191,7 +191,7 @@ def make_thumbnail_from_pdf(in_path, temp_dir, logging_group=None) -> str: | |||||||
|     """ |     """ | ||||||
|     The thumbnail of a PDF is just a 500px wide image of the first page. |     The thumbnail of a PDF is just a 500px wide image of the first page. | ||||||
|     """ |     """ | ||||||
|     out_path = os.path.join(temp_dir, "convert.png") |     out_path = os.path.join(temp_dir, "convert.webp") | ||||||
|  |  | ||||||
|     # Run convert to get a decent thumbnail |     # Run convert to get a decent thumbnail | ||||||
|     try: |     try: | ||||||
| @@ -321,7 +321,7 @@ class DocumentParser(LoggingMixin): | |||||||
|  |  | ||||||
|     def get_optimised_thumbnail(self, document_path, mime_type, file_name=None): |     def get_optimised_thumbnail(self, document_path, mime_type, file_name=None): | ||||||
|         thumbnail = self.get_thumbnail(document_path, mime_type, file_name) |         thumbnail = self.get_thumbnail(document_path, mime_type, file_name) | ||||||
|         if settings.OPTIMIZE_THUMBNAILS: |         if settings.OPTIMIZE_THUMBNAILS and os.path.splitext(thumbnail)[1] == ".png": | ||||||
|             out_path = os.path.join(self.tempdir, "thumb_optipng.png") |             out_path = os.path.join(self.tempdir, "thumb_optipng.png") | ||||||
|  |  | ||||||
|             args = ( |             args = ( | ||||||
|   | |||||||
| @@ -362,7 +362,12 @@ class DocumentViewSet( | |||||||
|                 handle = doc.thumbnail_file |                 handle = doc.thumbnail_file | ||||||
|             # TODO: Send ETag information and use that to send new thumbnails |             # TODO: Send ETag information and use that to send new thumbnails | ||||||
|             #  if available |             #  if available | ||||||
|             return HttpResponse(handle, content_type="image/png") |             content_type = ( | ||||||
|  |                 "image/webp" | ||||||
|  |                 if os.path.splitext(doc.thumbnail_path)[1] == ".webp" | ||||||
|  |                 else "image/png" | ||||||
|  |             ) | ||||||
|  |             return HttpResponse(handle, content_type=content_type) | ||||||
|         except (FileNotFoundError, Document.DoesNotExist): |         except (FileNotFoundError, Document.DoesNotExist): | ||||||
|             raise Http404() |             raise Http404() | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Michael Shamoon
					Michael Shamoon