mannaggianastri/app.py

70 lines
1.6 KiB
Python
Raw Normal View History

2024-06-14 17:39:50 +02:00
import os
2024-06-14 19:29:12 +02:00
from subprocess import check_call, CalledProcessError
2024-06-14 17:39:50 +02:00
from flask import Flask
from flask import request, render_template
app = Flask(
__name__,
# static_url_path="./",
static_folder="static",
template_folder="templates",
)
os.makedirs("uploads", exist_ok=True)
2024-06-14 19:29:12 +02:00
def convert_to_wav(file_name: str):
assert file_name.endswith(".wav")
output, extension = file_name.rsplit(".", maxsplit=1)
assert extension != "wav"
print(f"{extension=}")
command = f"tape2wav {file_name} ${output}.wav"
try:
check_call(command.split())
except CalledProcessError as exc:
print(f"tape2wav failed: {exc.output=}\n{exc.stderr=}")
raise
2024-06-14 17:39:50 +02:00
@app.route("/")
def upload_form():
return render_template(
"upload.html",
title="Upload your shit here",
)
@app.route("/upload", methods=["POST"])
def upload_file():
if "file" not in request.files:
return "No file part"
file = request.files["file"]
if file.filename == "":
return "No selected file"
if not file:
return "No file?"
save_path = os.path.join("uploads", file.filename)
assert not os.path.exists(save_path)
try:
file.save(save_path)
except Exception as exc:
return f"fail: {exc=}"
2024-06-14 19:29:12 +02:00
name, extension = file.filename.rsplit(".", maxsplit=1)
if extension != "wav":
thread = Thread(target=convert_to_wav, args=(file.filename,))
thread.start()
print(f"thread started for {file.filename=}")
2024-06-14 17:39:50 +02:00
return "File uploaded successfully"
if __name__ == "__main__":
app.run()