Skip to content

Commit

Permalink
Update MediaController.php
Browse files Browse the repository at this point in the history
  • Loading branch information
nasrulhazim authored Oct 25, 2022
1 parent 0bbde52 commit afdccb1
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/Http/Controllers/MediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@ public function __invoke(Request $request, string $type, string $uuid)
{
abort_if(! MediaAccess::acceptable($type), 422, 'Invalid request type of '.$type);

$media = config('laravel-media-secure.model')::whereUuid($uuid)->firstOrFail();
$media = config('media-library.media_model')::whereUuid($uuid)->firstOrFail();

abort_if($request->user()->cannot($type, $media), 403, 'Unauthorized Access.');

return match ($type) {
MediaAccess::stream()->value => response()->streamDownload(function () use ($media) {
echo file_get_contents($media->getPath());
}),
MediaAccess::view()->value => response()->file($media->getPath()),
MediaAccess::download()->value => response()->download($media->getPath()),
default => abort(422, 'Invalid media request')
};
if($type == MediaAccess::view()->value || $type == MediaAccess::stream()->value) {
return response()->make(file_get_contents($media->getPath()), 200, [
'Content-Type' => $media->mime_type,
'Content-Disposition' => 'inline; filename="'.$media->file_name.'"'
]);
}

if($type == MediaAccess::download()->value) {
return response()->download($media->getPath());
}

return abort(422, 'Invalid media request');
}
}

0 comments on commit afdccb1

Please sign in to comment.