From 3bf4f28dc46c328c68ecdb11433b401901d9a2db Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Sun, 12 Apr 2020 21:12:46 +0200 Subject: [PATCH] abs path, read json playlist --- ffplayout/api/utils.py | 34 ++++++++++++++++++++++++---------- ffplayout/api/views.py | 31 ++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/ffplayout/api/utils.py b/ffplayout/api/utils.py index 54917633..a5bf5502 100644 --- a/ffplayout/api/utils.py +++ b/ffplayout/api/utils.py @@ -1,4 +1,5 @@ import os +import json from platform import uname from time import sleep @@ -18,7 +19,17 @@ def read_yaml(): def write_yaml(data): if os.path.isfile(settings.FFPLAYOUT_CONFIG): with open(settings.FFPLAYOUT_CONFIG, 'w') as outfile: - yaml.dump(data, outfile, default_flow_style=False, sort_keys=False) + yaml.dump(data, outfile, default_flow_style=False, + sort_keys=False, indent=4) + + +def read_json(date): + config = read_yaml()['playlist']['path'] + y, m, d = date.split('-') + input = os.path.join(config, y, m, '{}.json'.format(date)) + if os.path.isfile(input): + with open(input, 'r') as playlist: + return json.load(playlist) def sizeof_fmt(num, suffix='B'): @@ -119,32 +130,35 @@ class SystemStats: def set_root(path): # prevent access to root file system - root = os.path.dirname(settings.MEDIA_FOLDER) - return path.lstrip(root) + dir = os.path.dirname( + read_yaml()['storage']['path'].replace('\\', '/').rstrip('/')) + + return path.replace(dir, '').strip('/') def get_media_path(dir=None): + config = read_yaml() + media_dir = config['storage']['path'].replace('\\', '/').rstrip('/') if not dir: - if not os.path.isdir(settings.MEDIA_FOLDER): + if not os.path.isdir(media_dir): return '' - dir = settings.MEDIA_FOLDER + dir = media_dir else: if '/..' in dir: dir = '/'.join(dir.split('/')[:-2]) - # prevent deeper access - dir = dir.replace('/../', '/') - dir = os.path.join(os.path.dirname(settings.MEDIA_FOLDER), dir) + dir = os.path.join(os.path.dirname(media_dir), + os.path.abspath('/' + dir).strip('/')) for root, dirs, files in os.walk(dir, topdown=True): media_files = [] for file in files: - if os.path.splitext(file)[1] in settings.MEDIA_EXTENSIONS: + if os.path.splitext(file)[1] in config['storage']['extensions']: media_files.append(file) dirs = natsorted(dirs) - if root != settings.MEDIA_FOLDER: + if root != media_dir: dirs.insert(0, '..') if not dirs: diff --git a/ffplayout/api/views.py b/ffplayout/api/views.py index 5f571f67..f13bdfc4 100644 --- a/ffplayout/api/views.py +++ b/ffplayout/api/views.py @@ -1,7 +1,8 @@ from rest_framework.response import Response from rest_framework.views import APIView -from .utils import read_yaml, write_yaml, SystemStats, get_media_path +from .utils import (SystemStats, get_media_path, read_yaml, write_yaml, + read_json) class Config(APIView): @@ -31,6 +32,34 @@ class Config(APIView): return Response({"success": False}) +class Playlist(APIView): + """ + read and write config from ffplayout engine + for reading endpoint: http://127.0.0.1:8000/api/playlist/?date=2020-04-12 + """ + + def get(self, request, *args, **kwargs): + if 'date' in request.GET.dict(): + date = request.GET.dict()['date'] + json_input = read_json(date) + + if json_input: + return Response(json_input) + else: + return Response({ + "success": False, + "error": "Playlist from {} not found!".format(date)}) + else: + return Response({"success": False}) + + def post(self, request, *args, **kwargs): + if 'data' in request.data: + write_yaml(request.data['data']) + return Response({"success": True}) + + return Response({"success": False}) + + class Statistics(APIView): """ get system statistics: cpu, ram, etc.