|
|
@ -1,9 +1,23 @@ |
|
|
|
"""Flask server for the vocoder back-end.""" |
|
|
|
|
|
|
|
from flask import Flask, render_template, request |
|
|
|
import os.path |
|
|
|
from pathlib import Path |
|
|
|
|
|
|
|
from flask import Flask, render_template, request, send_from_directory |
|
|
|
from flask_cors import CORS |
|
|
|
from werkzeug.utils import secure_filename |
|
|
|
|
|
|
|
ALLOWED_UPLOAD_EXTENSIONS = {"wav"} |
|
|
|
UPLOAD_DIRECTORY = Path(__file__).parent / "archive" |
|
|
|
MAXIMUM_FILE_SIZE = 16 * 1024 * 1024 # 16 MB |
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
|
|
app.config["UPLOAD_FOLDER"] = UPLOAD_DIRECTORY |
|
|
|
app.config["MAX_CONTENT_LENGTH"] = MAXIMUM_FILE_SIZE |
|
|
|
|
|
|
|
CORS(app) |
|
|
|
|
|
|
|
|
|
|
|
@app.route("/") |
|
|
|
def root(): |
|
|
@ -11,16 +25,38 @@ def root(): |
|
|
|
return render_template("index.html") |
|
|
|
|
|
|
|
|
|
|
|
@app.route("/add-to-archive/", methods=["POST"]) |
|
|
|
def allowed_file(filename): |
|
|
|
"""Validate uploaded file extensions.""" |
|
|
|
extension = filename.rsplit(".", 1)[1].lower() |
|
|
|
return "." in filename and extension in ALLOWED_UPLOAD_EXTENSIONS |
|
|
|
|
|
|
|
|
|
|
|
@app.route("/add-to-archive", methods=["POST"]) |
|
|
|
def add_to_archive(): |
|
|
|
"""Accept audio recordings for archiving.""" |
|
|
|
# TODO: implement the saving of the files currently, |
|
|
|
# I can't seem to get the file to send but the basic |
|
|
|
# plumbing is in place |
|
|
|
return ("faking it till we make it", 201) |
|
|
|
"""Archive recordings sent from the front-end.""" |
|
|
|
try: |
|
|
|
file = request.files["file"] |
|
|
|
except KeyError: |
|
|
|
return ("Missing file for upload", 400) |
|
|
|
|
|
|
|
if not allowed_file(file.filename): |
|
|
|
return ("File extension not allowed", 400) |
|
|
|
|
|
|
|
secured = secure_filename(file.filename) |
|
|
|
file.save(os.path.join(app.config["UPLOAD_FOLDER"], secured)) |
|
|
|
|
|
|
|
return ("file archived successfully", 201) |
|
|
|
|
|
|
|
|
|
|
|
@app.route("/archives/<filename>") |
|
|
|
def uploaded_file(filename): |
|
|
|
"""Retrieve an uploaded recording.""" |
|
|
|
return send_from_directory(app.config["UPLOAD_FOLDER"], filename) |
|
|
|
|
|
|
|
|
|
|
|
@app.route("/archive/") |
|
|
|
@app.route("/archive") |
|
|
|
def archive(): |
|
|
|
"""Serve the archive listing.""" |
|
|
|
return render_template("archive.html") |
|
|
|
listing = os.listdir(UPLOAD_DIRECTORY) |
|
|
|
files = filter(lambda f: f.endswith(".wav"), listing) |
|
|
|
return render_template("archive.html", files=files) |
|
|
|