[DEV] better return value and better seek in file

This commit is contained in:
Edouard DUPIN 2020-02-27 22:13:59 +01:00
parent be92c15b9f
commit 4794fd4cf0
7 changed files with 69 additions and 96 deletions

View File

@ -107,7 +107,7 @@ def add(_app, _name_api):
value = data_global_elements.get_interface(_name_api).find("sha512", sha512)
if value != None:
return response.json(value)
raise ServerError("No data found", status_code=404)
return response.HTTPResponse("No data found", status=404)
@elem_blueprint.post('/' + _name_api, strict_slashes=True, stream=True)
@ -187,6 +187,13 @@ def add(_app, _name_api):
await _response.write(json.dumps(return_bdd, sort_keys=True, indent=4))
return response.stream(streaming, content_type='application/json')
@elem_blueprint.get('/' + _name_api + '/<id:int>/<custom_user_video_name:string>', strict_slashes=True)
@doc.summary("get a specific resource")
@doc.description("Get a resource with all the needed datas ... It permeit seek for video stream.")
@doc.produces(content_type='application/json')
async def retrive2(request, id, custom_user_video_name):
return retrive2(request, id)
@elem_blueprint.get('/' + _name_api + '/<id:int>', strict_slashes=True)
@doc.summary("get a specific resource")
@doc.description("Get a resource with all the needed datas ... It permeit seek for video stream.")
@ -206,45 +213,43 @@ def add(_app, _name_api):
'Content-Type': value["mime_type"],
'Accept-Ranges': 'Accept-Ranges: bytes'
}
if tools.exist(filename) == False:
return response.HTTPResponse(status=404)
file_length = tools.file_size(filename);
try:
with open(filename, 'rb') as fff:
range_start = None
range_end = None
fff.seek(0, 2)
file_length = fff.tell()
fff.seek(0)
try:
range_ = '0-' + str(file_length)
if 'range' in request.headers:
range_ = request.headers['range'].split('=')[1]
range_split = range_.split('-')
range_start = int(range_split[0])
fff.seek(range_start)
range_end = int(range_split[1])
except ValueError:
pass
if range_start and range_start != 0:
if not range_end:
range_end = file_length
read_length = range_end - range_start
else:
range_start = 0
read_length = file_length
range_start = None
range_end = None
try:
range_ = '0-' + str(file_length)
if 'range' in request.headers:
range_ = request.headers['range'].split('=')[1]
range_split = range_.split('-')
range_start = int(range_split[0])
range_end = int(range_split[1])
except ValueError:
pass
if range_start and range_start != 0:
if not range_end:
range_end = file_length
fff.seek(range_start)
headers['Content-Length'] = read_length
headers['Content-Range'] = f'bytes {range_start}-{range_end-1}/{file_length}'
async def streaming_fn(response):
with open(filename, 'rb') as fff:
chunk_size = 8192
current_offset = range_start
while (current_offset < file_length):
chunk_start = current_offset
fff.seek(current_offset)
chunk_data = fff.read(min(chunk_size, file_length - current_offset))
current_offset += chunk_size
await response.write(chunk_data)
return response.stream(streaming_fn, headers=headers, status=206)
read_length = range_end - range_start
else:
range_start = 0
read_length = file_length
range_end = file_length
headers['Content-Length'] = read_length
headers['Content-Range'] = f'bytes {range_start}-{range_end-1}/{file_length}'
async def streaming_fn(response):
with open(filename, 'rb') as fff:
chunk_size = 8192
current_offset = range_start
while (current_offset < file_length):
chunk_start = current_offset
fff.seek(current_offset)
chunk_data = fff.read(min(chunk_size, file_length - current_offset))
current_offset += chunk_size
await response.write(chunk_data)
return response.stream(streaming_fn, headers=headers, status=206)
except FileNotFoundError:
return response.HTTPResponse(status=404)

View File

@ -103,7 +103,7 @@ def add(_app, _name_api):
value = data_global_elements.get_interface(_name_api).find("name", request.json["name"])
if value != None:
return response.json(value)
raise ServerError("No data found", status_code=404)
return response.HTTPResponse("No data found", status=404)
@elem_blueprint.get('/' + _name_api + '/<id:int>', strict_slashes=True)
@doc.summary("Show resources")
@ -113,7 +113,7 @@ def add(_app, _name_api):
value = data_global_elements.get_interface(_name_api).get(id)
if value != None:
return response.json(value)
raise ServerError("No data found", status_code=404)
return response.HTTPResponse("No data found", status=404)
@elem_blueprint.put('/' + _name_api + '/<id:int>', strict_slashes=True)
@doc.summary("Update resource")
@ -131,7 +131,7 @@ def add(_app, _name_api):
ret = data_global_elements.get_interface(_name_api).delete(id)
if ret == True:
return response.json({})
raise ServerError("No data found", status_code=404)
return response.HTTPResponse("No data found", status=404)
@elem_blueprint.post('/' + _name_api + "/<id:int>/add_cover", strict_slashes=True)
@doc.summary("Add cover on group")
@ -141,13 +141,13 @@ def add(_app, _name_api):
async def create_cover(request, id):
for type_key in ["data_id"]:
if type_key not in request.json.keys():
raise ServerError("Bad Request: Missing Key '" + type_key + "'", status_code=400)
return response.HTTPResponse("Bad Request: Missing Key '" + type_key + "'", status=400)
data = {}
data["node_id"] = id
data["data_id"] = request.json["data_id"]
value = data_global_elements.get_interface(_name_api).get(id)
if value == None:
raise ServerError("No data found", status_code=404)
return response.HTTPResponse("No data found", status=404)
data_global_elements.get_interface(data_global_elements.API_COVER).post(data)
value = data_global_elements.get_interface(_name_api).get(id)
return response.json(value)

View File

@ -108,7 +108,7 @@ def add(_app, _name_api):
value = data_global_elements.get_interface(_name_api).find2("parent_id", request.json["parent_id"], "name", request.json["name"])
if value != None:
return response.json(value)
raise ServerError("No data found", status_code=404)
return response.HTTPResponse("No data found", status=404)
@elem_blueprint.get('/' + _name_api + '/<id:int>', strict_slashes=True)
@doc.summary("Show resources")
@ -118,7 +118,7 @@ def add(_app, _name_api):
value = data_global_elements.get_interface(_name_api).get(id)
if value != None:
return response.json(value)
raise ServerError("No data found", status_code=404)
return response.HTTPResponse("No data found", status=404)
@elem_blueprint.put('/' + _name_api + '/<id:int>', strict_slashes=True)
@doc.summary("Update resource")
@ -136,7 +136,7 @@ def add(_app, _name_api):
ret = data_global_elements.get_interface(_name_api).delete(id)
if ret == True:
return response.json({})
raise ServerError("No data found", status_code=404)
return response.HTTPResponse("No data found", status=404)
@elem_blueprint.post('/' + _name_api + "/<id:int>/add_cover", strict_slashes=True)
@doc.summary("Add cover on video")
@ -146,13 +146,13 @@ def add(_app, _name_api):
async def create_cover(request, id):
for type_key in ["data_id"]:
if type_key not in request.json.keys():
raise ServerError("Bad Request: Missing Key '" + type_key + "'", status_code=400)
return response.HTTPResponse("Bad Request: Missing Key '" + type_key + "'", status=400)
data = {}
data["node_id"] = id
data["data_id"] = request.json["data_id"]
value = data_global_elements.get_interface(_name_api).get(id)
if value == None:
raise ServerError("No data found", status_code=404)
return response.HTTPResponse("No data found", status=404)
data_global_elements.get_interface(data_global_elements.API_COVER).post(data)
value = data_global_elements.get_interface(_name_api).get(id)
return response.json(value)

View File

@ -99,7 +99,7 @@ def add(_app, _name_api):
value = data_global_elements.get_interface(_name_api).get(id)
if value != None:
return response.json(value)
raise ServerError("No data found", status_code=404)
return response.HTTPResponse("No data found", status=404)
@elem_blueprint.put('/' + _name_api + '/<id:int>', strict_slashes=True)
@doc.summary("Update resource")
@ -117,7 +117,7 @@ def add(_app, _name_api):
ret = data_global_elements.get_interface(_name_api).delete(id)
if ret == True:
return response.json({})
raise ServerError("No data found", status_code=404)
return response.HTTPResponse("No data found", status=404)
@elem_blueprint.post('/' + _name_api + "/<id:int>/add_cover", strict_slashes=True)
@doc.summary("Add cover on video")
@ -127,13 +127,13 @@ def add(_app, _name_api):
async def create_cover(request, id):
for type_key in ["data_id"]:
if type_key not in request.json.keys():
raise ServerError("Bad Request: Missing Key '" + type_key + "'", status_code=400)
return response.HTTPResponse("Bad Request: Missing Key '" + type_key + "'", status=400)
data = {}
data["node_id"] = id
data["data_id"] = request.json["data_id"]
value = data_global_elements.get_interface(_name_api).get(id)
if value == None:
raise ServerError("No data found", status_code=404)
return response.HTTPResponse("No data found", status=404)
data_global_elements.get_interface(data_global_elements.API_COVER).post(data)
value = data_global_elements.get_interface(_name_api).get(id)
return response.json(value)

View File

@ -97,7 +97,7 @@ def add(_app, _name_api):
value = data_global_elements.get_interface(_name_api).get(id)
if value != None:
return response.json(value)
raise ServerError("No data found", status_code=404)
return response.HTTPResponse("No data found", status=404)
@elem_blueprint.put('/' + _name_api + '/<id:int>', strict_slashes=True)
@doc.summary("Update resource")
@ -115,7 +115,7 @@ def add(_app, _name_api):
ret = data_global_elements.get_interface(_name_api).delete(id)
if ret == True:
return response.json({})
raise ServerError("No data found", status_code=404)
return response.HTTPResponse("No data found", status=404)
@elem_blueprint.post('/' + _name_api + "/<id:int>/add_cover", strict_slashes=True)
@doc.summary("Add cover on univers")
@ -125,13 +125,13 @@ def add(_app, _name_api):
async def create_cover(request, id):
for type_key in ["data_id"]:
if type_key not in request.json.keys():
raise ServerError("Bad Request: Missing Key '" + type_key + "'", status_code=400)
return response.HTTPResponse("Bad Request: Missing Key '" + type_key + "'", status=400)
data = {}
data["node_id"] = id
data["data_id"] = request.json["data_id"]
value = data_global_elements.get_interface(_name_api).get(id)
if value == None:
raise ServerError("No data found", status_code=404)
return response.HTTPResponse("No data found", status=404)
data_global_elements.get_interface(data_global_elements.API_COVER).post(data)
value = data_global_elements.get_interface(_name_api).get(id)
return response.json(value)

View File

@ -223,7 +223,7 @@ def add(_app, _name_api):
tmp = copy.deepcopy(value)
tmp["generated_name"] = generated_name
return response.json(tmp)
raise ServerError("No data found", status_code=404)
return response.HTTPResponse("No data found", status=404)
@elem_blueprint.put('/' + _name_api + '/<id:int>', strict_slashes=True)
@doc.summary("Update resource")
@ -241,7 +241,7 @@ def add(_app, _name_api):
ret = data_global_elements.get_interface(_name_api).delete(id)
if ret == True:
return response.json({})
raise ServerError("No data found", status_code=404)
return response.HTTPResponse("No data found", status=404)
@elem_blueprint.post('/' + _name_api + "/<id:int>/add_cover", strict_slashes=True)
@doc.summary("Add cover on video")
@ -251,13 +251,13 @@ def add(_app, _name_api):
async def create_cover(request, id):
for type_key in ["data_id"]:
if type_key not in request.json.keys():
raise ServerError("Bad Request: Missing Key '" + type_key + "'", status_code=400)
return response.HTTPResponse("Bad Request: Missing Key '" + type_key + "'", status=400)
data = {}
data["node_id"] = id
data["data_id"] = request.json["data_id"]
value = data_global_elements.get_interface(_name_api).get(id)
if value == None:
raise ServerError("No data found", status_code=404)
return response.HTTPResponse("No data found", status=404)
data_global_elements.get_interface(data_global_elements.API_COVER).post(data)
value = data_global_elements.get_interface(_name_api).get(id)

View File

@ -853,13 +853,12 @@ elif requestAction == "tree":
debug.info("============================================");
debug.info("== tree files: ");
debug.info("============================================");
list_types = requests.get(get_base_url() + "type")
if list_types.status_code != 200:
debug.warning(" !! ca, ot get type list ... " + str(list_types.status_code) + "")
for elem in list_types.json():
for elem in result_list_types:
debug.info("-------------------------------------------------")
debug.info(" " + str(elem["name"]))
debug.info("-------------------------------------------------")
# Does not work anymore...
"""
# First get all the groups:
result_groups = requests.get(get_base_url() + "type/" + str(elem["id"]) + "/group")
if result_groups.status_code == 200:
@ -904,39 +903,8 @@ elif requestAction == "tree":
show_video(elem_video_id, 1)
else:
debug.warning("\t\tList video solo: !!!!!! " + str(result_video_solo.status_code) + "")
"""
"""
uint32_t count = remoteServiceVideo.count().wait().get();
debug.debug("have " + count + " medias");
for (uint32_t iii=0; iii<count ; iii += 1024:
uint32_t tmpMax = etk::min(iii + 1024, count);
debug.debug("read section " + iii + " -> " + tmpMax);
etk::Vector<uint32_t> list = remoteServiceVideo.getIds(iii, tmpMax).wait().get();
for (auto& it : list:
# Get the media
zeus::ProxyMedia media = remoteServiceVideo.get(it).waitFor(echrono::seconds(2000)).get();
if media.exist() == False:
debug.error("get media error");
return -1;
debug.debug(" Get title ...");
etk::String name = media.getMetadata("title").wait().get();
debug.debug(" Get series-name ...");
etk::String serie = media.getMetadata("series-name").wait().get();
debug.debug(" Get episode ...");
etk::String episode = media.getMetadata("episode").wait().get();
debug.debug(" Get saison ...");
etk::String saison = media.getMetadata("saison").wait().get();
etk::String outputDesc = "";
if serie != "":
outputDesc += serie + "-";
if saison != "":
outputDesc += "s" + saison + "-";
if episode != "":
outputDesc += "e" + episode + "-";
outputDesc += name;
debug.info("[" + it + "] '" + outputDesc + "'");
"""
debug.info("============================================");
debug.info("== DONE ==");
debug.info("============================================");