Skip to content

Commit

Permalink
Fix: #5026 - add friendy error message when failing to upload new med…
Browse files Browse the repository at this point in the history
…ia obect
  • Loading branch information
fisharebest committed Sep 14, 2024
1 parent ad20f96 commit 1fdc647
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
13 changes: 5 additions & 8 deletions app/Exceptions/FileUploadException.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,11 @@ public function __construct(?UploadedFileInterface $uploaded_file)
break;
}

$filename = $uploaded_file->getClientFilename() ?? '????????.???';

$message =
I18N::translate('There was an error uploading your file.') .
'<br>' .
I18N::translate('%1$s: %2$s', I18N::translate('Filename'), e($filename)) .
'<br>' .
$message;
$filename = $uploaded_file->getClientFilename() ?? '';

if ($filename !== '') {
$message = I18N::translate('%1$s: %2$s', e($filename), $message);
}

parent::__construct($message);
}
Expand Down
14 changes: 13 additions & 1 deletion app/Http/RequestHandlers/CreateMediaObjectAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace Fisharebest\Webtrees\Http\RequestHandlers;

use Fig\Http\Message\StatusCodeInterface;
use Fisharebest\Webtrees\Exceptions\FileUploadException;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Services\MediaFileService;
Expand All @@ -30,6 +31,7 @@
use Psr\Http\Server\RequestHandlerInterface;

use function response;
use function view;

/**
* Process a form to create a new media object.
Expand Down Expand Up @@ -70,7 +72,17 @@ public function handle(ServerRequestInterface $request): ResponseInterface
$title = Registry::elementFactory()->make('OBJE:FILE:TITL')->canonical($title);
$restriction = Registry::elementFactory()->make('OBJE:RESN')->canonical($restriction);

$file = $this->media_file_service->uploadFile($request);
try {
$file = $this->media_file_service->uploadFile($request);
} catch (FileUploadException $exception) {
return response([
'value' => '',
'text' => '',
'html' => view('components/alert-danger', [
'alert' => $exception->getMessage(),
]),
]);
}

if ($file === '') {
return response(['error_message' => I18N::translate('There was an error uploading your file.')], StatusCodeInterface::STATUS_NOT_ACCEPTABLE);
Expand Down
Loading

0 comments on commit 1fdc647

Please sign in to comment.