WebFM/src/core/routes/Routes.py

189 lines
6.0 KiB
Python
Raw Normal View History

2020-10-11 21:30:11 +00:00
# Python imports
2023-02-10 02:40:58 +00:00
import os
2023-02-25 21:37:15 +00:00
import requests
import uuid
2020-10-11 21:30:11 +00:00
# Lib imports
2023-02-25 21:37:15 +00:00
from flask import make_response
2023-02-10 02:40:58 +00:00
from flask import redirect
from flask import request
from flask import render_template
from flask import session
2023-02-10 02:40:58 +00:00
from flask import send_from_directory
2021-02-06 04:52:46 +00:00
2020-10-11 21:30:11 +00:00
# App imports
# Get from __init__
2023-02-10 02:40:58 +00:00
from core import app
from core import db
from core import Favorites
from core import oidc
2020-10-11 21:30:11 +00:00
2021-02-06 07:38:15 +00:00
2020-10-11 21:30:11 +00:00
@app.route('/', methods=['GET', 'POST'])
def home():
2021-02-06 04:52:46 +00:00
if request.method == 'GET':
2023-02-10 02:40:58 +00:00
view = get_view()
2023-02-25 21:37:15 +00:00
sse_id = get_sse_id()
2021-02-07 22:52:08 +00:00
_dot_dots = view.get_dot_dots()
_current_directory = view.get_current_directory()
2023-02-25 21:37:15 +00:00
response = make_response(
render_template(
'pages/index.html',
current_directory = _current_directory,
dot_dots = _dot_dots
)
)
response.set_cookie('sse_id', sse_id, secure=True, httponly = False)
return response
2020-10-11 21:30:11 +00:00
2021-02-07 22:52:08 +00:00
return render_template('error.html', title = 'Error!',
message = 'Must use GET request type...')
2020-10-11 21:30:11 +00:00
2021-02-06 07:38:15 +00:00
@app.route('/api/list-files/<_hash>', methods=['GET', 'POST'])
def list_files(_hash = None):
2020-10-11 21:30:11 +00:00
if request.method == 'POST':
2023-02-10 02:40:58 +00:00
view = get_view()
2021-02-08 02:07:13 +00:00
dot_dots = view.get_dot_dots()
2021-02-08 00:44:35 +00:00
2021-02-08 02:07:13 +00:00
if dot_dots[0][1] == _hash: # Refresh
2021-02-08 00:44:35 +00:00
view.load_directory()
2021-02-08 02:07:13 +00:00
elif dot_dots[1][1] == _hash: # Pop from dir
2021-02-08 00:44:35 +00:00
view.pop_from_path()
msg = "Log in with an Admin privlidged user to view the requested path!"
2021-02-08 02:07:13 +00:00
is_locked = view.is_folder_locked(_hash)
2021-02-08 00:44:35 +00:00
if is_locked and not oidc.user_loggedin:
2023-02-10 02:40:58 +00:00
return json_message.create("danger", msg)
2021-02-08 00:44:35 +00:00
elif is_locked and oidc.user_loggedin:
isAdmin = oidc.user_getfield("isAdmin")
if isAdmin != "yes" :
2023-02-10 02:40:58 +00:00
return json_message.create("danger", msg)
2020-10-11 21:30:11 +00:00
2021-02-08 02:07:13 +00:00
if dot_dots[0][1] != _hash and dot_dots[1][1] != _hash:
path = view.get_path_part_from_hash(_hash)
view.push_to_path(path)
2021-02-07 22:52:08 +00:00
2021-07-27 22:46:56 +00:00
error_msg = view.get_error_message()
2023-02-10 02:40:58 +00:00
if error_msg:
2021-07-27 22:46:56 +00:00
view.unset_error_message()
2023-02-10 02:40:58 +00:00
return json_message.create("danger", error_msg)
2023-02-10 02:40:58 +00:00
sub_path = view.get_current_sub_path()
2021-02-10 04:15:32 +00:00
files = view.get_files_formatted()
fave = db.session.query(Favorites).filter_by(link = sub_path).first()
in_fave = "true" if fave else "false"
2021-02-08 02:07:13 +00:00
files.update({'in_fave': in_fave})
return files
msg = "Can't manage the request type..."
return json_message.create("danger", msg)
2023-02-10 02:40:58 +00:00
2020-10-11 21:30:11 +00:00
@app.route('/api/file-manager-action/<_type>/<_hash>', methods=['GET', 'POST'])
def file_manager_action(_type, _hash = None):
2023-02-10 02:40:58 +00:00
view = get_view()
2021-02-07 22:52:08 +00:00
if _type == "reset-path" and _hash == "None":
2021-02-07 22:52:08 +00:00
view.set_to_home()
msg = "Returning to home directory..."
2023-02-10 02:40:58 +00:00
return json_message.create("success", msg)
2021-02-07 22:52:08 +00:00
folder = view.get_current_directory()
file = view.get_path_part_from_hash(_hash)
2021-02-07 22:52:08 +00:00
fpath = os.path.join(folder, file)
logger.debug(fpath)
2021-02-07 22:52:08 +00:00
if _type == "files":
logger.debug(f"Downloading:\n\tDirectory: {folder}\n\tFile: {file}")
return send_from_directory(directory=folder, filename=file)
2021-02-07 22:52:08 +00:00
if _type == "remux":
2023-02-25 21:37:15 +00:00
remux_video(get_sse_id(), _hash, fpath, view)
msg = "Remuxing: Remux process has started..."
return json_message.create("success", msg)
if _type == "stream":
2023-02-25 21:37:15 +00:00
setup_stream(get_sse_id(), _hash, fpath)
msg = "Streaming: Streaming process is being setup..."
return json_message.create("success", msg)
2021-02-06 04:52:46 +00:00
# NOTE: Positionally protecting actions further down that are privlidged
# Be aware of ordering!
msg = "Log in with an Admin privlidged user to do this action!"
if not oidc.user_loggedin:
2023-02-10 02:40:58 +00:00
return json_message.create("danger", msg)
elif oidc.user_loggedin:
isAdmin = oidc.user_getfield("isAdmin")
if isAdmin != "yes" :
2023-02-10 02:40:58 +00:00
return json_message.create("danger", msg)
if _type == "run-locally":
msg = "Opened media..."
view.open_file_locally(fpath)
2023-02-10 02:40:58 +00:00
return json_message.create("success", msg)
2023-02-25 21:37:15 +00:00
@daemon_threaded
def remux_video(sse_id, hash, path, view):
link = f"https://www.webfm.com/sse/{sse_id}"
body = '{"path":"static/remuxs/' + hash + '.mp4"}'
# good_result = view.remux_video(hash, path)
good_result = view.handbrake_remux_video(hash, path)
2023-02-25 21:37:15 +00:00
if not good_result:
body = json_message.create("warning", "Remuxing: Remux failed...")
requests.post(link, data=body, timeout=10)
def setup_stream(sse_id, hash, path):
link = f"https://www.webfm.com/sse/{sse_id}"
_sub_uuid = uuid.uuid4().hex
_video_path = path
_stub = f"{hash}{_sub_uuid}"
_rtsp_path = f"rtsp://127.0.0.1:8554/{_stub}"
2023-02-25 21:37:15 +00:00
_webrtc_path = f"http://www.{app_name.lower()}.com:8889/{_stub}/"
_stream_target = _rtsp_path
process = get_stream()
if process:
if not kill_stream(process):
msg = "Couldn't stop an existing stream!"
body = json_message.create("danger", msg)
requests.post(link, data=body, timeout=10)
return
stream = get_stream(_video_path, _stream_target)
if stream.poll():
msg = "Streaming: Setting up stream failed! Please try again..."
body = json_message.create("danger", msg)
requests.post(link, data=body, timeout=10)
return
_stream_target = _webrtc_path
body = '{"stream":"' + _stream_target + '"}'
2023-02-25 21:37:15 +00:00
requests.post(link, data=body, timeout=10)
@app.route('/api/stop-current-stream', methods=['GET', 'POST'])
def stop_current_stream():
type = "success"
msg = "Stopped found stream process..."
process = get_stream()
if process:
if not kill_stream(process):
type = "danger"
msg = "Couldn't stop an existing stream!"
else:
type = "warning"
msg = "No stream process found. Nothing to stop..."
return json_message.create(type, msg)