File size: 3,067 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
ff50748
2fe2727
 
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
"""DocVault Flask application."""

import os

from flask import Flask, jsonify, send_file
from flask_cors import CORS

from server.config import DEBUG, HF_REPO_ID, MAX_CONTENT_LENGTH, SECRET_KEY
from server.routes.api import api_bp
from server.utils.logger import setup_logger


logger = setup_logger(__name__)
ROOT_DIR = os.path.dirname(os.path.dirname(__file__))


def create_app() -> Flask:
    app = Flask(__name__, static_folder=None)
    app.config["SECRET_KEY"] = SECRET_KEY
    app.config["MAX_CONTENT_LENGTH"] = MAX_CONTENT_LENGTH
    app.config["JSON_SORT_KEYS"] = False

    CORS(app, resources={r"/api/*": {"origins": "*"}})
    app.register_blueprint(api_bp)

    @app.after_request
    def add_cache_headers(response):
        if response.content_type and (
            "text/html" in response.content_type
            or "text/javascript" in response.content_type
            or "application/javascript" in response.content_type
            or "text/css" in response.content_type
        ):
            response.cache_control.max_age = 0
            response.cache_control.no_cache = True
            response.cache_control.no_store = True
            response.headers["Pragma"] = "no-cache"
            response.headers["Expires"] = "0"
        return response

    @app.errorhandler(413)
    def request_entity_too_large(error):
        return jsonify(
            {
                "success": False,
                "error": f"File too large. Maximum size: {MAX_CONTENT_LENGTH / (1024 * 1024):.0f}MB",
            }
        ), 413

    @app.route("/<path:filename>")
    def serve_static(filename: str):
        filepath = os.path.join(ROOT_DIR, filename)
        if os.path.exists(filepath) and os.path.isfile(filepath):
            return send_file(filepath)
        return jsonify({"error": "Not found"}), 404

    @app.route("/", methods=["GET"])
    def index():
        index_path = os.path.join(ROOT_DIR, "index.html")
        if os.path.exists(index_path):
            return send_file(index_path)

        return jsonify(
            {
                "name": "DocVault",
                "version": "2.0.0",
                "description": "Hugging Face Hub-backed document storage",
                "storage": "HF",
                "repo": HF_REPO_ID,
                "endpoints": {
                    "GET /api/health": "Health check",
                    "POST /api/create-folder": "Create folder",
                    "POST /api/upload": "Upload file",
                    "POST /api/delete": "Delete file or folder",
                    "POST /api/rename": "Rename file or folder",
                    "GET /api/list": "List contents",
                    "GET /api/download/<file_path>": "Download file",
                },
            }
        ), 200

    logger.info("DocVault application initialized")
    return app


if __name__ == "__main__":
    app = create_app()
    port = int(os.environ.get("PORT", 7860))
    logger.info("Starting DocVault on http://0.0.0.0:%s", port)
    app.run(debug=DEBUG, host="0.0.0.0", port=port)