A free open source IT asset/license management system
feat: add PUBLIC_S3_PROXY option to serve public uploads through the app
Guillaume Delbergue1482abc8b934
diff --git a/.env.example b/.env.exampleindex dbdfcc3d72..8bbc34e31d 100644--- a/.env.example+++ b/.env.example@@ -149,6 +149,14 @@ REDIS_PORT=nullMEMCACHED_HOST=nullMEMCACHED_PORT=null+# --------------------------------------------+# OPTIONAL: S3 PROXY+# When enabled, public uploads (images, logos, avatars) are served through+# the application instead of directly from S3. This allows using a single+# fully private S3 bucket for all storage.+# --------------------------------------------+PUBLIC_S3_PROXY=false+# --------------------------------------------# OPTIONAL: PUBLIC S3 Settings# --------------------------------------------diff --git a/app/Http/Controllers/StorageProxyController.php b/app/Http/Controllers/StorageProxyController.phpnew file mode 100644index 0000000000..efed5e5c51--- /dev/null+++ b/app/Http/Controllers/StorageProxyController.php@@ -0,0 +1,74 @@+<?php++namespace App\Http\Controllers;++use Illuminate\Support\Facades\Storage;+use Symfony\Component\HttpFoundation\Response;+use Symfony\Component\HttpFoundation\StreamedResponse;++class StorageProxyController extends Controller+{+ /**+ * Proxy files from the "public" storage disk through the application.+ *+ * When PUBLIC_S3_PROXY is enabled, this serves files that would normally+ * be accessed directly from S3 (images, logos, avatars, etc.), allowing+ * a fully private S3 bucket setup.+ */+ public function show(string $path): Response|StreamedResponse+ {+ $disk = Storage::disk('public');++ // The S3 adapter includes the disk's root prefix in generated URLs,+ // but Flysystem also prepends it internally on every operation.+ // Strip it here to avoid double-prefixing.+ $root = trim(config('filesystems.disks.public.root', ''), '/');+ if ($root !== '' && str_starts_with($path, $root . '/')) {+ $path = substr($path, strlen($root) + 1);+ }++ if (! $disk->exists($path)) {+ abort(404);+ }++ $mimeType = $disk->mimeType($path) ?: 'application/octet-stream';+ $lastModified = $disk->lastModified($path);+ $etag = md5($path . $lastModified);+ $size = $disk->size($path);++ if ($this->isNotModified($etag, $lastModified)) {+ return response('', 304)+ ->header('ETag', '"' . $etag . '"')+ ->header('Cache-Control', 'public, max-age=86400');+ }++ return new StreamedResponse(function () use ($disk, $path) {+ $stream = $disk->readStream($path);+ fpassthru($stream);+ if (is_resource($stream)) {+ fclose($stream);+ }+ }, 200, [+ 'Content-Type' => $mimeType,+ 'Content-Length' => $size,+ 'ETag' => '"' . $etag . '"',+ 'Last-Modified' => gmdate('D, d M Y H:i:s', $lastModified) . ' GMT',+ 'Cache-Control' => 'public, max-age=86400',+ ]);+ }++ private function isNotModified(string $etag, int $lastModified): bool+ {+ $requestEtag = request()->header('If-None-Match');+ if ($requestEtag && $requestEtag === '"' . $etag . '"') {+ return true;+ }++ $ifModifiedSince = request()->header('If-Modified-Since');+ if ($ifModifiedSince && strtotime($ifModifiedSince) >= $lastModified) {+ return true;+ }++ return false;+ }+}diff --git a/config/filesystems.php b/config/filesystems.phpindex 82098258df..e42ae3d758 100644--- a/config/filesystems.php+++ b/config/filesystems.php@@ -109,6 +109,14 @@ $config = [// (by default, the PUBLIC_FILESYSTEM DISK is 'local_public', in the public/uploads directory)$config['disks']['public'] = $config['disks'][env('PUBLIC_FILESYSTEM_DISK', 'local_public')];+// When PUBLIC_S3_PROXY is enabled, all "public" uploads are served through the application+// instead of being accessed directly from S3. This allows using a single private S3 bucket+// for all storage, with the app proxying requests for public files (images, logos, avatars).+if (env('PUBLIC_S3_PROXY', false)) {+ $config['disks']['public']['visibility'] = 'private';+ $config['disks']['public']['url'] = env('APP_URL').'/storage-proxy';+}+// This is used to determine which files to accept, and also to populate the language strings for the upload-file blade$config['allowed_upload_extensions_array'] = ['avif',diff --git a/routes/web.php b/routes/web.phpindex 4488f7a7bf..ae5b83b356 100644--- a/routes/web.php+++ b/routes/web.php@@ -27,6 +27,7 @@ use App\Http\Controllers\ReportsController;use App\Http\Controllers\SettingsController;use App\Http\Controllers\SetupController;use App\Http\Controllers\StatuslabelsController;+use App\Http\Controllers\StorageProxyController;use App\Http\Controllers\SuppliersController;use App\Http\Controllers\ViewAssetsController;use App\Livewire\Importer;@@ -759,6 +760,20 @@ Route::group(['middleware' => 'web'], function () {});+/*+|--------------------------------------------------------------------------+| Storage Proxy Route+|--------------------------------------------------------------------------+|+| When PUBLIC_S3_PROXY=true, public uploads (images, logos, avatars) are+| served through the application instead of directly from S3. This allows+| using a fully private S3 bucket for all storage.+|+*/+Route::get('storage-proxy/{path}', [StorageProxyController::class, 'show'])+ ->where('path', '.*')+ ->name('storage-proxy');+/*** Health check route - skip middleware*/