sattar / app.py
raomyousaf's picture
Update app.py
9c36702 verified
Raw
History Blame Contribute Delete
1.57 kB
import os
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
app = Flask(__name__)
# --- Folder setup ---
UPLOAD_FOLDER = "uploads"
TEMPLATE_FOLDER = "templates"
# Create folders if they don’t exist
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(TEMPLATE_FOLDER, exist_ok=True)
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
# In-memory record of uploaded files
file_records = []
# --- Upload Page ---
@app.route("/", methods=["GET", "POST"])
def upload_page():
if request.method == "POST":
custom_name = request.form.get("custom_name")
file = request.files.get("file")
if not custom_name or not file:
return render_template("upload.html", msg="❌ Please enter a name and choose a file.")
# Get extension
ext = os.path.splitext(file.filename)[1]
save_path = os.path.join(app.config["UPLOAD_FOLDER"], f"{custom_name}{ext}")
# Save file
file.save(save_path)
# Save record
file_records.append({"name": custom_name, "path": f"/uploads/{custom_name}{ext}"})
return redirect(url_for("view_page"))
return render_template("upload.html", msg="")
# --- Viewer Page ---
@app.route("/view")
def view_page():
return render_template("view.html", files=file_records)
# --- Serve Uploaded Files ---
@app.route("/uploads/<path:filename>")
def uploaded_file(filename):
return send_from_directory(app.config["UPLOAD_FOLDER"], filename)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)