-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
402 lines (293 loc) · 10.6 KB
/
server.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
"""
router service main entry
"""
from fastapi import FastAPI, UploadFile, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from module.project_module import ResourcesType
from utils.status import StatusCode
from utils.return_type import ReturnList, ReturnDict, ReturnStatus
from kernel.engine import ENGINE_NAME, ENGINE_VERSION
from kernel.frame import FrameModel
from controller.project_controller import ProjectController
from controller.resource_controller import ResourceController
from controller.server_controller import ServerController
from controller.engine_controller import EngineController
# from typing import Optional
CONFIG_DIR = "./service.ini"
# register controllers
project_utils = ProjectController(config_dir=CONFIG_DIR)
resources_utils = ResourceController(config_dir=CONFIG_DIR)
server_utils = ServerController(config_dir=CONFIG_DIR)
engine_utils = EngineController(config_dir=CONFIG_DIR)
# end register controllers
origins = project_utils.cors_info["origins"].split(",")
app = FastAPI(
title=project_utils.version_info["name"],
description=project_utils.version_info["description"],
version=project_utils.version_info["version"],
swagger_ui_parameters={"defaultModelsExpandDepth": -1},
)
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
with open("static/ascii_logo", "r", encoding="UTF-8") as f_stream:
print("\n", f_stream.read())
print(
"\n"
f"{project_utils.version_info['name']}\n"
f"Version: {project_utils.version_info['version']}\n"
f"{ENGINE_NAME}: {ENGINE_VERSION}\n"
)
@app.get("/", include_in_schema=False)
async def read_index():
"""
server index page
"""
return FileResponse("static/index.html")
@app.get("/ok_image", include_in_schema=False)
async def ok_image():
"""
server index page image
"""
return FileResponse("static/ok.webp")
@app.post("/init_project", tags=["project"])
async def initialize_project(base_dir: str) -> ReturnDict:
"""
initialize project, create new if given directory not exist
"""
result = project_utils.init_project(base_dir=base_dir)
return result
@app.post("/get_base", tags=["project"])
async def get_project_dir(task_id: str) -> ReturnDict:
"""
get the project directory
"""
return project_utils.get_project_dir(task_id=task_id)
@app.post("/remove_task", tags=["project"])
async def remove_task(task_id: str) -> ReturnDict:
"""
remove task by task id
"""
return project_utils.remove_task(task_id=task_id)
@app.post("/list_projects", tags=["project"])
async def list_project() -> ReturnList:
"""
list all projects
"""
return project_utils.list_projects()
@app.post("/remove_project_by_id", tags=["project"])
async def remove_project_by_id(task_id: str) -> ReturnDict:
"""
remove the project
"""
return project_utils.remove_project_dir(task_id=task_id)
@app.post("/remove_project", tags=["project"])
async def remove_project(project_name: str) -> ReturnStatus:
"""
remove the project
"""
task_id = project_utils.get_task_id_by_project_name(project_name)
if task_id is not None:
project_utils.remove_task(task_id=task_id)
return server_utils.delete_project(project_name=project_name)
@app.get("/resources/{rtype}/{item_name}", tags=["resources"])
async def get_resources(
task_id: str, rtype: ResourcesType, item_name: str
) -> FileResponse:
"""
get resources file
"""
task = project_utils.get_task(task_id)
if task is None:
raise HTTPException(status_code=400, detail="task id invalid")
resource_at = resources_utils.get_resources(
task=task, rtype=rtype, item_name=item_name
)
if resource_at.status == StatusCode.FAIL:
raise HTTPException(status_code=404, detail="item not found")
return FileResponse(resource_at.content[0])
@app.post("/get_res", tags=["resources"])
async def get_resources_name(
task_id: str, rtype: ResourcesType, filter_by: str = ""
) -> ReturnList:
"""
get resources
"""
task = project_utils.get_task(task_id)
if task is None:
return ReturnList(status=StatusCode.FAIL, msg="no such task id")
result = resources_utils.get_resource_name(
task=task, rtype=rtype, filter_str=filter_by
)
return result
@app.delete("/remove_res", tags=["resources"])
async def remove_resource(
task_id: str, rtype: ResourcesType, item_name: str
) -> ReturnList:
"""
remove resources by resources name
"""
task = project_utils.get_task(task_id)
if task is None:
return ReturnList(status=StatusCode.FAIL, msg="no such task id")
return resources_utils.remove_resource(task=task, rtype=rtype, item_name=item_name)
@app.post("/rename_res", tags=["resources"])
async def rename_project(
task_id: str, rtype: ResourcesType, item_name: str, new_name: str
) -> ReturnDict:
"""
rename resources by resources name
"""
task = project_utils.get_task(task_id)
if task is None:
return ReturnDict(status=StatusCode.FAIL, msg="no such task id")
return resources_utils.rename_resource(
task=task,
rtype=rtype,
item_name=item_name,
new_name=new_name,
)
@app.post("/upload", tags=["resources"])
async def upload_file(
task_id: str, rtype: ResourcesType, file: UploadFile
) -> ReturnDict:
"""
update resources to rtype
"""
task = project_utils.get_task(task_id)
if task is None:
return ReturnDict(status=StatusCode.FAIL, msg="no such task id")
return resources_utils.upload_file(task=task, rtype=rtype, file=file)
@app.post("/upload_files", tags=["resources"])
async def upload_files(
task_id: str, rtype: ResourcesType, files: list[UploadFile]
) -> ReturnList:
"""
update multi resources to rtype
"""
task = project_utils.get_task(task_id)
if task is None:
return ReturnList(status=StatusCode.FAIL, msg="no such task id")
return resources_utils.upload_files(task=task, rtype=rtype, files=files)
@app.post("/engine/get_frame_ids", tags=["kernel"])
async def get_fids(task_id: str, chapter_name: str) -> ReturnList:
"""
get fids corresponding to the task id
"""
task = project_utils.get_task(task_id)
if task is None:
return ReturnList(status=StatusCode.FAIL, msg="no such task id")
return engine_utils.get_frame_ids(task, chapter_name)
@app.post("/engine/get_frame_names", tags=["kernel"])
async def get_frame_names(task_id: str, chapter_name: str) -> ReturnList:
"""
return the list of name of current chapter name
"""
task = project_utils.get_task(task_id)
if task is None:
return ReturnList(status=StatusCode.FAIL, msg="no such task id")
return engine_utils.get_frame_names(task, chapter_name)
@app.delete("/engine/remove_frame", tags=["kernel"])
async def remove_frame(task_id: str, fid: int) -> ReturnList:
"""
remove the frame with given fid
"""
task = project_utils.get_task(task_id)
if task is None:
return ReturnList(status=StatusCode.FAIL, msg="no such task id")
return engine_utils.remove_frame(task, fid)
@app.post("/engine/append_frame", tags=["kernel"])
async def append_frame(
task_id: str, to_chapter: str, frame_name: str = "default"
) -> ReturnList:
"""
append an empty frame to the specified chapter
@param frame_name: the name for frame
@param task_id:
@param to_chapter:
@return:
"""
task = project_utils.get_task(task_id)
if task is None:
return ReturnList(status=StatusCode.FAIL, msg="no such task id")
return engine_utils.append_frame(task, to_chapter, frame_name)
@app.post("/engine/modify_frame", tags=["kernel"])
async def modify_frame(
task_id: str, fid: int, frame_component_raw: FrameModel
) -> ReturnStatus:
"""
get fids corresponding to the task id and save the change
**music_signal define:**
--------------------
KEEP = 1
PAUSE = 2
NEXT = 3
PLAY = 4
--------------------
"""
task = project_utils.get_task(task_id)
if task is None:
return ReturnStatus(status=StatusCode.FAIL, msg="no such task id")
return engine_utils.modify_frame(task, fid, frame_component_raw)
@app.post("/engine/get_frame", tags=["kernel"])
async def get_frame(task_id: str, fid: int) -> ReturnDict:
"""
get frame by frame id
"""
task = project_utils.get_task(task_id)
if task is None:
return ReturnDict(status=StatusCode.FAIL, msg="no such task id")
return engine_utils.get_frame(task=task, fid=fid)
@app.post("/engine/get_struct", tags=["kernel"])
async def get_struct(task_id: str, chapter: str = None) -> ReturnDict:
"""
`task_id:` id of the task
`chapter:` optional,
if given, return content contains all the frame id of the current chapter,
if not , return 2d array of frame id of the project
"""
task = project_utils.get_task(task_id)
if task is None:
return ReturnDict(status=StatusCode.FAIL, msg="no such task id")
return engine_utils.render_struct(task=task, chapter_name=chapter)
@app.post("/engine/get_chapters", tags=["kernel"])
async def get_chapters(task_id: str) -> ReturnList:
"""
get all chapters
"""
task = project_utils.get_task(task_id)
if task is None:
return ReturnList(status=StatusCode.FAIL, msg="no such task id")
return engine_utils.get_chapters(task=task)
@app.post("/engine/add_chapter", tags=["kernel"])
async def add_chapters(task_id: str, chapter_name: str) -> ReturnStatus:
"""
add a chapter with given chapter name
"""
task = project_utils.get_task(task_id)
if task is None:
return ReturnList(status=StatusCode.FAIL, msg="no such task id")
return engine_utils.add_chapter(task=task, chapter_name=chapter_name)
@app.delete("/engine/remove_chapter", tags=["kernel"])
async def remove_chapter(task_id: str, chapter_name: str = None) -> ReturnStatus:
"""
get the metadata for current used kernel
"""
task = project_utils.get_task(task_id)
if task is None:
return ReturnDict(status=StatusCode.FAIL, msg="no such task id")
return engine_utils.remove_chapter(task, chapter_name)
@app.post("/engine/engine_meta", tags=["meta"])
async def engine_meta(task_id: str) -> ReturnDict:
"""
get the metadata for current used kernel
"""
task = project_utils.get_task(task_id)
if task is None:
return ReturnDict(status=StatusCode.FAIL, msg="no such task id")
return engine_utils.get_engine_meta(task)