From b7719edccf3cb355a44dd5c6962b4509ed77a140 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Thu, 2 Jan 2020 22:43:06 +0100 Subject: [PATCH] [DEV] better interface --- back/Dockerfile | 23 ++++++++++-------- back/docker-compose.yaml | 2 ++ back/src/api/data.py | 24 +++++++++++++------ back/src/app_video.py | 2 +- back/src/data_global_elements.py | 2 +- back/src/data_interface.py | 3 +++ back/tools/sendLocalData.py | 7 ++++-- .../element-video/element-video.component.ts | 2 +- .../video-detail/video-detail.component.ts | 2 +- 9 files changed, 44 insertions(+), 23 deletions(-) diff --git a/back/Dockerfile b/back/Dockerfile index 5b06467..23c007e 100755 --- a/back/Dockerfile +++ b/back/Dockerfile @@ -1,26 +1,29 @@ -FROM python:alpine +FROM python:alpine3.6 -RUN pip install --upgrade pip +RUN apk update \ + && apk add build-base -RUN pip install sanic +RUN pip3 install --upgrade pip -RUN pip install sanic-cors +RUN pip3 install sanic -RUN pip install sanic-simple-swagger +RUN pip3 install sanic-cors -RUN pip install python-dateutil +RUN pip3 install sanic-simple-swagger -RUN pip install realog +RUN pip3 install python-dateutil -RUN pip install python-magic +RUN pip3 install realog -RUN pip install pymediainfo +RUN pip3 install python-magic + +RUN pip3 install pymediainfo EXPOSE 80 ADD src /application/ WORKDIR /application/ -CMD ["python", "-u", "./app_video.py"] +CMD ["python3", "-u", "./app_video.py"] diff --git a/back/docker-compose.yaml b/back/docker-compose.yaml index 78fedb1..2a6e62d 100755 --- a/back/docker-compose.yaml +++ b/back/docker-compose.yaml @@ -7,4 +7,6 @@ services: container_name: video_rest_api ports: - 15080:80 + volumes: + - ./data:/application/data diff --git a/back/src/api/data.py b/back/src/api/data.py index ca6601e..3d42c22 100644 --- a/back/src/api/data.py +++ b/back/src/api/data.py @@ -64,6 +64,16 @@ def add(_app, _name_api): data_global_elements.get_interface(_name_api).set_data_model(DataModelBdd) + @elem_blueprint.get('/' + _name_api + '/exist/', strict_slashes=True) + @doc.summary("check resource existance") + @doc.description("simply check if the resource is already uploaded.") + @doc.produces(content_type='application/json') + async def check_existance(request, sha512): + value = data_global_elements.get_interface(_name_api).gets_where(select=[["==", "sha512", sha512]], filter=["id"]) + if value != None: + return response.json({"found":True}) + raise ServerError("No data found", status_code=404) + @elem_blueprint.post('/' + _name_api, strict_slashes=True, stream=True) @doc.summary("send new file data") @@ -85,6 +95,7 @@ def add(_app, _name_api): file_stream = open(temporary_file,"wb") sha1 = hashlib.sha512() while True: + debug.warning("ploufffff " + str(dir(_request.stream))) body = await _request.stream.read() if body is None: debug.warning("empty body"); @@ -140,22 +151,21 @@ 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) + @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 retrive(request, id): - debug.warning("Request data media 2 : " + id); + debug.warning("Request data media 2 : " + str(id)); + """ if id[-4:] == ".mp4": id = id[:-4] if id[-4:] == ".mkv": id = id[:-4] - if id[-4:] == ".avi": - id = id[:-4] - if id[-4:] == ".ts": - id = id[:-3] - filename = os.path.join(_app.config['REST_MEDIA_DATA'], id, "video") + """ + filename = os.path.join(_app.config['REST_MEDIA_DATA'], str(id), "video") value = data_global_elements.get_interface(_name_api).get(id) + debug.info("plouuuuuuf " + str(value)) headers = { 'Content-Type': value["mime_type"], 'Accept-Ranges': 'Accept-Ranges: bytes' diff --git a/back/src/app_video.py b/back/src/app_video.py index b1d87f6..3359625 100755 --- a/back/src/app_video.py +++ b/back/src/app_video.py @@ -59,7 +59,7 @@ if "REST_MEDIA_DATA" not in app.config.keys(): if "REST_DATA" not in app.config.keys(): app.config['REST_DATA'] = "data" if "REST_HOST" not in app.config.keys(): - app.config['REST_HOST'] = "localhost" + app.config['REST_HOST'] = "0.0.0.0" if "REST_PORT" not in app.config.keys(): app.config['REST_PORT'] = "80" diff --git a/back/src/data_global_elements.py b/back/src/data_global_elements.py index c040392..602fc01 100644 --- a/back/src/data_global_elements.py +++ b/back/src/data_global_elements.py @@ -34,6 +34,7 @@ def save_all(): if system_counter <= 10: return system_counter = 0 + print(time.ctime()) for elem in interfaces.keys(): if system_stop == True: return @@ -46,7 +47,6 @@ def save_all_before_stop(): interfaces[elem].check_save() def check_save(): - print(time.ctime()) save_all() if system_stop == True: return diff --git a/back/src/data_interface.py b/back/src/data_interface.py index c82d216..cf2039d 100644 --- a/back/src/data_interface.py +++ b/back/src/data_interface.py @@ -132,11 +132,14 @@ class DataInterface(): return self.filter_object_values(tmp_list, filter); def get(self, _id): + if type(_id) != int: + debug.warning("get wrong input type...") debug.info("get " + self.name + ": " + str(_id)) for elem in self.bdd: if 'id' in elem.keys() \ and elem["id"] == _id: return elem + debug.warning("not found element: " + str(len(self.bdd))) return None def delete(self, _id): diff --git a/back/tools/sendLocalData.py b/back/tools/sendLocalData.py index c4fe932..8eb9768 100755 --- a/back/tools/sendLocalData.py +++ b/back/tools/sendLocalData.py @@ -10,6 +10,7 @@ import os import copy import sys +import datetime import hashlib import requests # pip install requests import realog.debug as debug @@ -23,6 +24,7 @@ class upload_in_chunks(object): self.filename = filename self.chunksize = chunksize self.totalsize = os.path.getsize(filename) + self.start_time = datetime.datetime.utcnow() self.readsofar = 0 def __iter__(self): @@ -34,7 +36,8 @@ class upload_in_chunks(object): break self.readsofar += len(data) percent = self.readsofar * 1e2 / self.totalsize - sys.stderr.write("\rSendfing data: {percent:3.0f}% {size:14.0f} / {total_size}".format(percent=percent, size=self.readsofar, total_size=self.totalsize)) + since_time = datetime.datetime.utcnow() - self.start_time + sys.stderr.write("\rSending data: {percent:3.0f}% {size:14.0f} / {total_size} {time}".format(percent=percent, size=self.readsofar, total_size=self.totalsize), time=since_time) yield data def __len__(self): @@ -410,7 +413,7 @@ def push_video_file(_path, _basic_key={}): debug.info("pared meta data: " + json.dumps(_basic_key, sort_keys=True, indent=4)) data_model = { "type_id": _basic_key["type"], - "sha512": result_send_data_json["sha512"], + "data_id": result_send_data_json["id"], #"group_id": int, "name": _basic_key["title"], # number of second diff --git a/front/src/app/element-video/element-video.component.ts b/front/src/app/element-video/element-video.component.ts index a447951..1c50bf7 100644 --- a/front/src/app/element-video/element-video.component.ts +++ b/front/src/app/element-video/element-video.component.ts @@ -57,7 +57,7 @@ export class ElementVideoComponent implements OnInit { self.time = response.time; self.generated_name = response.generated_name; if (self.data_id != -1) { - self.video_source = "http://localhost:15080/data/" + self.data_id + ".mp4"; + self.video_source = "http://localhost:15080/data/" + self.data_id; self.video_enable = true; } else { self.video_source = ""; diff --git a/front/src/app/video-detail/video-detail.component.ts b/front/src/app/video-detail/video-detail.component.ts index 8a59107..54eb410 100644 --- a/front/src/app/video-detail/video-detail.component.ts +++ b/front/src/app/video-detail/video-detail.component.ts @@ -58,7 +58,7 @@ export class VideoDetailComponent implements OnInit { self.time = response.time; self.generated_name = response.generated_name; if (self.data_id != -1) { - self.video_source = "http://localhost:15080/data/" + self.data_id + ".mp4"; + self.video_source = "http://localhost:15080/data/" + self.data_id; } else { self.video_source = ""; }