j
This commit is contained in:
@@ -161,7 +161,7 @@ def create_app():
|
||||
"Flask not installed. Install with: pip install flask flask-cors"
|
||||
)
|
||||
|
||||
from flask import Flask, request, jsonify
|
||||
from flask import Flask, request, jsonify, send_file
|
||||
from flask_cors import CORS
|
||||
|
||||
app = Flask(__name__)
|
||||
@@ -325,12 +325,32 @@ def create_app():
|
||||
logger.error(f"Get metadata error: {e}", exc_info=True)
|
||||
return jsonify({"error": f"Failed to get metadata: {str(e)}"}), 500
|
||||
|
||||
@app.route("/files/raw/<file_hash>", methods=["GET"])
|
||||
@require_auth()
|
||||
@require_storage()
|
||||
def download_file(file_hash: str):
|
||||
"""Download a raw file by hash."""
|
||||
try:
|
||||
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
|
||||
|
||||
return send_file(file_path)
|
||||
except Exception as e:
|
||||
logger.error(f"Download error: {e}", exc_info=True)
|
||||
return jsonify({"error": f"Download failed: {str(e)}"}), 500
|
||||
|
||||
@app.route("/files/index", methods=["POST"])
|
||||
@require_auth()
|
||||
@require_storage()
|
||||
def index_file():
|
||||
"""Index a new file in the storage."""
|
||||
from API.folder import API_folder_store
|
||||
from SYS.utils import sha256_file
|
||||
|
||||
data = request.get_json() or {}
|
||||
@@ -347,28 +367,32 @@ def create_app():
|
||||
if not file_path.exists():
|
||||
return jsonify({"error": "File does not exist"}), 404
|
||||
|
||||
with API_folder_store(STORAGE_PATH) as db:
|
||||
db.get_or_create_file_entry(file_path)
|
||||
search_db = get_db(STORAGE_PATH)
|
||||
db = search_db.db
|
||||
if not db:
|
||||
return jsonify({"error": "Database unavailable"}), 500
|
||||
|
||||
db.get_or_create_file_entry(file_path)
|
||||
|
||||
if tags:
|
||||
db.add_tags(file_path, tags)
|
||||
if tags:
|
||||
db.add_tags(file_path, tags)
|
||||
|
||||
if url:
|
||||
db.add_url(file_path, url)
|
||||
if url:
|
||||
db.add_url(file_path, url)
|
||||
|
||||
file_hash = sha256_file(file_path)
|
||||
file_hash = sha256_file(file_path)
|
||||
|
||||
return (
|
||||
jsonify(
|
||||
{
|
||||
"hash": file_hash,
|
||||
"path": str(file_path),
|
||||
"tags_added": len(tags),
|
||||
"url_added": len(url),
|
||||
}
|
||||
),
|
||||
201,
|
||||
)
|
||||
return (
|
||||
jsonify(
|
||||
{
|
||||
"hash": file_hash,
|
||||
"path": str(file_path),
|
||||
"tags_added": len(tags),
|
||||
"url_added": len(url),
|
||||
}
|
||||
),
|
||||
201,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Index error: {e}", exc_info=True)
|
||||
return jsonify({"error": f"Indexing failed: {str(e)}"}), 500
|
||||
|
||||
Reference in New Issue
Block a user