Skip to content

Commit 9078c45

Browse files
committed
Handle file not found exception from storage as 404
1 parent c48761f commit 9078c45

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/Http/Controllers/DownloadController.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,40 @@
33
namespace Bnb\Laravel\Attachments\Http\Controllers;
44

55
use Bnb\Laravel\Attachments\Contracts\AttachmentContract;
6+
use Illuminate\Contracts\Filesystem\FileNotFoundException;
67
use Illuminate\Http\Request;
78
use Illuminate\Routing\Controller;
89
use Lang;
910

1011
class DownloadController extends Controller
1112
{
13+
1214
/**
1315
* Attachment model
1416
*
1517
* @var AttachmentContract
1618
*/
1719
protected $model;
1820

21+
1922
public function __construct(AttachmentContract $model)
2023
{
2124
$this->model = $model;
2225
}
2326

27+
2428
public function download($id, Request $request)
2529
{
2630
$disposition = ($disposition = $request->input('disposition')) === 'inline' ? $disposition : 'attachment';
2731

2832
if ($file = $this->model->where('uuid', $id)->first()) {
29-
/** @var \Bnb\Laravel\Attachments\Attachment $file */
30-
if ( ! $file->output($disposition)) {
31-
abort(403, Lang::get('attachments::messages.errors.access_denied'));
33+
try {
34+
/** @var \Bnb\Laravel\Attachments\Attachment $file */
35+
if ( ! $file->output($disposition)) {
36+
abort(403, Lang::get('attachments::messages.errors.access_denied'));
37+
}
38+
} catch (FileNotFoundException $e) {
39+
abort(404, Lang::get('attachments::messages.errors.file_not_found'));
3240
}
3341
}
3442

src/Http/Controllers/ShareController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Carbon\Carbon;
77
use Crypt;
88
use Illuminate\Contracts\Encryption\DecryptException;
9+
use Illuminate\Contracts\Filesystem\FileNotFoundException;
910
use Illuminate\Http\Request;
1011
use Illuminate\Routing\Controller;
1112
use Lang;
@@ -46,10 +47,14 @@ public function download($token, Request $request)
4647
}
4748

4849
if ($file = $this->model->where('uuid', $id)->first()) {
50+
try {
4951
/** @var AttachmentContract $file */
5052
if ( ! $file->output($disposition)) {
5153
abort(403, Lang::get('attachments::messages.errors.access_denied'));
5254
}
55+
} catch (FileNotFoundException $e) {
56+
abort(404, Lang::get('attachments::messages.errors.file_not_found'));
57+
}
5358
}
5459

5560
abort(404, Lang::get('attachments::messages.errors.file_not_found'));

0 commit comments

Comments
 (0)