This commit is contained in:
2026-01-14 15:56:04 -08:00
parent 0f726b11dc
commit d474916874
2 changed files with 159 additions and 126 deletions

View File

@@ -864,22 +864,23 @@ class API_folder_store:
def get_metadata(self, file_hash: str) -> Optional[Dict[str, Any]]:
"""Get metadata for a file by hash."""
try:
cursor = self.connection.cursor()
with self._db_lock:
cursor = self.connection.cursor()
cursor.execute(
"""
SELECT m.* FROM metadata m
WHERE m.hash = ?
""",
(file_hash,
),
)
cursor.execute(
"""
SELECT m.* FROM metadata m
WHERE m.hash = ?
""",
(file_hash,
),
)
row = cursor.fetchone()
if not row:
return None
row = cursor.fetchone()
if not row:
return None
metadata = dict(row)
metadata = dict(row)
# Parse JSON fields
for field in ["url", "relationships"]:
@@ -1236,19 +1237,20 @@ class API_folder_store:
def get_tags(self, file_hash: str) -> List[str]:
"""Get all tags for a file by hash."""
try:
cursor = self.connection.cursor()
with self._db_lock:
cursor = self.connection.cursor()
cursor.execute(
"""
SELECT t.tag FROM tag t
WHERE t.hash = ?
ORDER BY t.tag
""",
(file_hash,
),
)
cursor.execute(
"""
SELECT t.tag FROM tag t
WHERE t.hash = ?
ORDER BY t.tag
""",
(file_hash,
),
)
return [row[0] for row in cursor.fetchall()]
return [row[0] for row in cursor.fetchall()]
except Exception as e:
logger.error(f"Error getting tags for hash {file_hash}: {e}", exc_info=True)
return []
@@ -1833,18 +1835,19 @@ class API_folder_store:
def search_hash(self, file_hash: str) -> Optional[Path]:
"""Search for a file by hash."""
try:
cursor = self.connection.cursor()
with self._db_lock:
cursor = self.connection.cursor()
cursor.execute(
"""
SELECT file_path FROM file WHERE hash = ?
""",
(file_hash,
),
)
cursor.execute(
"""
SELECT file_path FROM file WHERE hash = ?
""",
(file_hash,
),
)
row = cursor.fetchone()
return self._from_db_file_path(row[0]) if row else None
row = cursor.fetchone()
return self._from_db_file_path(row[0]) if row else None
except Exception as e:
logger.error(f"Error searching by hash '{file_hash}': {e}", exc_info=True)
return None
@@ -3525,13 +3528,15 @@ class LocalLibrarySearchOptimizer:
"""Get tags from database cache."""
if not self.db:
return []
return self.db.get_tags(file_path)
file_hash = self.db.get_file_hash(file_path)
return self.db.get_tags(file_hash) if file_hash else []
def get_cached_metadata(self, file_path: Path) -> Optional[Dict[str, Any]]:
"""Get metadata from database cache."""
if not self.db:
return None
return self.db.get_metadata(file_path)
file_hash = self.db.get_file_hash(file_path)
return self.db.get_metadata(file_hash) if file_hash else None
def prefetch_metadata(self, file_paths: List[Path]) -> None:
"""Pre-cache metadata for multiple files."""
@@ -3554,11 +3559,15 @@ class LocalLibrarySearchOptimizer:
return
try:
tags = self.db.get_tags(file_path)
file_hash = self.db.get_file_hash(file_path)
if not file_hash:
return
tags = self.db.get_tags(file_hash)
if tags:
search_result.tag_summary = ", ".join(tags)
metadata = self.db.get_metadata(file_path)
metadata = self.db.get_metadata(file_hash)
if metadata:
if "hash" in metadata:
search_result.hash_hex = metadata["hash"]
@@ -3575,27 +3584,28 @@ class LocalLibrarySearchOptimizer:
return []
try:
cursor = self.db.connection.cursor()
cursor.execute(
"""
SELECT f.hash, f.file_path, m.duration, m.size, m.type as media_kind, m.url
FROM file f
JOIN tag t ON f.hash = t.hash
LEFT JOIN metadata m ON f.hash = m.hash
WHERE t.tag LIKE ?
LIMIT ?
""",
(f"%{tag}%",
limit),
)
with self.db._db_lock:
cursor = self.db.connection.cursor()
cursor.execute(
"""
SELECT f.hash, f.file_path, m.duration, m.size, m.type as media_kind, m.url
FROM file f
JOIN tag t ON f.hash = t.hash
LEFT JOIN metadata m ON f.hash = m.hash
WHERE t.tag LIKE ?
LIMIT ?
""",
(f"%{tag}%",
limit),
)
results = []
for row in cursor.fetchall():
res = dict(row)
# Resolve path to absolute string for remote consumption
res["file_path"] = str(self.db._from_db_file_path(res["file_path"]))
results.append(res)
return results
results = []
for row in cursor.fetchall():
res = dict(row)
# Resolve path to absolute string for remote consumption
res["file_path"] = str(self.db._from_db_file_path(res["file_path"]))
results.append(res)
return results
except Exception as e:
logger.error(f"Tag search failed: {e}")
return []
@@ -3606,26 +3616,27 @@ class LocalLibrarySearchOptimizer:
return []
try:
cursor = self.db.connection.cursor()
cursor.execute(
"""
SELECT f.hash, f.file_path, m.duration, m.size, m.type as media_kind, m.url
FROM file f
LEFT JOIN metadata m ON f.hash = m.hash
WHERE f.file_path LIKE ?
LIMIT ?
""",
(f"%{query}%",
limit),
)
with self.db._db_lock:
cursor = self.db.connection.cursor()
cursor.execute(
"""
SELECT f.hash, f.file_path, m.duration, m.size, m.type as media_kind, m.url
FROM file f
LEFT JOIN metadata m ON f.hash = m.hash
WHERE f.file_path LIKE ?
LIMIT ?
""",
(f"%{query}%",
limit),
)
results = []
for row in cursor.fetchall():
res = dict(row)
# Resolve path to absolute string for remote consumption
res["file_path"] = str(self.db._from_db_file_path(res["file_path"]))
results.append(res)
return results
results = []
for row in cursor.fetchall():
res = dict(row)
# Resolve path to absolute string for remote consumption
res["file_path"] = str(self.db._from_db_file_path(res["file_path"]))
results.append(res)
return results
except Exception as e:
logger.error(f"Name search failed: {e}")
return []