97 lines
2.3 KiB
Python
97 lines
2.3 KiB
Python
from enum import IntEnum
|
|
from pathlib import Path
|
|
from falcon import HTTPNotFound, HTTP_CREATED
|
|
|
|
class TaskStatus(IntEnum):
|
|
Created = 0
|
|
Starting = 1
|
|
Running = 2
|
|
Finished = 3
|
|
Error = 4
|
|
|
|
tasks = [
|
|
{
|
|
"TaskId":0,
|
|
"Progress":0,
|
|
"SourceStreamId":0,
|
|
"DestinationStreamId":1,
|
|
"TaskStatus":TaskStatus.Created
|
|
}
|
|
]
|
|
|
|
streams = [
|
|
{
|
|
"StreamId":0,
|
|
"MimeType":"application/x-shockwave-flash",
|
|
"Uris":[
|
|
{"Uri":"http://10.42.0.1:8080/Files/00000000.swf"},
|
|
{"Uri":"http://10.42.0.1:8080/Files/00015000.swf"},
|
|
{"Uri":"http://10.42.0.1:8080/Files/00030000.swf"}
|
|
]
|
|
},
|
|
{
|
|
"StreamId":1,
|
|
"MimeType":"video/x-m4v",
|
|
"Uris":[]
|
|
}
|
|
]
|
|
|
|
class TaskResource():
|
|
def on_get(self, req, resp):
|
|
for task in tasks:
|
|
if task["TaskStatus"] in (TaskStatus.Created, TaskStatus.Error):
|
|
#task["TaskStatus"] = TaskStatus.Starting
|
|
resp.json = task
|
|
return
|
|
|
|
else:
|
|
raise HTTPNotFound()
|
|
|
|
class TasksCollection():
|
|
|
|
def on_get(self, req, resp, TaskId=None):
|
|
if TaskId:
|
|
TaskId = int(TaskId)
|
|
try:
|
|
resp.json = tasks[TaskId]
|
|
except IndexError:
|
|
raise HTTPNotFound()
|
|
else:
|
|
resp.json = tasks
|
|
|
|
def on_post(self, req, resp):
|
|
task = req.json
|
|
tasks.append(task)
|
|
|
|
|
|
|
|
class StreamCollection():
|
|
|
|
def on_get(self, req, resp, VideoUuid, StreamId):
|
|
print(VideoUuid, StreamId)
|
|
StreamId = int(StreamId)
|
|
try:
|
|
resp.json = streams[StreamId]
|
|
except IndexError:
|
|
raise HTTPNotFound()
|
|
|
|
class FileCollection():
|
|
def __init__(self, path):
|
|
self.path = path
|
|
|
|
def on_get(self, req, resp, FileId):
|
|
File = Path(FileId).name
|
|
File = self.path/File
|
|
try:
|
|
with File.open("rb") as f:
|
|
resp.content_type = "application/octet-stream"
|
|
resp.body= f.read()
|
|
except FileNotFoundError:
|
|
raise HTTPNotFound()
|
|
def on_post(self, req, resp):
|
|
f = req.get_param("file")
|
|
with open(self.path/f.filename, "wb") as fo:
|
|
fo.write(f.file.read())
|
|
resp.status = HTTP_CREATED
|
|
resp.content_location = f"/Files/{f.filename}"
|