aboutsummaryrefslogtreecommitdiffhomepage
path: root/sec_certs_page/docs/views.py
blob: 9a6160ce4a3a65c11ef1462ea2cf2327ae2c9ce1 (plain) (blame)
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
import shutil
import zipfile
from pathlib import Path

from flask import abort, current_app, request, send_file
from redis.exceptions import LockNotOwnedError
from werkzeug.utils import safe_join

from .. import csrf, redis, sitemap
from . import docs


@docs.route("/upload", methods=["POST"])
@csrf.exempt
def upload_docs():
    if "token" not in request.args:
        return abort(403)
    if request.args["token"] != current_app.config["DOCS_AUTH_TOKEN"]:
        return abort(403)

    lock = redis.lock("upload_docs", sleep=0.1, timeout=20)
    lock.acquire()
    try:
        docs_dir = Path(current_app.instance_path) / "docs"
        shutil.rmtree(docs_dir, ignore_errors=True)
        docs_dir.mkdir()
        with zipfile.ZipFile(request.files["data"], "r") as z:
            z.extractall(docs_dir)
    finally:
        try:
            lock.release()
        except LockNotOwnedError:
            # We lost the lock in the meantime but no biggie
            pass
    return "Docs uploaded correctly"


@docs.route("", strict_slashes=False, defaults={"path": ""})
@docs.route("/<path:path>")
def serve_docs(path):
    docs_path = Path(current_app.instance_path) / "docs"
    full_path = Path(safe_join(str(docs_path), path))
    if full_path.exists():
        if full_path.is_file():
            return send_file(full_path)
        elif full_path.is_dir():
            index_path = full_path / "index.html"
            if index_path.exists() and index_path.is_file():
                return send_file(index_path)
    return abort(404)


@sitemap.register_generator
def sitemap_urls():
    yield "docs.serve_docs", {}