Skip to content

Instantly share code, notes, and snippets.

@EvilWolf
Created December 23, 2021 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EvilWolf/1b58d408419b2cd5dcee361a5096e18e to your computer and use it in GitHub Desktop.
Save EvilWolf/1b58d408419b2cd5dcee361a5096e18e to your computer and use it in GitHub Desktop.
Middleware for laravel orchid
<?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