Skip to content

Commit

Permalink
Forward to error page not found assets
Browse files Browse the repository at this point in the history
  • Loading branch information
giuscris committed Nov 16, 2024
1 parent f5ce118 commit cc34768
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
14 changes: 7 additions & 7 deletions formwork/src/Controllers/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@
namespace Formwork\Controllers;

use Formwork\Http\FileResponse;
use Formwork\Http\Response;
use Formwork\Router\RouteParams;
use Formwork\Utils\Exceptions\FileNotFoundException;
use Formwork\Utils\FileSystem;

class AssetsController extends AbstractController
{
public function asset(RouteParams $routeParams): FileResponse
public function asset(RouteParams $routeParams): Response
{
$path = FileSystem::joinPaths($this->config->get('system.images.processPath'), $routeParams->get('id'), $routeParams->get('name'));

if (FileSystem::isFile($path)) {
if (FileSystem::isFile($path, assertExists: false)) {
return new FileResponse($path, headers: ['Cache-Control' => 'private, max-age=31536000, immutable'], autoEtag: true, autoLastModified: true);
}

throw new FileNotFoundException('Cannot find asset');
return $this->forward(PageController::class, 'error');
}

public function template(RouteParams $routeParams): FileResponse
public function template(RouteParams $routeParams): Response
{
$path = FileSystem::joinPaths($this->config->get('system.templates.path'), 'assets', $routeParams->get('file'));

if (FileSystem::isFile($path)) {
if (FileSystem::isFile($path, assertExists: false)) {
$headers = $this->request->query()->has('v')
? ['Cache-Control' => 'private, max-age=31536000, immutable']
: [];
return new FileResponse($path, headers: $headers, autoEtag: true, autoLastModified: true);
}

throw new FileNotFoundException('Cannot find asset');
return $this->forward(PageController::class, 'error');
}
}
5 changes: 5 additions & 0 deletions formwork/src/Controllers/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public function load(RouteParams $routeParams, Statistics $statistics): Response
return $this->getPageResponse($this->site->errorPage());
}

public function error(): Response
{
return $this->getPageResponse($this->site->errorPage());
}

protected function getPageResponse(Page $page): Response
{
if ($this->site->currentPage() === null) {
Expand Down
8 changes: 4 additions & 4 deletions formwork/src/Panel/Controllers/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
namespace Formwork\Panel\Controllers;

use Formwork\Http\FileResponse;
use Formwork\Http\Response;
use Formwork\Router\RouteParams;
use Formwork\Utils\Exceptions\FileNotFoundException;
use Formwork\Utils\FileSystem;

class AssetsController extends AbstractController
{
public function asset(RouteParams $routeParams): FileResponse
public function asset(RouteParams $routeParams): Response
{
$path = FileSystem::joinPaths($this->config->get('system.panel.paths.assets'), $routeParams->get('type'), $routeParams->get('file'));

if (FileSystem::isFile($path)) {
if (FileSystem::isFile($path, assertExists: false)) {
$headers = ($this->request->query()->has('v') || $routeParams->get('type') === 'icons')
? ['Cache-Control' => 'private, max-age=31536000, immutable']
: [];
return new FileResponse($path, headers: $headers, autoEtag: true, autoLastModified: true);
}

throw new FileNotFoundException('Cannot find asset');
return $this->forward(ErrorsController::class, 'notFound');
}
}
5 changes: 2 additions & 3 deletions formwork/src/Panel/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Formwork\Router\RouteParams;
use Formwork\Users\User;
use Formwork\Utils\Arr;
use Formwork\Utils\Exceptions\FileNotFoundException;
use Formwork\Utils\FileSystem;

class UsersController extends AbstractController
Expand Down Expand Up @@ -217,11 +216,11 @@ public function images(RouteParams $routeParams): Response
{
$path = FileSystem::joinPaths($this->config->get('system.users.paths.images'), $routeParams->get('image'));

if (FileSystem::isFile($path)) {
if (FileSystem::isFile($path, assertExists: false)) {
return new FileResponse($path, headers: ['Cache-Control' => 'private, max-age=31536000, immutable'], autoEtag: true, autoLastModified: true);
}

throw new FileNotFoundException('Cannot find asset');
return $this->forward(ErrorsController::class, 'notFound');
}

/**
Expand Down

0 comments on commit cc34768

Please sign in to comment.