-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_utils.py
More file actions
49 lines (31 loc) · 1.09 KB
/
file_utils.py
File metadata and controls
49 lines (31 loc) · 1.09 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
import base64
import json
import os
import re
from datetime import datetime
def project_path(*parts):
root_dir = os.path.dirname(os.path.abspath(__file__))
return os.path.join(root_dir, *parts)
def get_frontend_dir():
return project_path("frontend")
def ensure_directory(path):
if not os.path.exists(path):
os.makedirs(path)
return path
def now_compact():
return datetime.now().strftime("%Y%m%d-%H%M%S")
def safe_filename(name):
return re.sub(r"[^a-zA-Z0-9._-]+", "_", name)
def save_json(path, data):
with open(path, "w", encoding="utf-8") as handle:
json.dump(data, handle, ensure_ascii=False, indent=2)
def save_binary(path, data):
with open(path, "wb") as handle:
handle.write(data)
def image_input_to_bytes(image_base64):
if image_base64.startswith("data:"):
image_base64 = image_base64.split(",", 1)[1]
return base64.b64decode(image_base64)
def image_bytes_to_data_url(image_bytes, mime_type):
encoded = base64.b64encode(image_bytes).decode("utf-8")
return "data:{0};base64,{1}".format(mime_type, encoded)