From 10502157a334975bb1049641464c684be1b10259 Mon Sep 17 00:00:00 2001 From: markmental Date: Mon, 2 Mar 2026 16:58:47 -0500 Subject: [PATCH] Big performance improvement! Added Caddyfile, using Caddy's file server to serve large files instead of PHP --- Caddyfile | 38 ++++++++++++++++++++++++++++++++++++++ assets/js/app.js | 13 ++++++++++++- authz_media.php | 8 ++++++++ start-media-server.sh | 17 +++++++++++++++-- 4 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 Caddyfile create mode 100644 authz_media.php diff --git a/Caddyfile b/Caddyfile new file mode 100644 index 0000000..6a48e17 --- /dev/null +++ b/Caddyfile @@ -0,0 +1,38 @@ +{ + frankenphp { + num_threads {$FRANKENPHP_NUM_THREADS:8} + max_threads {$FRANKENPHP_MAX_THREADS:16} + } + + servers { + trusted_proxies static private_ranges + } +} + +{$FREAX_SITE_ADDR::9000} { + root * {$FREAX_DOCROOT:/home/user/freax-media} + + encode zstd gzip + + @static path /assets/* /freaxlogo.png + handle @static { + file_server + } + + handle_path /media/* { + @allowed_media path_regexp allowed_media (?i).*\.(mp4|mkv|avi|mov|wmv|flv|webm|m4v|mp3|wav|ogg|flac|m4a|aac|wma|opus)$ + handle @allowed_media { + root * {$FREAX_MEDIA_ROOT:/__FREAX_MEDIA_ROOT_NOT_SET__} + forward_auth {$FREAX_AUTH_UPSTREAM:127.0.0.1:9000} { + uri /authz_media.php + } + file_server + } + + respond "Forbidden" 403 + } + + php_server + file_server +} + diff --git a/assets/js/app.js b/assets/js/app.js index 453722f..0cf6262 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -475,6 +475,17 @@ function navigateToPlayback(path) { window.location.href = `index.php?${params.toString()}`; } +function buildMediaStreamUrl(path) { + const normalized = String(path || '').replace(/\\/g, '/'); + const encodedPath = normalized + .split('/') + .filter(Boolean) + .map(part => encodeURIComponent(part)) + .join('/'); + + return `/media/${encodedPath}`; +} + function playFile(path, name) { const container = $('playerContainer'); const ext = path.toLowerCase().split('.').pop(); @@ -483,7 +494,7 @@ function playFile(path, name) { const media = document.createElement(isVideo ? 'video' : 'audio'); media.controls = true; media.autoplay = true; - media.src = `serve_media.php?file=${encodeURIComponent(path)}`; + media.src = buildMediaStreamUrl(path); media.addEventListener('error', () => { container.innerHTML = ` diff --git a/authz_media.php b/authz_media.php new file mode 100644 index 0000000..b30157a --- /dev/null +++ b/authz_media.php @@ -0,0 +1,8 @@ +