Skip to content

Internals Bootstrap Controllers StorageController

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

bootstrap\controllers\StorageController

Framework Internals · Subsystem overview · Developer guide

Source: bootstrap/controllers/StorageController.php · Snapshot 8411f4287706.

Controllers extend View and receive package context, paths and the dispatcher from their runtime or object factory. Route execution selects an action method.

Imports and dependencies

use controller\Controller;
use response\Response;
use vfs\resource\PublicResource;
use vfs\storage\StorageAccess;
use vfs\storage\StorageAccessDenied;
use vfs\storage\StorageRepository;

StorageController

Declaration and architectural role

class StorageController extends Controller

Constants, enum cases, traits and properties

Declarations below preserve visibility, type, default and modifiers exactly. A typed property without an initializer remains uninitialized until its constructor or another method assigns it. Mutable fields belong to this instance unless marked static; readonly fields can be initialized once. Constructor-promoted properties are also listed below.

Member Declaration Purpose or source contract
$controllerName public string $controllerName = 'DefaultStorageController'; Declared extension/state field; no own method directly consumes it in this source file.

Method index

Method Signature
index public function index(array|string $params): Response
Resource public function Resource(array|string $params): Response
fileResponse private function fileResponse(string $path): Response

index

Source line 16

public function index(array|string $params): Response

Parameters

Parameter Type, modifiers and default Purpose
$params array|string $params Operation input; the exact use, validation and propagation are shown in the implementation trace.

Return expressions (including local callbacks): Response::make(404); Response::make(403); $this->fileResponse($path).

Failure handling catches \OutOfBoundsException, StorageAccessDenied.

Implementation, validation guards and interaction order
public function index(array|string $params): Response
    {
        if (!is_array($params) || !isset($params['id']) || !ctype_digit((string) $params['id'])) return Response::make(404);
        try {
            // Always hydrate current metadata and authorize before serving, including database cache hits.
            $entry = (new StorageRepository())->fetchById((int) $params['id']);
            StorageAccess::authorize($entry);
        } catch (\OutOfBoundsException $e) {
            return Response::make(404);
        } catch (StorageAccessDenied $e) {
            return Response::make(403);
        }
        $path = $entry->getPath();
        if (!is_file($path) || !is_readable($path)) return Response::make(404);
        return $this->fileResponse($path);
    }

Resource

Source line 33

public function Resource(array|string $params): Response

Parameters

Parameter Type, modifiers and default Purpose
$params array|string $params Operation input; the exact use, validation and propagation are shown in the implementation trace.

Return expressions (including local callbacks): Response::make(404); $this->fileResponse($path).

Implementation, validation guards and interaction order
public function Resource(array|string $params): Response
    {
        if (!is_array($params)) return Response::make(404);
        $package = $params['package'] ?? 'System';
        if (!is_string($package) || !preg_match('/^[a-zA-Z0-9_.-]+$/D', $package) || $package === '.' || $package === '..') return Response::make(404);
        $segments = [];
        for ($i = 0; isset($params['resource' . $i]); $i++) $segments[] = rawurldecode($params['resource' . $i]);
        $segments[] = rawurldecode($params['file'] ?? '');
        $relative = implode('/', $segments);
        // Resolve the exact URL path; filename-only recursive lookup is reserved for internal callers.
        $directory = rtrim(PACKAGES, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $package;
        $path = $directory . DIRECTORY_SEPARATOR . $relative;
        foreach (explode('/', $relative) as $segment) {
            if ($segment === '' || $segment === '.' || $segment === '..' || str_contains($segment, '\\') || str_contains($segment, "\0")) return Response::make(404);
        }
        if (!PublicResource::allows($package, $path)) return Response::make(404);
        return $this->fileResponse($path);
    }

fileResponse

Source line 52

private function fileResponse(string $path): Response

Parameters

Parameter Type, modifiers and default Purpose
$path string $path Filesystem or route path as defined by the owning component; no universal path normalization is implied.

Return expressions (including local callbacks): $response.

External effects include header.

Implementation, validation guards and interaction order
private function fileResponse(string $path): Response
    {
        $response = Response::make(200)->file($path)->header('X-Content-Type-Options', 'nosniff');
        // Unknown extensions are binary files, regardless of the request Accept header.
        if ((new \response\resolvers\MimeResolver())->fromPath($path) === null) $response->asFile();
        return $response;
    }

Related types

Clone this wiki locally