Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
113 lines (99 loc) · 3.25 KB
/
Copy pathmain.py
File metadata and controls
113 lines (99 loc) · 3.25 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import traceback
import uuid
from os import listdir
from os.path import getsize
from time import time
from typing import Optional
from fastapi import FastAPI, status
from fastapi.responses import JSONResponse
from utils import *
from colors import CSS_COLORS
app = FastAPI()
@app.get("/color/{filetype}", status_code=status.HTTP_200_OK)
def _generate_color_pdf(filetype: str, width: int, height: int, color: str):
start = time()
filetype = filetype.lower()
response = {
"color": color,
"size": f"{width}x{height}",
"filesize": 0,
"base64": "",
"time": 0,
}
if (color.lower() not in CSS_COLORS) and ("#" not in color):
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={"msg": "Please specify a valid color."},
)
file_name = uuid.uuid4()
svg_path = f"/tmp/{file_name}.svg"
export_path = f"/tmp/{file_name}.{filetype}"
generate_color_svg(size=[width, height], color=color, out=svg_path)
if filetype == "pdf":
svg2pdf(svg_path, export_path)
elif filetype == "png":
svg2png(svg_path, export_path, 50)
response["base64"] = get_base64(export_path)
response["filesize"] = getsize(export_path)
response["time"] = time() - start
return response
@app.get("/font/{filetype}", status_code=status.HTTP_200_OK)
def _generate_font_pdf(
filetype: str,
fontname: str,
text: str,
color: str = "black",
iszip: bool = True,
weight: Optional[str] = None,
dpi: int = 72,
):
start = time()
response = {
"font": fontname,
"base64": "",
"color": color,
"weight": weight,
"dl_time": None,
}
filetype = filetype.lower()
font_path = download_font(fontname, iszip, weight)
if isinstance(font_path, dict):
response["dl_time"] = font_path["download_time"]
font_path = font_path["path"]
if isinstance(font_path, list):
response["weight_list"] = font_path
response["selected_weight"] = font_path[0]
font_path = font_path[0]
if "error" in font_path:
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content={"msg": "Download failed.", "status_code": font_path[6:]},
)
file_name = uuid.uuid4()
svg_path = f"/tmp/{file_name}.svg"
export_path = f"/tmp/{file_name}.{filetype}"
generate_font_svg(font_path, text, 32, svg_path, color)
try:
if filetype == "pdf":
svg2pdf(svg_path, export_path)
elif filetype == "png":
svg2png(svg_path, export_path, dpi)
except Exception as e:
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content={"msg": traceback.format_exception_only((type(e), e))[0][:-2]},
)
response["base64"] = get_base64(export_path)
response["filesize"] = getsize(export_path)
response["time"] = time() - start
return response
@app.get("/debug/ls")
@app.get("/debug/ls/{path}")
def _file_tree(path: str = "."):
try:
return {"result": listdir(path)}
except:
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={"msg": "path is not found."},
)