-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
60 lines (49 loc) · 2.82 KB
/
app.py
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
from fastapi import FastAPI, Response, HTTPException
from starlette.requests import Request
import os
import subprocess
ttl_path: str = os.environ.get("TTL_PATH", "ttl")
html_path: str = os.environ.get("HTML_PATH", "html")
app = FastAPI()
@app.get("/{record_id}")
async def get_record(request: Request, record_id: str):
accept_header = request.headers.get('accept')
ttl_record_full_path: str = os.path.join(ttl_path, f"{record_id}.ttl")
html_record_full_path: str = os.path.join(html_path, f"{record_id}.xml")
if accept_header == 'text/html':
if os.path.exists(html_record_full_path) and os.path.isfile(html_record_full_path):
with open(html_record_full_path, "r") as file:
return Response(content=file.read(), media_type='text/html')
else:
raise HTTPException(status_code=404, detail=f"Record {html_record_full_path} not found as HTML and TTL files not found")
else:
if os.path.exists(ttl_record_full_path) and os.path.isfile(ttl_record_full_path):
with open(ttl_record_full_path, "r") as file:
return Response(content=file.read(), media_type='text/turtle')
else:
raise HTTPException(status_code=404, detail=f"Record {ttl_record_full_path} not found as TTL file not found")
@app.get("/html/{record_id}")
async def get_html_record(record_id: str):
html_record_full_path: str = os.path.join(html_path, f"{record_id}.xml")
ttl_record_full_path: str = os.path.join(ttl_path, f"{record_id}.ttl")
trix_full_path: str = "trix/test.trix"
if os.path.isfile(html_record_full_path):
with open(html_record_full_path, "r") as file:
return Response(content=file.read(), media_type='text/html')
elif os.path.isfile(ttl_record_full_path):
# Convert TTL to Trix to a fixed file
subprocess.run(["/bin/sh", "/app/rdfconvert.sh", "-i", "Turtle", "-o", "Trix", ttl_record_full_path, trix_full_path])
# Convert Trix to HTML(XML)
subprocess.run(["/bin/sh", "/app/xsl.sh", "-xsl:FATtoHTML.xsl", f"-s:{trix_full_path}", f"-o:{html_record_full_path}"])
with open(html_record_full_path, "r") as file:
return Response(content=file.read(), media_type='text/html')
else:
raise HTTPException(status_code=404, detail=f"Record {html_record_full_path} not found as HTML files not found")
@app.get("/ttl/{record_id}")
async def get_ttl_record(record_id: str):
ttl_record_full_path: str = os.path.join(ttl_path, f"{record_id}.ttl")
if os.path.exists(ttl_record_full_path) and os.path.isfile(ttl_record_full_path):
with open(ttl_record_full_path, "r") as file:
return Response(content=file.read(), media_type='text/turtle')
else:
raise HTTPException(status_code=404, detail=f"Record {ttl_record_full_path} not found as TTL file not found")