mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-06 13:22:12 +00:00
Handle skip indexing of unsupported image files
Previously unsupported image file types would trigger an unbound local variable error.
This commit is contained in:
@@ -54,6 +54,7 @@ class ImageToEntries(TextToEntries):
|
|||||||
entries: List[str] = []
|
entries: List[str] = []
|
||||||
entry_to_location_map: List[Tuple[str, str]] = []
|
entry_to_location_map: List[Tuple[str, str]] = []
|
||||||
for image_file in image_files:
|
for image_file in image_files:
|
||||||
|
tmp_file = None
|
||||||
try:
|
try:
|
||||||
bytes = image_files[image_file]
|
bytes = image_files[image_file]
|
||||||
# write the image to a temporary file
|
# write the image to a temporary file
|
||||||
@@ -65,6 +66,8 @@ class ImageToEntries(TextToEntries):
|
|||||||
tmp_file = f"tmp_image_file_{timestamp_now}.jpg"
|
tmp_file = f"tmp_image_file_{timestamp_now}.jpg"
|
||||||
elif image_file.endswith(".webp"):
|
elif image_file.endswith(".webp"):
|
||||||
tmp_file = f"tmp_image_file_{timestamp_now}.webp"
|
tmp_file = f"tmp_image_file_{timestamp_now}.webp"
|
||||||
|
else:
|
||||||
|
continue
|
||||||
with open(tmp_file, "wb") as f:
|
with open(tmp_file, "wb") as f:
|
||||||
bytes = image_files[image_file]
|
bytes = image_files[image_file]
|
||||||
f.write(bytes)
|
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(f"Unable to process file: {image_file}. This file will not be indexed.")
|
||||||
logger.warning(e, exc_info=True)
|
logger.warning(e, exc_info=True)
|
||||||
finally:
|
finally:
|
||||||
if os.path.exists(tmp_file):
|
if tmp_file and os.path.exists(tmp_file):
|
||||||
os.remove(tmp_file)
|
os.remove(tmp_file)
|
||||||
return file_to_text_map, ImageToEntries.convert_image_entries_to_maps(entries, dict(entry_to_location_map))
|
return file_to_text_map, ImageToEntries.convert_image_entries_to_maps(entries, dict(entry_to_location_map))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user