First Commit
This commit is contained in:
1 parent
a9a0a0bf8c
commit
676a52199c
8 files changed
+177
-2
No files matched your search
@@ -0,0 +1,58 @@
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from flask import Flask, abort, render_template, send_file
|
||||
|
||||
load_dotenv()
|
||||
|
||||
BASE_DIR = Path(os.environ.get("MP4_VIEWER_DIR", ".")).resolve()
|
||||
FLASK_HOST = os.environ.get("FLASK_HOST", "127.0.0.1")
|
||||
FLASK_PORT = int(os.environ.get("FLASK_PORT", "5000"))
|
||||
|
||||
if not BASE_DIR.exists() or not BASE_DIR.is_dir():
|
||||
print(f"Error: MP4_VIEWER_DIR does not exist or is not a directory: {BASE_DIR}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def _validate_path(rel_path):
|
||||
full = BASE_DIR / rel_path
|
||||
try:
|
||||
full = full.resolve(strict=False)
|
||||
except (ValueError, OSError):
|
||||
abort(400)
|
||||
if not full.is_relative_to(BASE_DIR):
|
||||
abort(403)
|
||||
if not full.exists() or not full.is_file():
|
||||
abort(404)
|
||||
return full
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
files = sorted(
|
||||
f.name
|
||||
for f in BASE_DIR.iterdir()
|
||||
if f.is_file() and f.suffix.lower() == ".mp4"
|
||||
)
|
||||
return render_template("index.html", files=files, dirname=str(BASE_DIR))
|
||||
|
||||
|
||||
@app.route("/play/<path:path>")
|
||||
def play(path):
|
||||
_validate_path(path)
|
||||
return render_template("play.html", filename=Path(path).name, path=path)
|
||||
|
||||
|
||||
@app.route("/video/<path:path>")
|
||||
def serve_video(path):
|
||||
full = _validate_path(path)
|
||||
return send_file(full, mimetype="video/mp4")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host=FLASK_HOST, port=FLASK_PORT, debug=True)
|
||||
Reference in new issue
Block a user