diff --git a/app/Http/Controllers/FileController.php b/app/Http/Controllers/FileController.php index 14d3c28..136f81b 100644 --- a/app/Http/Controllers/FileController.php +++ b/app/Http/Controllers/FileController.php @@ -3,18 +3,86 @@ namespace App\Http\Controllers; use App\Http\Requests\FileStoreRequest; +use App\Http\Resources\FileResource; +use App\Jobs\FileConversionQueue; +use App\Models\File; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Storage; class FileController extends Controller { - public function show($id) + public function show($collection, $uuid, $ext) { - // + dump($collection, $uuid, $ext); + } + + public function private($collection, $path) + { + Storage::disk(app()->collection->disk)->download($path); } public function store(Request $request) { - // $fileStoreRequest; + + // skip policy ---and first level---------- + + $validated = $request->validate([ + "file" => [ + "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, + ], + "alts" => [app()->collection->alt_required ? "required" : "null", 'array'], + "alts.*" => [app()->collection->alt_required ? "required" : "null"], + "description" => [app()->collection->description_required ? "required" : "null"], + ]); + + + + + DB::beginTransaction(); + + $urlStorage = '/' . date('y') . '/' . date('m') . app()->getFileUuid; + + try { + $file = File::create([ + 'uuid' => app()->getFileUuid, + 'original_name' => $request->name, + 'public' => $request->public, + 'ext' => $request->file->extension(), + 'mimetype' => $request->file->getMimeType(), + 'width' => getimagesize($request->file)[0], + 'height' => getimagesize($request->file)[1], + 'file_size' => $request->file->getSize(), + 'sort' => $request->file->getSize(), + 'alts' => $request->alts, + 'description' => $request->description, + 'user_id' => auth()->id(), + 'ip' => $request->ip(), + 'collection_id' => app()->collection->id, + 'published_at' => $request->published_at, + 'server_path' => $urlStorage + ]); + + if (!app()->collection->tmp_support && app()->collection->count == 1) { + File::where('user_id', auth()->id())->delete(); + } + + $file = $request->file->storeAs($urlStorage, app()->getFileUuid, app()->collection->disk); + if (app()->collection->public) { + Storage::setVisibility($file, 'public'); + } + + DB::commit(); + } catch (\Exception $e) { + DB::rollback(); + } + + FileConversionQueue::dispatch($file, app()->collection); + return new FileResource($file); } diff --git a/app/Http/Middleware/BindCollectionModelMiddleware.php b/app/Http/Middleware/BindCollectionModelMiddleware.php index 2461825..7d70045 100644 --- a/app/Http/Middleware/BindCollectionModelMiddleware.php +++ b/app/Http/Middleware/BindCollectionModelMiddleware.php @@ -5,6 +5,7 @@ namespace App\Http\Middleware; use App\Models\Collection; use Closure; use Illuminate\Http\Request; +use Illuminate\support\Str; class BindCollectionModelMiddleware { @@ -18,9 +19,18 @@ class BindCollectionModelMiddleware 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(); + app()->bind('collection', function () use ($request) { + return Collection::where('name', $request->route('collections_name'))->get(); + }); + + app()->singleton('getFileUuid', function () { + return Str::uuid(); + }); + } + + if ($request->route()->action['as'] == 'api.file.private') { + app()->bind('collection', function () use ($request) { + return Collection::where('name', $request->route('collections_name'))->get(); }); } diff --git a/app/Http/Resources/CollectionResource.php b/app/Http/Resources/CollectionResource.php index a855bcf..24773b9 100644 --- a/app/Http/Resources/CollectionResource.php +++ b/app/Http/Resources/CollectionResource.php @@ -33,7 +33,7 @@ class CollectionResource extends JsonResource "count" => $this->count, "exts" => $this->exts, "avalible_exts" => $this->avalible_exts, - "memetypes" => $this->memetypes, + "mimetypes" => $this->mimetypes, "model" => $this->model, "expire_date" => $this->expire_date, "created_at" => $this->created_at, diff --git a/app/Http/Resources/FileResource.php b/app/Http/Resources/FileResource.php index 8a3d851..6eaf9fd 100644 --- a/app/Http/Resources/FileResource.php +++ b/app/Http/Resources/FileResource.php @@ -18,7 +18,7 @@ class FileResource extends JsonResource "uuid" => $this->uuid, "original_name" => $this->original_name, "ext" => $this->ext, - "memetype" => $this->memetype, + "mimetype" => $this->mimetype, "width" => $this->width, "height" => $this->height, "file_size" => $this->file_size, diff --git a/app/Jobs/FileConversionQueue.php b/app/Jobs/FileConversionQueue.php new file mode 100644 index 0000000..287f1a1 --- /dev/null +++ b/app/Jobs/FileConversionQueue.php @@ -0,0 +1,38 @@ +file = $file; + $this->collection = $collection; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + // + } +} diff --git a/app/Models/Collection.php b/app/Models/Collection.php index 714e106..7fd6714 100644 --- a/app/Models/Collection.php +++ b/app/Models/Collection.php @@ -29,7 +29,7 @@ class Collection extends Model "description_required", "exts", "avalible_exts", - "memetypes", + "mimetypes", "model", "expire_date", ]; @@ -37,7 +37,7 @@ class Collection extends Model protected $casts = [ 'exts' => 'array', 'avalible_exts' => 'array', - 'memetypes' => 'array', + 'mimetypes' => 'array', ]; protected function exts(): Attribute @@ -52,15 +52,13 @@ class Collection extends Model set: fn ($value) => json_encode($value), ); } - protected function memetypes(): Attribute + protected function mimetypes(): Attribute { return Attribute::make( set: fn ($value) => json_encode($value), ); } - - public function getExts() { return implode(",", app()->collection->exts); diff --git a/app/Models/File.php b/app/Models/File.php index 9d44a4c..3df7b3b 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, Validatable; + use HasFactory, SoftDeletes; protected $primaryKey = 'uuid'; @@ -19,7 +19,7 @@ class File extends Model "uuid", "original_name", "ext", - "memetype", + "mimetype", "width", "server_path", "height", @@ -37,13 +37,6 @@ class File extends Model 'alts' => 'array', ]; - protected function uuid(): Attribute - { - return Attribute::make( - set: fn ($value) => Str::uuid(), - ); - } - protected function alts(): Attribute { @@ -52,16 +45,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, - ], - ]; - } + // 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/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index ee8ca5b..34c5d64 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,8 @@ namespace App\Providers; +use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Facades\URL; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -23,6 +25,12 @@ class AppServiceProvider extends ServiceProvider */ public function boot() { - // + Storage::disk('local')->buildTemporaryUrlsUsing(function ($path, $expiration, $options) { + return URL::temporarySignedRoute( + 'api.file.private', + $expiration, + array_merge($options, ['path' => $path]) + ); + }); } } diff --git a/config/filesystems.php b/config/filesystems.php index e9d9dbd..1b24a72 100644 --- a/config/filesystems.php +++ b/config/filesystems.php @@ -33,16 +33,28 @@ return [ 'local' => [ 'driver' => 'local', 'root' => storage_path('app'), + 'url' => env('APP_URL') . '/storage', + 'visibility' => 'private', 'throw' => false, + 'permissions' => [ + 'file' => [ + 'public' => 0644, + 'private' => 0600, + ], + 'dir' => [ + 'public' => 0755, + 'private' => 0700, + ], + ], ], - 'public' => [ - 'driver' => 'local', - 'root' => storage_path('app/public'), - 'url' => env('APP_URL').'/storage', - 'visibility' => 'public', - 'throw' => false, - ], + // 'public' => [ + // 'driver' => 'local', + // 'root' => storage_path('app/public'), + // 'url' => env('APP_URL') . '/storage', + // 'visibility' => 'public', + // 'throw' => false, + // ], 's3' => [ 'driver' => 's3', @@ -70,7 +82,7 @@ return [ */ 'links' => [ - public_path('storage') => storage_path('app/public'), + // public_path('storage') => storage_path('app/public'), ], ]; diff --git a/database/factories/CollectionFactory.php b/database/factories/CollectionFactory.php index 80bdf20..1ca7151 100644 --- a/database/factories/CollectionFactory.php +++ b/database/factories/CollectionFactory.php @@ -44,7 +44,7 @@ class CollectionFactory extends Factory "png", "webp" ], - "memetypes" => [ + "mimetypes" => [ "jpg", "jpeg", "png", diff --git a/database/factories/FileFactory.php b/database/factories/FileFactory.php index 22a7549..5ebfb9d 100644 --- a/database/factories/FileFactory.php +++ b/database/factories/FileFactory.php @@ -21,7 +21,7 @@ class FileFactory extends Factory "uuid" => 1, "original_name" => fake()->name(), "ext" => ['jpg', 'jpeg', 'png', 'webp'][rand(0, 3)], - "memetype" => 'image', + "mimetype" => 'image', "width" => rand(300, 2000), "height" => rand(300, 2000), "file_size" => rand(300, 2000), diff --git a/database/migrations/2022_07_27_073906_create_collections_table.php b/database/migrations/2022_07_27_073906_create_collections_table.php index 328f0e4..09a587a 100644 --- a/database/migrations/2022_07_27_073906_create_collections_table.php +++ b/database/migrations/2022_07_27_073906_create_collections_table.php @@ -34,7 +34,7 @@ return new class extends Migration $table->json("avalible_exts")->nullable(); $table->json("mimetypes")->nullable(); $table->string("model")->unique()->nullable(); - $table->timestamp("expire_date")->nullable(); + $table->time("expire_date")->nullable(); $table->timestamps(); $table->softDeletes(); }); diff --git a/public/image-modified.jpeg b/public/image-modified.jpeg deleted file mode 100644 index fc73601..0000000 Binary files a/public/image-modified.jpeg and /dev/null differ diff --git a/public/image-modified.png b/public/image-modified.png deleted file mode 100644 index 96e5ca4..0000000 Binary files a/public/image-modified.png and /dev/null differ diff --git a/public/image-modified.webp b/public/image-modified.webp index 72b2702..fa30048 100644 Binary files a/public/image-modified.webp and b/public/image-modified.webp differ diff --git a/public/image-modified2.jpg b/public/image-modified2.jpg deleted file mode 100644 index c25b919..0000000 Binary files a/public/image-modified2.jpg and /dev/null differ diff --git a/public/image.jpg b/public/image.jpg deleted file mode 100644 index 54e5b4f..0000000 Binary files a/public/image.jpg and /dev/null differ diff --git a/public/imagecircle.png b/public/imagecircle.png deleted file mode 100644 index 758dfc7..0000000 Binary files a/public/imagecircle.png and /dev/null differ diff --git a/resources/views/welcome.blade.php b/resources/views/welcome.blade.php index c002ac3..f3ee5b1 100644 --- a/resources/views/welcome.blade.php +++ b/resources/views/welcome.blade.php @@ -1,32 +1,41 @@ - - - - - Laravel - - - - - - - - - - - -
- @csrf - -
- -
- + + + + + + Laravel + + + + + + + + + + +
+ @csrf + +
+ + + +
+
+
+
+
+
+
+
+
+
+
+ +
+ + diff --git a/routes/api.php b/routes/api.php index 853666b..f6d9471 100644 --- a/routes/api.php +++ b/routes/api.php @@ -21,6 +21,8 @@ use Illuminate\Support\Facades\Route; 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'); + Route::delete('collections/{collection}', [CollectionController::class, "destroy"])->withTrashed(); + Route::get('{collection_name}/{uuid}.{extention}', [FileController::class, 'show'])->name('file.show'); + Route::get('{collection_name}/{path}', [FileController::class, 'private'])->name('file.private'); + Route::post('{collection_name}', [FileController::class, 'store'])->name('file.store'); });