j
This commit is contained in:
@@ -864,6 +864,7 @@ class API_folder_store:
|
||||
def get_metadata(self, file_hash: str) -> Optional[Dict[str, Any]]:
|
||||
"""Get metadata for a file by hash."""
|
||||
try:
|
||||
with self._db_lock:
|
||||
cursor = self.connection.cursor()
|
||||
|
||||
cursor.execute(
|
||||
@@ -1236,6 +1237,7 @@ class API_folder_store:
|
||||
def get_tags(self, file_hash: str) -> List[str]:
|
||||
"""Get all tags for a file by hash."""
|
||||
try:
|
||||
with self._db_lock:
|
||||
cursor = self.connection.cursor()
|
||||
|
||||
cursor.execute(
|
||||
@@ -1833,6 +1835,7 @@ class API_folder_store:
|
||||
def search_hash(self, file_hash: str) -> Optional[Path]:
|
||||
"""Search for a file by hash."""
|
||||
try:
|
||||
with self._db_lock:
|
||||
cursor = self.connection.cursor()
|
||||
|
||||
cursor.execute(
|
||||
@@ -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,6 +3584,7 @@ class LocalLibrarySearchOptimizer:
|
||||
return []
|
||||
|
||||
try:
|
||||
with self.db._db_lock:
|
||||
cursor = self.db.connection.cursor()
|
||||
cursor.execute(
|
||||
"""
|
||||
@@ -3606,6 +3616,7 @@ class LocalLibrarySearchOptimizer:
|
||||
return []
|
||||
|
||||
try:
|
||||
with self.db._db_lock:
|
||||
cursor = self.db.connection.cursor()
|
||||
cursor.execute(
|
||||
"""
|
||||
|
||||
@@ -69,6 +69,17 @@ logger = logging.getLogger(__name__)
|
||||
STORAGE_PATH: Optional[Path] = None
|
||||
API_KEY: Optional[str] = None # API key for authentication (None = no auth required)
|
||||
|
||||
# Cache for database connection to prevent "database is locked" on high frequency requests
|
||||
_DB_CACHE: Dict[str, Any] = {}
|
||||
|
||||
def get_db(path: Path):
|
||||
from API.folder import LocalLibrarySearchOptimizer
|
||||
p_str = str(path)
|
||||
if p_str not in _DB_CACHE:
|
||||
_DB_CACHE[p_str] = LocalLibrarySearchOptimizer(path)
|
||||
_DB_CACHE[p_str].__enter__()
|
||||
return _DB_CACHE[p_str]
|
||||
|
||||
# Try importing Flask - will be used in main() only
|
||||
try:
|
||||
from flask import Flask, request, jsonify
|
||||
@@ -199,24 +210,33 @@ def create_app():
|
||||
# ========================================================================
|
||||
|
||||
@app.route("/health", methods=["GET"])
|
||||
@require_auth()
|
||||
def health():
|
||||
"""Check server health and storage availability."""
|
||||
# Check auth manually to allow discovery even if locked
|
||||
authed = True
|
||||
if API_KEY:
|
||||
provided_key = request.headers.get("X-API-Key") or request.args.get("api_key")
|
||||
if not provided_key or provided_key != API_KEY:
|
||||
authed = False
|
||||
|
||||
status = {
|
||||
"status": "ok",
|
||||
"service": "remote_storage",
|
||||
"name": os.environ.get("MM_SERVER_NAME", "Remote Storage"),
|
||||
"storage_configured": STORAGE_PATH is not None,
|
||||
"timestamp": datetime.now().isoformat(),
|
||||
"locked": not authed and API_KEY is not None
|
||||
}
|
||||
|
||||
# If not authed but API_KEY is required, return minimal info for discovery
|
||||
if not authed and API_KEY:
|
||||
return jsonify(status), 200
|
||||
|
||||
if STORAGE_PATH:
|
||||
status["storage_path"] = str(STORAGE_PATH)
|
||||
status["storage_exists"] = STORAGE_PATH.exists()
|
||||
try:
|
||||
from API.folder import API_folder_store
|
||||
|
||||
with API_folder_store(STORAGE_PATH) as db:
|
||||
search_db = get_db(STORAGE_PATH)
|
||||
status["database_accessible"] = True
|
||||
except Exception as e:
|
||||
status["database_accessible"] = False
|
||||
@@ -233,8 +253,6 @@ def create_app():
|
||||
@require_storage()
|
||||
def search_files():
|
||||
"""Search for files by name or tag."""
|
||||
from API.folder import LocalLibrarySearchOptimizer, API_folder_store
|
||||
|
||||
query = request.args.get("q", "")
|
||||
limit = request.args.get("limit", 100, type=int)
|
||||
|
||||
@@ -242,7 +260,7 @@ def create_app():
|
||||
db_query = query if query and query != "*" else ""
|
||||
|
||||
try:
|
||||
with LocalLibrarySearchOptimizer(STORAGE_PATH) as search_db:
|
||||
search_db = get_db(STORAGE_PATH)
|
||||
results = search_db.search_by_name(db_query, limit)
|
||||
tag_results = search_db.search_by_tag(db_query, limit)
|
||||
all_results_dict = {
|
||||
@@ -251,10 +269,12 @@ def create_app():
|
||||
}
|
||||
|
||||
# Fetch tags for each result to support title extraction on client
|
||||
with API_folder_store(STORAGE_PATH) as db:
|
||||
if search_db.db:
|
||||
for res in all_results_dict.values():
|
||||
if res.get("file_path"):
|
||||
res["tag"] = db.get_tags(Path(res["file_path"]))
|
||||
file_hash = res.get("hash")
|
||||
if file_hash:
|
||||
tags = search_db.db.get_tags(file_hash)
|
||||
res["tag"] = tags
|
||||
|
||||
return (
|
||||
jsonify(
|
||||
@@ -275,17 +295,19 @@ def create_app():
|
||||
@require_storage()
|
||||
def get_file_metadata(file_hash: str):
|
||||
"""Get metadata for a specific file by hash."""
|
||||
from API.folder import API_folder_store
|
||||
|
||||
try:
|
||||
with API_folder_store(STORAGE_PATH) as db:
|
||||
search_db = get_db(STORAGE_PATH)
|
||||
db = search_db.db
|
||||
if not db:
|
||||
return jsonify({"error": "Database unavailable"}), 500
|
||||
|
||||
file_path = db.search_hash(file_hash)
|
||||
|
||||
if not file_path or not file_path.exists():
|
||||
return jsonify({"error": "File not found"}), 404
|
||||
|
||||
metadata = db.get_metadata(file_path)
|
||||
tags = db.get_tags(file_path)
|
||||
metadata = db.get_metadata(file_hash)
|
||||
tags = db.get_tags(file_hash) # Use hash string
|
||||
|
||||
return (
|
||||
jsonify(
|
||||
|
||||
Reference in New Issue
Block a user