From a350cf8a88eb39700d8b23fc2d0be0fcacd26949 Mon Sep 17 00:00:00 2001 From: Mohammad Khazaee Date: Wed, 27 Jul 2022 18:51:40 +0430 Subject: [PATCH] add more functionalliy --- app/Http/Controllers/FileController.php | 4 +- app/Http/Kernel.php | 4 +- .../BindCollectionModelMiddleware.php | 29 +++++++++ app/Http/Requests/FileStoreRequest.php | 60 ------------------- app/Models/Collection.php | 12 ++++ app/Models/File.php | 14 ++++- app/Providers/RouteServiceProvider.php | 22 +++---- config/app.php | 1 + routes/api.php | 9 +-- 9 files changed, 76 insertions(+), 79 deletions(-) create mode 100644 app/Http/Middleware/BindCollectionModelMiddleware.php delete mode 100644 app/Http/Requests/FileStoreRequest.php diff --git a/app/Http/Controllers/FileController.php b/app/Http/Controllers/FileController.php index d5cd5be..14d3c28 100644 --- a/app/Http/Controllers/FileController.php +++ b/app/Http/Controllers/FileController.php @@ -12,9 +12,9 @@ class FileController extends Controller // } - public function store(FileStoreRequest $fileStoreRequest) + public function store(Request $request) { - $fileStoreRequest; + // $fileStoreRequest; } diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 69b8bca..337be02 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -2,6 +2,7 @@ namespace App\Http; +use App\Http\Middleware\BindCollectionModelMiddleware; use App\Http\Middleware\BindModelDocumentMiddleware; use Illuminate\Foundation\Http\Kernel as HttpKernel; @@ -43,7 +44,8 @@ class Kernel extends HttpKernel // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class, 'throttle:api', \Illuminate\Routing\Middleware\SubstituteBindings::class, - BindModelDocumentMiddleware::class + // BindModelDocumentMiddleware::class, + BindCollectionModelMiddleware::class ], ]; diff --git a/app/Http/Middleware/BindCollectionModelMiddleware.php b/app/Http/Middleware/BindCollectionModelMiddleware.php new file mode 100644 index 0000000..2461825 --- /dev/null +++ b/app/Http/Middleware/BindCollectionModelMiddleware.php @@ -0,0 +1,29 @@ +route()->action['as'] == 'api.file.store') { + app()->bind('collection',function() use ($request) + { + return Collection::where('name',$request->route('collections_name'))->get(); + }); + } + + return $next($request); + } +} diff --git a/app/Http/Requests/FileStoreRequest.php b/app/Http/Requests/FileStoreRequest.php deleted file mode 100644 index 1e6ac41..0000000 --- a/app/Http/Requests/FileStoreRequest.php +++ /dev/null @@ -1,60 +0,0 @@ -collection = Collection::where('name',$this->collection_name)->get(); - } - - /** - * Determine if the user is authorized to make this request. - * - * @return bool - */ - public function authorize() - { - return true; - } - - private function getExts() - { - return implode(",",$this->collection->exts); - } - - private function getMimeTypes() - { - return implode(",",$this->collection->mimetypes); - } - - // private function getExt() - // { - // return implode(",",$this->collection->ext); - // } - - /** - * Get the validation rules that apply to the request. - * - * @return array - */ - public function rules() - { - return [ - "ext" => ["mimes:" . $this->getExts], - "memetype" => ["mimetypes:" . $this->getMimeTypes], - "width"=> [''], - "height"=> [''], - "file_size"=> [''], - ]; - } -} diff --git a/app/Models/Collection.php b/app/Models/Collection.php index 0f77ea0..714e106 100644 --- a/app/Models/Collection.php +++ b/app/Models/Collection.php @@ -59,4 +59,16 @@ class Collection extends Model ); } + + + public function getExts() + { + return implode(",", app()->collection->exts); + } + + public function getMimeTypes() + { + return implode(",", app()->collection->mimetypes); + } + } diff --git a/app/Models/File.php b/app/Models/File.php index e34f328..9d44a4c 100644 --- a/app/Models/File.php +++ b/app/Models/File.php @@ -11,7 +11,7 @@ use Illuminate\Support\Str; class File extends Model { - use HasFactory, SoftDeletes; + use HasFactory, SoftDeletes, Validatable; protected $primaryKey = 'uuid'; @@ -52,4 +52,16 @@ class File extends Model ); } + public function rules(): array + { + return [ + "image" => [ + "mimes:" . app()->collection->getExts(), + "mimetypes:" . app()->collection->getMimeTypes(), + "dimensions:min_width=" . app()->collection->min_width . ",min_height=" . app()->collection->min_height . ',max_width=' . app()->collection->max_width . ',max_height='. app()->collection->max_height, + "max:" . app()->collection->max_size, + "min:" . app()->collection->min_size, + ], + ]; + } } diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index a598ea5..28dec01 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -45,15 +45,15 @@ class RouteServiceProvider extends ServiceProvider }); } - /** - * Configure the rate limiters for the application. - * - * @return void - */ - protected function configureRateLimiting() - { - RateLimiter::for('api', function (Request $request) { - return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip()); - }); - } + /** + * Configure the rate limiters for the application. + * + * @return void + */ + protected function configureRateLimiting() + { + RateLimiter::for('api', function (Request $request) { + return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip()); + }); } +} diff --git a/config/app.php b/config/app.php index 4977c05..e8b5010 100644 --- a/config/app.php +++ b/config/app.php @@ -195,6 +195,7 @@ return [ App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, App\Providers\SettingServiceProvider::class, + // App\Providers\CollectionServiceProvider::class ], /* diff --git a/routes/api.php b/routes/api.php index 4669c61..853666b 100644 --- a/routes/api.php +++ b/routes/api.php @@ -19,7 +19,8 @@ use Illuminate\Support\Facades\Route; // return $request->user(); // }); - -Route::apiResource('collections',CollectionController::class); -Route::delete('/collections/{collection}',[CollectionController::class,"destroy"])->withTrashed(); -Route::post('{collection_name}/{?model_name}',[FileController::class,'store']); +Route::group(['as' => 'api.'], function () { + Route::apiResource('collections', CollectionController::class); + Route::delete('/collections/{collection}', [CollectionController::class, "destroy"])->withTrashed(); + Route::post('{collection_name}/{model_name?}', [FileController::class, 'store'])->name('file.store'); +});