fix(effi-ocr): improve file search and show available files on error
This commit is contained in:
@@ -39,7 +39,8 @@ class Tools:
|
|||||||
file_path = self._find_file(image_path)
|
file_path = self._find_file(image_path)
|
||||||
|
|
||||||
if not file_path:
|
if not file_path:
|
||||||
return "[ERROR] File not found: " + image_path + ". Use list_recent_files() to see available files."
|
available = self._list_files()
|
||||||
|
return "[ERROR] File not found: " + image_path + "\n\nAvailable files:\n" + available
|
||||||
|
|
||||||
with open(file_path, 'rb') as f:
|
with open(file_path, 'rb') as f:
|
||||||
files = {'file': (os.path.basename(file_path), f)}
|
files = {'file': (os.path.basename(file_path), f)}
|
||||||
@@ -103,6 +104,20 @@ class Tools:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return "[ERROR] " + str(e)
|
return "[ERROR] " + str(e)
|
||||||
|
|
||||||
|
def _list_files(self):
|
||||||
|
try:
|
||||||
|
files = []
|
||||||
|
for directory in [self.uploads_dir, self.host_uploads_dir]:
|
||||||
|
if os.path.exists(directory):
|
||||||
|
files.extend(glob.glob(os.path.join(directory, "*")))
|
||||||
|
files = [f for f in files if os.path.isfile(f)]
|
||||||
|
if not files:
|
||||||
|
return "No files found"
|
||||||
|
files.sort(key=os.path.getmtime, reverse=True)
|
||||||
|
return "\n".join([os.path.basename(f) for f in files[:10]])
|
||||||
|
except:
|
||||||
|
return "Cannot list files"
|
||||||
|
|
||||||
def _find_file(self, filename):
|
def _find_file(self, filename):
|
||||||
filename = filename.strip().strip('"').strip("'")
|
filename = filename.strip().strip('"').strip("'")
|
||||||
|
|
||||||
@@ -119,11 +134,19 @@ class Tools:
|
|||||||
|
|
||||||
search_dirs = [d for d in [self.uploads_dir, self.host_uploads_dir] if os.path.exists(d)]
|
search_dirs = [d for d in [self.uploads_dir, self.host_uploads_dir] if os.path.exists(d)]
|
||||||
|
|
||||||
|
if not search_dirs:
|
||||||
|
return None
|
||||||
|
|
||||||
for directory in search_dirs:
|
for directory in search_dirs:
|
||||||
all_files = glob.glob(os.path.join(directory, "*"))
|
all_files = glob.glob(os.path.join(directory, "*"))
|
||||||
for f in all_files:
|
for f in all_files:
|
||||||
basename = os.path.basename(f)
|
basename = os.path.basename(f).lower()
|
||||||
if basename.startswith(filename) or filename in basename:
|
search_name = filename.lower()
|
||||||
|
if search_name in basename:
|
||||||
|
return f
|
||||||
|
parts = search_name.replace('.jpeg', '').replace('.jpg', '').replace('.png', '').split()
|
||||||
|
for part in parts:
|
||||||
|
if len(part) > 3 and part in basename:
|
||||||
return f
|
return f
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|||||||
Reference in New Issue
Block a user