File size: 11,187 Bytes
2fe2727
ffa0093
2fe2727
 
 
 
ffa0093
 
2fe2727
 
 
 
 
 
 
ffa0093
 
 
2fe2727
 
ffa0093
 
2fe2727
 
ffa0093
2fe2727
 
 
 
 
 
ffa0093
2fe2727
 
 
 
ffa0093
2fe2727
 
ffa0093
2fe2727
 
 
 
 
 
 
ffa0093
 
2fe2727
ffa0093
 
 
2fe2727
 
ffa0093
 
2fe2727
 
 
 
 
 
 
 
 
 
 
 
 
ffa0093
 
2fe2727
ffa0093
2fe2727
 
ffa0093
2fe2727
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffa0093
 
2fe2727
 
 
 
 
ffa0093
 
2fe2727
 
ffa0093
 
2fe2727
 
 
ffa0093
2fe2727
 
 
 
 
ffa0093
 
2fe2727
ffa0093
 
 
2fe2727
 
 
 
 
 
 
 
 
 
 
 
 
ffa0093
 
 
2fe2727
 
 
ffa0093
 
2fe2727
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffa0093
 
 
2fe2727
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffa0093
 
2fe2727
 
 
 
 
 
 
 
 
 
 
ffa0093
 
 
 
 
 
 
 
 
2fe2727
ffa0093
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""API routes for DocVault."""

import mimetypes

import requests
from flask import Blueprint, Response, jsonify, request, stream_with_context
from werkzeug.utils import secure_filename

from server import config
from server.storage.factory import get_storage
from server.utils.logger import setup_logger
from server.utils.validators import PathValidator


api_bp = Blueprint("api", __name__, url_prefix="/api")
logger = setup_logger(__name__)


def get_user_id_from_request() -> str:
    return request.headers.get("X-User-ID", config.DEFAULT_USER_ID)


def allowed_file(filename: str) -> bool:
    if "." not in filename:
        return False
    return filename.rsplit(".", 1)[1].lower() in config.ALLOWED_EXTENSIONS


def _storage():
    return get_storage()


def _item_type(storage, user_id: str, path: str) -> str | None:
    if hasattr(storage, "get_item_type"):
        return storage.get_item_type(user_id, path)
    return None


@api_bp.route("/health", methods=["GET"])
def health_check():
    return jsonify(
        {
            "storage": config.HF_STORAGE_LABEL,
            "repo": config.HF_REPO_ID,
            "status": "ok",
        }
    ), 200


@api_bp.route("/create-folder", methods=["POST"])
def create_folder():
    try:
        user_id = get_user_id_from_request()
        data = request.get_json(silent=True) or {}
        folder_path = (data.get("folder_path") or "").strip()
        if not folder_path:
            return jsonify({"success": False, "error": "folder_path is required"}), 400

        result = _storage().create_folder(user_id, folder_path)
        return jsonify(result), 201 if result.get("success") else 400
    except ValueError as exc:
        return jsonify({"success": False, "error": str(exc)}), 400
    except Exception as exc:
        logger.error("create_folder failed: %s", exc, exc_info=True)
        return jsonify({"success": False, "error": str(exc)}), 500


@api_bp.route("/upload", methods=["POST"])
@api_bp.route("/upload-file", methods=["POST"])
def upload_file():
    try:
        user_id = get_user_id_from_request()
        folder_path = (request.form.get("folder_path") or "").strip()

        if "file" not in request.files:
            return jsonify({"success": False, "error": "No file provided"}), 400

        file = request.files["file"]
        if not file.filename:
            return jsonify({"success": False, "error": "No file selected"}), 400

        filename = secure_filename(file.filename)
        if not filename:
            return jsonify({"success": False, "error": "Invalid filename"}), 400
        if not allowed_file(filename):
            return jsonify({"success": False, "error": "File type not allowed"}), 400
        if not PathValidator.is_valid_filename(filename):
            return jsonify({"success": False, "error": "Invalid filename"}), 400

        result = _storage().upload_file(user_id, folder_path, filename, file)
        return jsonify(result), 201 if result.get("success") else 400
    except ValueError as exc:
        return jsonify({"success": False, "error": str(exc)}), 400
    except Exception as exc:
        logger.error("upload failed: %s", exc, exc_info=True)
        return jsonify({"success": False, "error": str(exc)}), 500


@api_bp.route("/delete", methods=["POST"])
def delete_item():
    try:
        user_id = get_user_id_from_request()
        data = request.get_json(silent=True) or {}
        storage = _storage()

        file_path = (data.get("file_path") or "").strip()
        folder_path = (data.get("folder_path") or "").strip()
        path = (data.get("path") or "").strip()
        item_type = (data.get("type") or "").strip().lower()

        if file_path:
            result = storage.delete_file(user_id, file_path)
        elif folder_path:
            result = storage.delete_folder(user_id, folder_path)
        elif path:
            if item_type == "file":
                result = storage.delete_file(user_id, path)
            elif item_type == "folder":
                result = storage.delete_folder(user_id, path)
            else:
                resolved_type = _item_type(storage, user_id, path)
                if resolved_type == "file":
                    result = storage.delete_file(user_id, path)
                elif resolved_type == "folder":
                    result = storage.delete_folder(user_id, path)
                else:
                    result = {"success": False, "error": "Item not found", "code": "NOT_FOUND"}
        else:
            return jsonify({"success": False, "error": "path is required"}), 400

        return jsonify(result), 200 if result.get("success") else 400
    except Exception as exc:
        logger.error("delete failed: %s", exc, exc_info=True)
        return jsonify({"success": False, "error": str(exc)}), 500


@api_bp.route("/delete-file", methods=["POST"])
def delete_file_alias():
    try:
        user_id = get_user_id_from_request()
        data = request.get_json(silent=True) or {}
        file_path = (data.get("file_path") or data.get("path") or "").strip()
        if not file_path:
            return jsonify({"success": False, "error": "file_path is required"}), 400
        result = _storage().delete_file(user_id, file_path)
        return jsonify(result), 200 if result.get("success") else 400
    except Exception as exc:
        logger.error("delete_file failed: %s", exc, exc_info=True)
        return jsonify({"success": False, "error": str(exc)}), 500


@api_bp.route("/delete-folder", methods=["POST"])
def delete_folder_alias():
    try:
        user_id = get_user_id_from_request()
        data = request.get_json(silent=True) or {}
        folder_path = (data.get("folder_path") or data.get("path") or "").strip()
        if not folder_path:
            return jsonify({"success": False, "error": "folder_path is required"}), 400
        result = _storage().delete_folder(user_id, folder_path)
        return jsonify(result), 200 if result.get("success") else 400
    except Exception as exc:
        logger.error("delete_folder failed: %s", exc, exc_info=True)
        return jsonify({"success": False, "error": str(exc)}), 500


@api_bp.route("/list", methods=["GET"])
def list_contents():
    try:
        user_id = get_user_id_from_request()
        folder_path = (
            request.args.get("folder_path", request.args.get("path", "")) or ""
        ).strip()
        result = _storage().list(user_id, folder_path)
        return jsonify(result), 200 if result.get("success") else 400
    except ValueError as exc:
        return jsonify({"success": False, "error": str(exc)}), 400
    except Exception as exc:
        logger.error("list failed: %s", exc, exc_info=True)
        return jsonify({"success": False, "error": str(exc)}), 500


@api_bp.route("/rename", methods=["POST"])
def rename_item():
    try:
        user_id = get_user_id_from_request()
        data = request.get_json(silent=True) or {}
        item_path = (data.get("item_path") or "").strip()
        new_name = (data.get("new_name") or "").strip()
        if not item_path or not new_name:
            return jsonify({"success": False, "error": "item_path and new_name are required"}), 400

        storage = _storage()
        item_type = _item_type(storage, user_id, item_path)
        if item_type == "file":
            result = storage.rename_file(user_id, item_path, new_name)
        elif item_type == "folder":
            result = storage.rename_folder(user_id, item_path, new_name)
        else:
            result = {"success": False, "error": "Item not found", "code": "NOT_FOUND"}

        return jsonify(result), 200 if result.get("success") else 400
    except ValueError as exc:
        return jsonify({"success": False, "error": str(exc)}), 400
    except Exception as exc:
        logger.error("rename failed: %s", exc, exc_info=True)
        return jsonify({"success": False, "error": str(exc)}), 500


@api_bp.route("/storage-stats", methods=["GET"])
def storage_stats():
    try:
        user_id = get_user_id_from_request()
        result = _storage().get_stats(user_id)
        return jsonify(result), 200 if result.get("success") else 400
    except Exception as exc:
        logger.error("storage_stats failed: %s", exc, exc_info=True)
        return jsonify({"success": False, "error": str(exc)}), 500


@api_bp.route("/download/<path:file_path>", methods=["GET"])
def download_file(file_path: str):
    try:
        user_id = get_user_id_from_request()
        download_info = _storage().download(user_id, file_path)
        headers = {"Authorization": f"Bearer {config.HF_TOKEN}"}
        upstream = requests.get(download_info["url"], headers=headers, stream=True, timeout=30)
        upstream.raise_for_status()

        content_type = upstream.headers.get("Content-Type") or mimetypes.guess_type(file_path)[0]
        if not content_type:
            content_type = "application/octet-stream"

        filename = file_path.split("/")[-1]
        disposition = "attachment" if request.args.get("download") == "true" else "inline"

        return Response(
            stream_with_context(upstream.iter_content(chunk_size=8192)),
            content_type=content_type,
            headers={"Content-Disposition": f'{disposition}; filename="{filename}"'},
        )
    except FileNotFoundError as exc:
        return jsonify({"success": False, "error": str(exc)}), 404
    except Exception as exc:
        logger.error("download failed: %s", exc, exc_info=True)
        return jsonify({"success": False, "error": str(exc)}), 500


@api_bp.route("/history", methods=["GET"])
def get_history():
    try:
        user_id = get_user_id_from_request()
        path = (request.args.get("path") or "").strip()
        if not path:
            return jsonify({"success": False, "error": "path is required"}), 400
        return jsonify({"success": True, "history": _storage().get_history(user_id, path)}), 200
    except Exception as exc:
        logger.error("history failed: %s", exc, exc_info=True)
        return jsonify({"success": False, "error": str(exc)}), 500


@api_bp.route("/restore", methods=["POST"])
def restore_version():
    try:
        user_id = get_user_id_from_request()
        data = request.get_json(silent=True) or {}
        path = (data.get("path") or "").strip()
        revision = (data.get("revision") or "").strip()
        as_copy = bool(data.get("as_copy", False))
        if not path or not revision:
            return jsonify({"success": False, "error": "path and revision are required"}), 400
        result = _storage().restore(user_id, path, revision, as_copy=as_copy)
        return jsonify(result), 200 if result.get("success") else 400
    except Exception as exc:
        logger.error("restore failed: %s", exc, exc_info=True)
        return jsonify({"success": False, "error": str(exc)}), 500


@api_bp.errorhandler(404)
def not_found(error):
    return jsonify({"success": False, "error": "Endpoint not found"}), 404


@api_bp.errorhandler(500)
def internal_error(error):
    logger.error("Internal server error: %s", error)
    return jsonify({"success": False, "error": "Internal server error"}), 500