voicemail/app.py

54 lines
1.6 KiB
Python
Raw Permalink Normal View History

2024-12-27 16:56:43 +00:00
import os
from flask import Flask, flash, request, redirect, url_for, render_template, send_from_directory
from werkzeug.utils import secure_filename
import secrets
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'flac', 'mp3', 'm4a', 'ogg', 'wav'}
app = Flask(__name__)
app.secret_key = secrets.token_urlsafe(16)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/")
def index():
upload = request.args.get('upload')
return render_template("index.jinja", status=upload)
@app.route('/', methods=['POST'])
def upload_file():
if request.method == 'POST':
if 'the_voicemail' not in request.files:
flash('No file uploaded', 'form')
return redirect(request.url)
file = request.files['the_voicemail']
if file.filename == '':
flash('No selected file', 'form')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(path)
return redirect("?upload=completed")
else:
print(request.method)
return "not a valid thing"
@app.route('/recent')
def get_most_recent():
files = os.listdir(app.config["UPLOAD_FOLDER"])
paths = [os.path.join(app.config["UPLOAD_FOLDER"], basename) for basename in files]
if paths:
return send_from_directory(".", max(paths, key=os.path.getctime))
else:
return 'file not found', 404