Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,95 @@
|
|
1 |
-
from fastapi import FastAPI,
|
2 |
-
|
3 |
-
from
|
|
|
|
|
|
|
|
|
4 |
|
5 |
app = FastAPI()
|
6 |
-
|
7 |
-
os.makedirs(os.path.dirname(LOG), exist_ok=True)
|
8 |
|
9 |
@app.get("/")
|
10 |
-
def
|
11 |
-
return {
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
except:
|
19 |
-
return {"err": "invalid json"}
|
20 |
-
ip = req.headers.get("x-forwarded-for", "?.?.?.?")
|
21 |
-
ua = req.headers.get("user-agent", "")
|
22 |
-
ref = req.headers.get("referer", "")
|
23 |
-
ts = datetime.utcnow().isoformat()
|
24 |
-
raw = json.dumps(j)
|
25 |
-
sig = hashlib.md5(raw.encode()).hexdigest()
|
26 |
-
marks = {
|
27 |
-
"jwt": re.findall(r'eyJ[\w-]+\.[\w-]+\.[\w-]+', raw),
|
28 |
-
"csrf": re.findall(r'csrf[_\-]?(token)?[\'"]?\s*[:=]\s*[\'"]?([\w\-_]{8,})', raw, re.I),
|
29 |
-
"session": re.findall(r'(?:sessionid|sid)[=:]+([\w\-_.]{10,})', raw, re.I)
|
30 |
}
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from fastapi.responses import FileResponse
|
3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
4 |
+
import os, mimetypes, hashlib, re
|
5 |
+
from collections import defaultdict
|
6 |
+
|
7 |
+
VAULT = "vault"
|
8 |
|
9 |
app = FastAPI()
|
10 |
+
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
|
|
11 |
|
12 |
@app.get("/")
|
13 |
+
def root():
|
14 |
+
return {
|
15 |
+
"status": "vault-online",
|
16 |
+
"browse": "/vault",
|
17 |
+
"read": "/vault/{path}",
|
18 |
+
"raw": "/raw/{path}",
|
19 |
+
"tags": "/tags",
|
20 |
+
"graph": "/graph"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
}
|
22 |
+
|
23 |
+
@app.get("/vault")
|
24 |
+
@app.get("/vault/{path:path}")
|
25 |
+
def vault(path: str = ""):
|
26 |
+
full = os.path.join(VAULT, path)
|
27 |
+
if not os.path.exists(full):
|
28 |
+
raise HTTPException(404)
|
29 |
+
if os.path.isdir(full):
|
30 |
+
return {
|
31 |
+
"path": path,
|
32 |
+
"entries": [{
|
33 |
+
"name": f,
|
34 |
+
"type": "dir" if os.path.isdir(os.path.join(full, f)) else "file",
|
35 |
+
"size": os.path.getsize(os.path.join(full, f)) if os.path.isfile(os.path.join(full, f)) else None,
|
36 |
+
"modified": os.path.getmtime(os.path.join(full, f)),
|
37 |
+
"hash": hashlib.md5(f.encode()).hexdigest()[:8]
|
38 |
+
} for f in sorted(os.listdir(full))]
|
39 |
+
}
|
40 |
+
if os.path.isfile(full):
|
41 |
+
try:
|
42 |
+
with open(full, "r", encoding="utf-8") as f:
|
43 |
+
return {"path": path, "content": f.read()}
|
44 |
+
except:
|
45 |
+
raise HTTPException(500)
|
46 |
+
raise HTTPException(400)
|
47 |
+
|
48 |
+
@app.get("/raw/{path:path}")
|
49 |
+
def raw(path: str):
|
50 |
+
full = os.path.join(VAULT, path)
|
51 |
+
if not os.path.isfile(full):
|
52 |
+
raise HTTPException(404)
|
53 |
+
mt, _ = mimetypes.guess_type(full)
|
54 |
+
return FileResponse(full, media_type=mt or "application/octet-stream")
|
55 |
+
|
56 |
+
@app.get("/tags")
|
57 |
+
def extract_tags():
|
58 |
+
tag_map = defaultdict(list)
|
59 |
+
for root, _, files in os.walk(VAULT):
|
60 |
+
for f in files:
|
61 |
+
if f.endswith(".md"):
|
62 |
+
path = os.path.join(root, f)
|
63 |
+
try:
|
64 |
+
with open(path, "r", encoding="utf-8") as file:
|
65 |
+
content = file.read()
|
66 |
+
tags = re.findall(r'(?<!\w)#([\w/-]+)', content)
|
67 |
+
for tag in tags:
|
68 |
+
tag_map[tag].append(os.path.relpath(path, VAULT))
|
69 |
+
except:
|
70 |
+
continue
|
71 |
+
return dict(tag_map)
|
72 |
+
|
73 |
+
@app.get("/graph")
|
74 |
+
def link_graph():
|
75 |
+
nodes = set()
|
76 |
+
edges = []
|
77 |
+
for root, _, files in os.walk(VAULT):
|
78 |
+
for f in files:
|
79 |
+
if f.endswith(".md"):
|
80 |
+
src_path = os.path.relpath(os.path.join(root, f), VAULT)
|
81 |
+
nodes.add(src_path)
|
82 |
+
try:
|
83 |
+
with open(os.path.join(root, f), "r", encoding="utf-8") as file:
|
84 |
+
content = file.read()
|
85 |
+
links = re.findall(r'\[\[([^\]]+)\]\]', content)
|
86 |
+
for link in links:
|
87 |
+
target_file = link if link.endswith(".md") else f"{link}.md"
|
88 |
+
edges.append({"from": src_path, "to": target_file})
|
89 |
+
nodes.add(target_file)
|
90 |
+
except:
|
91 |
+
continue
|
92 |
+
return {
|
93 |
+
"nodes": sorted(list(nodes)),
|
94 |
+
"edges": edges
|
95 |
+
}
|