Skip to content

Commit

Permalink
Handle file not found exception from storage as 404
Browse files Browse the repository at this point in the history
  • Loading branch information
gabsource committed Apr 12, 2020
1 parent c48761f commit 9078c45
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Http/Controllers/DownloadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,40 @@
namespace Bnb\Laravel\Attachments\Http\Controllers;

use Bnb\Laravel\Attachments\Contracts\AttachmentContract;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Lang;

class DownloadController extends Controller
{

/**
* Attachment model
*
* @var AttachmentContract
*/
protected $model;


public function __construct(AttachmentContract $model)
{
$this->model = $model;
}


public function download($id, Request $request)
{
$disposition = ($disposition = $request->input('disposition')) === 'inline' ? $disposition : 'attachment';

if ($file = $this->model->where('uuid', $id)->first()) {
/** @var \Bnb\Laravel\Attachments\Attachment $file */
if ( ! $file->output($disposition)) {
abort(403, Lang::get('attachments::messages.errors.access_denied'));
try {
/** @var \Bnb\Laravel\Attachments\Attachment $file */
if ( ! $file->output($disposition)) {
abort(403, Lang::get('attachments::messages.errors.access_denied'));
}
} catch (FileNotFoundException $e) {
abort(404, Lang::get('attachments::messages.errors.file_not_found'));
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/Http/Controllers/ShareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Carbon\Carbon;
use Crypt;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Lang;
Expand Down Expand Up @@ -46,10 +47,14 @@ public function download($token, Request $request)
}

if ($file = $this->model->where('uuid', $id)->first()) {
try {
/** @var AttachmentContract $file */
if ( ! $file->output($disposition)) {
abort(403, Lang::get('attachments::messages.errors.access_denied'));
}
} catch (FileNotFoundException $e) {
abort(404, Lang::get('attachments::messages.errors.file_not_found'));
}
}

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

0 comments on commit 9078c45

Please sign in to comment.