[DEV] better interface

This commit is contained in:
Edouard DUPIN 2020-01-02 22:43:06 +01:00
parent aff5e71cc5
commit b7719edccf
9 changed files with 44 additions and 23 deletions

View File

@ -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 EXPOSE 80
ADD src /application/ ADD src /application/
WORKDIR /application/ WORKDIR /application/
CMD ["python", "-u", "./app_video.py"] CMD ["python3", "-u", "./app_video.py"]

View File

@ -7,4 +7,6 @@ services:
container_name: video_rest_api container_name: video_rest_api
ports: ports:
- 15080:80 - 15080:80
volumes:
- ./data:/application/data

View File

@ -64,6 +64,16 @@ def add(_app, _name_api):
data_global_elements.get_interface(_name_api).set_data_model(DataModelBdd) data_global_elements.get_interface(_name_api).set_data_model(DataModelBdd)
@elem_blueprint.get('/' + _name_api + '/exist/<sha512:string>', 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) @elem_blueprint.post('/' + _name_api, strict_slashes=True, stream=True)
@doc.summary("send new file data") @doc.summary("send new file data")
@ -85,6 +95,7 @@ def add(_app, _name_api):
file_stream = open(temporary_file,"wb") file_stream = open(temporary_file,"wb")
sha1 = hashlib.sha512() sha1 = hashlib.sha512()
while True: while True:
debug.warning("ploufffff " + str(dir(_request.stream)))
body = await _request.stream.read() body = await _request.stream.read()
if body is None: if body is None:
debug.warning("empty body"); 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)) await _response.write(json.dumps(return_bdd, sort_keys=True, indent=4))
return response.stream(streaming, content_type='application/json') return response.stream(streaming, content_type='application/json')
@elem_blueprint.get('/' + _name_api + '/<id:string>', strict_slashes=True) @elem_blueprint.get('/' + _name_api + '/<id:int>', strict_slashes=True)
@doc.summary("get a specific resource") @doc.summary("get a specific resource")
@doc.description("Get a resource with all the needed datas ... It permeit seek for video stream.") @doc.description("Get a resource with all the needed datas ... It permeit seek for video stream.")
@doc.produces(content_type='application/json') @doc.produces(content_type='application/json')
async def retrive(request, id): async def retrive(request, id):
debug.warning("Request data media 2 : " + id); debug.warning("Request data media 2 : " + str(id));
"""
if id[-4:] == ".mp4": if id[-4:] == ".mp4":
id = id[:-4] id = id[:-4]
if id[-4:] == ".mkv": if id[-4:] == ".mkv":
id = id[:-4] id = id[:-4]
if id[-4:] == ".avi": """
id = id[:-4] filename = os.path.join(_app.config['REST_MEDIA_DATA'], str(id), "video")
if id[-4:] == ".ts":
id = id[:-3]
filename = os.path.join(_app.config['REST_MEDIA_DATA'], id, "video")
value = data_global_elements.get_interface(_name_api).get(id) value = data_global_elements.get_interface(_name_api).get(id)
debug.info("plouuuuuuf " + str(value))
headers = { headers = {
'Content-Type': value["mime_type"], 'Content-Type': value["mime_type"],
'Accept-Ranges': 'Accept-Ranges: bytes' 'Accept-Ranges': 'Accept-Ranges: bytes'

View File

@ -59,7 +59,7 @@ if "REST_MEDIA_DATA" not in app.config.keys():
if "REST_DATA" not in app.config.keys(): if "REST_DATA" not in app.config.keys():
app.config['REST_DATA'] = "data" app.config['REST_DATA'] = "data"
if "REST_HOST" not in app.config.keys(): 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(): if "REST_PORT" not in app.config.keys():
app.config['REST_PORT'] = "80" app.config['REST_PORT'] = "80"

View File

@ -34,6 +34,7 @@ def save_all():
if system_counter <= 10: if system_counter <= 10:
return return
system_counter = 0 system_counter = 0
print(time.ctime())
for elem in interfaces.keys(): for elem in interfaces.keys():
if system_stop == True: if system_stop == True:
return return
@ -46,7 +47,6 @@ def save_all_before_stop():
interfaces[elem].check_save() interfaces[elem].check_save()
def check_save(): def check_save():
print(time.ctime())
save_all() save_all()
if system_stop == True: if system_stop == True:
return return

View File

@ -132,11 +132,14 @@ class DataInterface():
return self.filter_object_values(tmp_list, filter); return self.filter_object_values(tmp_list, filter);
def get(self, _id): def get(self, _id):
if type(_id) != int:
debug.warning("get wrong input type...")
debug.info("get " + self.name + ": " + str(_id)) debug.info("get " + self.name + ": " + str(_id))
for elem in self.bdd: for elem in self.bdd:
if 'id' in elem.keys() \ if 'id' in elem.keys() \
and elem["id"] == _id: and elem["id"] == _id:
return elem return elem
debug.warning("not found element: " + str(len(self.bdd)))
return None return None
def delete(self, _id): def delete(self, _id):

View File

@ -10,6 +10,7 @@
import os import os
import copy import copy
import sys import sys
import datetime
import hashlib import hashlib
import requests # pip install requests import requests # pip install requests
import realog.debug as debug import realog.debug as debug
@ -23,6 +24,7 @@ class upload_in_chunks(object):
self.filename = filename self.filename = filename
self.chunksize = chunksize self.chunksize = chunksize
self.totalsize = os.path.getsize(filename) self.totalsize = os.path.getsize(filename)
self.start_time = datetime.datetime.utcnow()
self.readsofar = 0 self.readsofar = 0
def __iter__(self): def __iter__(self):
@ -34,7 +36,8 @@ class upload_in_chunks(object):
break break
self.readsofar += len(data) self.readsofar += len(data)
percent = self.readsofar * 1e2 / self.totalsize 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 yield data
def __len__(self): 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)) debug.info("pared meta data: " + json.dumps(_basic_key, sort_keys=True, indent=4))
data_model = { data_model = {
"type_id": _basic_key["type"], "type_id": _basic_key["type"],
"sha512": result_send_data_json["sha512"], "data_id": result_send_data_json["id"],
#"group_id": int, #"group_id": int,
"name": _basic_key["title"], "name": _basic_key["title"],
# number of second # number of second

View File

@ -57,7 +57,7 @@ export class ElementVideoComponent implements OnInit {
self.time = response.time; self.time = response.time;
self.generated_name = response.generated_name; self.generated_name = response.generated_name;
if (self.data_id != -1) { 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; self.video_enable = true;
} else { } else {
self.video_source = ""; self.video_source = "";

View File

@ -58,7 +58,7 @@ export class VideoDetailComponent implements OnInit {
self.time = response.time; self.time = response.time;
self.generated_name = response.generated_name; self.generated_name = response.generated_name;
if (self.data_id != -1) { 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 { } else {
self.video_source = ""; self.video_source = "";
} }