Handle skip indexing of unsupported image files

Previously unsupported image file types would trigger an unbound local
variable error.
This commit is contained in:
Debanjum
2025-10-13 15:42:43 -04:00
parent 749bbed23d
commit a809de8970

View File

@@ -54,6 +54,7 @@ class ImageToEntries(TextToEntries):
entries: List[str] = []
entry_to_location_map: List[Tuple[str, str]] = []
for image_file in image_files:
tmp_file = None
try:
bytes = image_files[image_file]
# write the image to a temporary file
@@ -65,6 +66,8 @@ class ImageToEntries(TextToEntries):
tmp_file = f"tmp_image_file_{timestamp_now}.jpg"
elif image_file.endswith(".webp"):
tmp_file = f"tmp_image_file_{timestamp_now}.webp"
else:
continue
with open(tmp_file, "wb") as f:
bytes = image_files[image_file]
f.write(bytes)
@@ -89,7 +92,7 @@ class ImageToEntries(TextToEntries):
logger.warning(f"Unable to process file: {image_file}. This file will not be indexed.")
logger.warning(e, exc_info=True)
finally:
if os.path.exists(tmp_file):
if tmp_file and os.path.exists(tmp_file):
os.remove(tmp_file)
return file_to_text_map, ImageToEntries.convert_image_entries_to_maps(entries, dict(entry_to_location_map))