From 68568fb8827dca49383e3294693017472af49313 Mon Sep 17 00:00:00 2001 From: Mohammad Khazaee Date: Mon, 22 Aug 2022 13:31:28 +0430 Subject: [PATCH] fix bugs --- app/Http/Controllers/FileController.php | 89 +++++++++++++++++++++++ app/Http/Controllers/Traits/FileTrait.php | 2 +- app/Image/ImageProcessor.php | 9 ++- docker-compose.yml | 1 + 4 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 app/Http/Controllers/FileController.php diff --git a/app/Http/Controllers/FileController.php b/app/Http/Controllers/FileController.php new file mode 100644 index 0000000..f159a36 --- /dev/null +++ b/app/Http/Controllers/FileController.php @@ -0,0 +1,89 @@ +castParams == 'resource') { + return new FileResource(app()->file); + } + if (!is_null(array_intersect(array_keys($request->all()), $this->availableParams))) { + $data = $imageProcessor->processToBuffer(app()->file->getPath(), $request->all()); + $response = Response::make($data, 200); + $response->header('Content-Type','image/webp'); + return $response; + } + return response()->file(app()->file->getPath()); + } + + public function store(FileStoreRequest $request, ImageProcessor $imageProcessor) + { + + $request->file = $imageProcessor->convertImage($request->file->path(), '/tmp/' . app()->uuid . '.' . app()->collection->ext); + if (!is_null(app()->collection->process)) { + $request->file = $imageProcessor->process($request->file->path(), '/tmp/' . app()->uuid . '.' . app()->collection->ext, app()->collection->process); + } + + $fileResource = null; + DB::transaction(function () use ($request, &$fileResource) { + $uuid = app()->uuid; + $fileResource = File::create([ + 'uuid' => $uuid, + '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' => '/' . date('y') . '/' . date('m') . '/', + ]); + if (!app()->collection->tmp_support && app()->collection->count == 1) { + File::where('user_id', auth()->id())->delete(); + } + + + $storedFile = Storage::disk(app()->collection->disk)->putFileAs($fileResource->server_path, $request->file, $fileResource->uuid . '.' . app()->collection->ext); + + if (app()->collection->public) { + Storage::disk(app()->collection->disk)->setVisibility($storedFile, 'public'); + } + }); + + return new FileResource($fileResource); + } + + public function update(FileUpdateRequest $request) + { + app()->file->update($request->all()); + return new FileResource(app()->file); + } + + public function destroy() + { + if (app()->file->trashed()) { + return app()->file->restore(); + } + return app()->file->delete(); + } +} diff --git a/app/Http/Controllers/Traits/FileTrait.php b/app/Http/Controllers/Traits/FileTrait.php index 0e22e27..3b5bddd 100644 --- a/app/Http/Controllers/Traits/FileTrait.php +++ b/app/Http/Controllers/Traits/FileTrait.php @@ -4,5 +4,5 @@ namespace App\Http\Controllers\Traits; trait FileTrait { - public $availableParams = ['r','w','h','canv','brightness','saturation','hue','rotation','flip']; + public $availableParams = ['q','r','w','h','canv','brightness','saturation','hue','rotation','flip']; } diff --git a/app/Image/ImageProcessor.php b/app/Image/ImageProcessor.php index 0e324b7..7aef495 100644 --- a/app/Image/ImageProcessor.php +++ b/app/Image/ImageProcessor.php @@ -99,7 +99,8 @@ class ImageProcessor $widthH, $options['w'], $options['h'], - ['extend' => 'background', 'background' => 1024] + ['extend' => 'background', 'background' => [255,255,255]] + ); } else { $image = Image::thumbnail($filename, $options['w'], ['height' => $options['h'], 'crop' => 'centre']); @@ -131,6 +132,10 @@ class ImageProcessor if (array_key_exists('q', $options)) { $saveOptions['Q'] = $options['q']; } + if (app()->file->ext == 'png') { + $saveOptions = array_merge(['palette'=>true],$saveOptions); + } + return $image->writeToBuffer("." . app()->file->ext, $saveOptions); } @@ -218,7 +223,7 @@ class ImageProcessor $widthH, $options['w'], $options['h'], - ['extend' => 'background', 'background' => 1024] + ['extend' => 'background', 'background' => 0] ); } else { $image = Image::thumbnail($filename, $options['w'], ['height' => $options['h'], 'crop' => 'centre']); diff --git a/docker-compose.yml b/docker-compose.yml index cb06596..000513d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,6 +9,7 @@ services: - '8000:80' volumes: - '.:/var/www/html' + mysql: image: 'mysql' tmpfs: /var/lib/mysql