feat: dispatch FileAccessed and FileNotFound events from warehouse

Lets host apps hook project-specific side-effects (access logs, analytics,
tracking pixels) without subclassing WarehouseController.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-04-23 19:36:14 +02:00
parent 361e48762a
commit 9831ed6c53
3 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,22 @@
<?php
namespace Blax\Files\Events;
use Blax\Files\Models\File;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Http\Request;
/**
* Fired by the warehouse controller after a file is resolved and about to be
* served. Listen for side-effects that need the resolved File model
* access logs, analytics, tracking pixels, etc.
*/
class FileAccessed
{
use Dispatchable;
public function __construct(
public readonly File $file,
public readonly Request $request,
) {}
}

View File

@ -0,0 +1,21 @@
<?php
namespace Blax\Files\Events;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Http\Request;
/**
* Fired by the warehouse controller when the requested identifier does not
* resolve to a File (persisted or asset-backed). Fires immediately before the
* 404 abort, so listeners can record the miss for analytics / tamper alerting.
*/
class FileNotFound
{
use Dispatchable;
public function __construct(
public readonly ?string $identifier,
public readonly Request $request,
) {}
}

View File

@ -2,6 +2,8 @@
namespace Blax\Files\Http\Controllers;
use Blax\Files\Events\FileAccessed;
use Blax\Files\Events\FileNotFound;
use Blax\Files\Models\File;
use Blax\Files\Services\WarehouseService;
use Illuminate\Http\Request;
@ -16,6 +18,7 @@ class WarehouseController extends Controller
$file = WarehouseService::searchFile($request, $identifier);
if (! $file) {
FileNotFound::dispatch($identifier, $request);
abort(404);
}
@ -24,6 +27,8 @@ class WarehouseController extends Controller
$this->checkAccess($request, $file);
}
FileAccessed::dispatch($file, $request);
return $file->respond($request);
}