laas/app.py
2025-02-22 22:37:11 +01:00

58 lines
1.9 KiB
Python

from flask import Flask, render_template, request, send_file
import os
playback = False
if not os.path.exists("music"):
os.mkdir("music")
app = Flask(__name__)
def audio_player():
os.system(f'mpv --input-ipc-server=/tmp/mpvsocket --shuffle music --no-video &')
def sizeof_fmt(num, suffix="B"):
for unit in ("", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"):
if abs(num) < 1024.0:
return f"{num:3.1f}{unit}{suffix}"
num /= 1024.0
return f"{num:.1f}Yi{suffix}"
@app.route('/', methods=["POST", "GET"])
def audiothing():
global playback
if request.method == "GET":
return render_template("player.html", playing=playback)
if request.method == "POST":
if playback:
print("Killing Audio Playback..")
os.system("killall mpv")
playback = False
else:
print("Starting Audio Playback..")
audio_player()
playback = True
return render_template("player.html", playing=playback)
@app.route("/files/<path:path>", methods=["GET"])
@app.route("/files")
def filemgr(path=""):
full_path = os.path.join("music", path)
print(full_path)
if os.path.isfile(full_path):
return send_file(full_path)
files = os.listdir(full_path)
for i in range(len(files)):
if os.path.isfile(os.path.join(full_path, files[i])):
files[i] = [files[i], os.path.getsize(os.path.join(full_path, files[i]))]
else:
totalsize = 0
for file in os.listdir(os.path.join(full_path, files[i])):
totalsize += os.path.getsize(os.path.join(full_path, files[i], file))
files[i] = [files[i], totalsize]
for file in files:
file[1] = sizeof_fmt(file[1])
return render_template("filemanager.html", files=files, path=path)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=1337)