Created
December 23, 2021 10:10
-
-
Save EvilWolf/1b58d408419b2cd5dcee361a5096e18e to your computer and use it in GitHub Desktop.
Middleware for laravel orchid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Middleware; | |
use Closure; | |
use Illuminate\Http\Request; | |
use Illuminate\Http\UploadedFile; | |
use Illuminate\Support\Str; | |
use Illuminate\Validation\ValidationException; | |
class UploadFileExtensionWhiteList | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param Request $request | |
* @param Closure $next | |
* @return mixed | |
* @throws ValidationException | |
*/ | |
public function handle(Request $request, Closure $next) | |
{ | |
$extensionWhileList = config('app.upload_extensions_whitelist', []); | |
foreach ($request->allFiles() as $file) { | |
/** @var $file UploadedFile */ | |
$fileExtension = Str::lower($file->getClientOriginalExtension()); | |
if (!in_array($fileExtension, $extensionWhileList)) { | |
$fileExtension = htmlspecialchars(html_entity_decode($fileExtension)); | |
throw ValidationException::withMessages(["File \".{$fileExtension}\" not allowed to upload"]); | |
} | |
} | |
return $next($request); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment