File size: 3,284 Bytes
f5d45fc
 
 
 
 
 
 
0a404ca
 
f5d45fc
0a404ca
c7bb80b
f5d45fc
 
 
 
 
 
 
 
0a404ca
f5d45fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse
from fastapi.middleware.cors import CORSMiddleware
import os, mimetypes, hashlib, re
from collections import defaultdict

VAULT = "vault"

app = FastAPI()
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])

@app.get("/")
def root():
    return {
        "status": "vault-online",
        "browse": "/vault",
        "read": "/vault/{path}",
        "raw": "/raw/{path}",
        "tags": "/tags",
        "graph": "/graph"
    }

@app.get("/vault")
@app.get("/vault/{path:path}")
def vault(path: str = ""):
    full = os.path.join(VAULT, path)
    if not os.path.exists(full):
        raise HTTPException(404)
    if os.path.isdir(full):
        return {
            "path": path,
            "entries": [{
                "name": f,
                "type": "dir" if os.path.isdir(os.path.join(full, f)) else "file",
                "size": os.path.getsize(os.path.join(full, f)) if os.path.isfile(os.path.join(full, f)) else None,
                "modified": os.path.getmtime(os.path.join(full, f)),
                "hash": hashlib.md5(f.encode()).hexdigest()[:8]
            } for f in sorted(os.listdir(full))]
        }
    if os.path.isfile(full):
        try:
            with open(full, "r", encoding="utf-8") as f:
                return {"path": path, "content": f.read()}
        except:
            raise HTTPException(500)
    raise HTTPException(400)

@app.get("/raw/{path:path}")
def raw(path: str):
    full = os.path.join(VAULT, path)
    if not os.path.isfile(full):
        raise HTTPException(404)
    mt, _ = mimetypes.guess_type(full)
    return FileResponse(full, media_type=mt or "application/octet-stream")

@app.get("/tags")
def extract_tags():
    tag_map = defaultdict(list)
    for root, _, files in os.walk(VAULT):
        for f in files:
            if f.endswith(".md"):
                path = os.path.join(root, f)
                try:
                    with open(path, "r", encoding="utf-8") as file:
                        content = file.read()
                        tags = re.findall(r'(?<!\w)#([\w/-]+)', content)
                        for tag in tags:
                            tag_map[tag].append(os.path.relpath(path, VAULT))
                except:
                    continue
    return dict(tag_map)

@app.get("/graph")
def link_graph():
    nodes = set()
    edges = []
    for root, _, files in os.walk(VAULT):
        for f in files:
            if f.endswith(".md"):
                src_path = os.path.relpath(os.path.join(root, f), VAULT)
                nodes.add(src_path)
                try:
                    with open(os.path.join(root, f), "r", encoding="utf-8") as file:
                        content = file.read()
                        links = re.findall(r'\[\[([^\]]+)\]\]', content)
                        for link in links:
                            target_file = link if link.endswith(".md") else f"{link}.md"
                            edges.append({"from": src_path, "to": target_file})
                            nodes.add(target_file)
                except:
                    continue
    return {
        "nodes": sorted(list(nodes)),
        "edges": edges
    }