From 4794fd4cf07a4070e8165b34b09c6429eb5d1d89 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Thu, 27 Feb 2020 22:13:59 +0100 Subject: [PATCH] [DEV] better return value and better seek in file --- back/src/api/data.py | 81 ++++++++++++++++++++----------------- back/src/api/group.py | 10 ++--- back/src/api/saison.py | 10 ++--- back/src/api/type.py | 8 ++-- back/src/api/univers.py | 8 ++-- back/src/api/video.py | 8 ++-- back/tools/sendLocalData.py | 40 ++---------------- 7 files changed, 69 insertions(+), 96 deletions(-) diff --git a/back/src/api/data.py b/back/src/api/data.py index 5812452..61bd5fd 100644 --- a/back/src/api/data.py +++ b/back/src/api/data.py @@ -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 + '//', 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 + '/', 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) diff --git a/back/src/api/group.py b/back/src/api/group.py index ae02703..011aabb 100644 --- a/back/src/api/group.py +++ b/back/src/api/group.py @@ -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 + '/', 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 + '/', 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 + "//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) diff --git a/back/src/api/saison.py b/back/src/api/saison.py index 10ec416..6a6a37d 100644 --- a/back/src/api/saison.py +++ b/back/src/api/saison.py @@ -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 + '/', 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 + '/', 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 + "//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) diff --git a/back/src/api/type.py b/back/src/api/type.py index ca22cc2..419ba68 100644 --- a/back/src/api/type.py +++ b/back/src/api/type.py @@ -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 + '/', 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 + "//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) diff --git a/back/src/api/univers.py b/back/src/api/univers.py index 5b11461..9ece958 100644 --- a/back/src/api/univers.py +++ b/back/src/api/univers.py @@ -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 + '/', 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 + "//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) diff --git a/back/src/api/video.py b/back/src/api/video.py index 9bb325b..6f0b786 100644 --- a/back/src/api/video.py +++ b/back/src/api/video.py @@ -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 + '/', 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 + "//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) diff --git a/back/tools/sendLocalData.py b/back/tools/sendLocalData.py index de884a0..12cf706 100755 --- a/back/tools/sendLocalData.py +++ b/back/tools/sendLocalData.py @@ -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 " + tmpMax); - etk::Vector 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("============================================");