Browse Source

add more functionalliy

master
Mohammad Khazaee 2 years ago
parent
commit
a350cf8a88
  1. 4
      app/Http/Controllers/FileController.php
  2. 4
      app/Http/Kernel.php
  3. 29
      app/Http/Middleware/BindCollectionModelMiddleware.php
  4. 60
      app/Http/Requests/FileStoreRequest.php
  5. 12
      app/Models/Collection.php
  6. 14
      app/Models/File.php
  7. 22
      app/Providers/RouteServiceProvider.php
  8. 1
      config/app.php
  9. 9
      routes/api.php

4
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;
}

4
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
],
];

29
app/Http/Middleware/BindCollectionModelMiddleware.php

@ -0,0 +1,29 @@
<?php
namespace App\Http\Middleware;
use App\Models\Collection;
use Closure;
use Illuminate\Http\Request;
class BindCollectionModelMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
if ($request->route()->action['as'] == 'api.file.store') {
app()->bind('collection',function() use ($request)
{
return Collection::where('name',$request->route('collections_name'))->get();
});
}
return $next($request);
}
}

60
app/Http/Requests/FileStoreRequest.php

@ -1,60 +0,0 @@
<?php
namespace App\Http\Requests;
use App\Models\Collection;
use Illuminate\Foundation\Http\FormRequest;
class FileStoreRequest extends FormRequest
{
private $collection = null;
public function __construct()
{
$this->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<string, mixed>
*/
public function rules()
{
return [
"ext" => ["mimes:" . $this->getExts],
"memetype" => ["mimetypes:" . $this->getMimeTypes],
"width"=> [''],
"height"=> [''],
"file_size"=> [''],
];
}
}

12
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);
}
}

14
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,
],
];
}
}

22
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());
});
}
}

1
config/app.php

@ -195,6 +195,7 @@ return [
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\SettingServiceProvider::class,
// App\Providers\CollectionServiceProvider::class
],
/*

9
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');
});
Loading…
Cancel
Save