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")
# 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):
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):