add list of uploads, /run/ entrypoint

This commit is contained in:
bretello 2024-06-14 19:52:38 +02:00
parent d3709bd779
commit 3c651d65aa
Signed by: brethil
GPG Key ID: 876AAC6290170FE7
2 changed files with 55 additions and 1 deletions

34
app.py
View File

@ -1,8 +1,11 @@
import os
import glob
from subprocess import check_call, CalledProcessError
from flask import Flask
from flask import request, render_template
from flask import request, render_template, make_response
from threading import Thread
app = Flask(
__name__,
@ -28,11 +31,28 @@ def convert_to_wav(file_name: str):
raise
def run_cassetta(cassetta: str):
command = f"aplay uploads/{cassetta}"
try:
check_call(command.split())
except CalledProcessError as exc:
print(f"aplay failed: {exc.output=}\n{exc.stderr=}")
def uploads():
return map(
os.path.basename,
glob.glob("uploads/*wav"),
)
@app.route("/")
def upload_form():
return render_template(
"upload.html",
title="Upload your shit here",
files=uploads(),
)
@ -65,5 +85,17 @@ def upload_file():
return "File uploaded successfully"
@app.route("/run/<string:cassetta>", methods=["GET"])
def run(cassetta: str):
thread = Thread(target=run_cassetta, args=(cassetta,))
thread.start()
response = make_response(make_response("", 301))
response.headers["Location"] = "/"
response.status_code = 301
return response
if __name__ == "__main__":
app.run()

View File

@ -15,6 +15,28 @@
<button type="submit">Upload</button>
</form>
</div>
<div>
<p> Cassette:</p>
<table border="1">
<thead>
<tr>
<th>Nome</th>
<!-- {% for header in headers %} -->
<!-- <th>{{ header }}</th> -->
<!-- {% endfor %} -->
</tr>
</thead>
<tbody>
{% for file in files %}
<tr>
<td>{{ file }}</td>
<td><a href="/run/{{ file }}">run</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
</p>
</html>