From d7b097d061d29283cf5c071e32e0953a5335bc3f Mon Sep 17 00:00:00 2001 From: kroutony Date: Sun, 23 Feb 2020 21:47:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=94=A2=E7=94=9F=E5=AA=92?= =?UTF-8?q?=E9=AB=94=E5=8F=8A=E5=AA=92=E9=AB=94=E5=88=86=E9=A1=9E=E5=81=87?= =?UTF-8?q?=E8=B3=87=E6=96=99=E7=9A=84=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/GenerateMediaCategory.php | 58 +++++++++ app/Console/Commands/GenerateMediaFiles.php | 63 ++++++++++ .../MediaFileFactoryServiceException.php | 10 ++ app/Services/MediaFileFactoryService.php | 118 ++++++++++++++++++ database/factories/MediaCategoryFactory.php | 21 ++++ 5 files changed, 270 insertions(+) create mode 100644 app/Console/Commands/GenerateMediaCategory.php create mode 100644 app/Console/Commands/GenerateMediaFiles.php create mode 100644 app/Exceptions/MediaFileFactoryServiceException.php create mode 100644 app/Services/MediaFileFactoryService.php create mode 100644 database/factories/MediaCategoryFactory.php diff --git a/app/Console/Commands/GenerateMediaCategory.php b/app/Console/Commands/GenerateMediaCategory.php new file mode 100644 index 0000000..8bc5345 --- /dev/null +++ b/app/Console/Commands/GenerateMediaCategory.php @@ -0,0 +1,58 @@ +option('user') !== null ? $this->option('user') : 1; + $isAppMediaCategory = true; + $amount = $this->argument('amount'); + if(!$amount) $amount = 1; + + for($i = 0; $i < $amount; $i++) { + if($isAppMediaCategory) { + factory(MediaCategory::class)->state('app')->create([ + 'user_id' => $userId + ]); + } else { + factory(MediaCategory::class)->create([ + 'user_id' => $userId + ]); + } + } + } +} diff --git a/app/Console/Commands/GenerateMediaFiles.php b/app/Console/Commands/GenerateMediaFiles.php new file mode 100644 index 0000000..594dcfb --- /dev/null +++ b/app/Console/Commands/GenerateMediaFiles.php @@ -0,0 +1,63 @@ + $this->option('category'), + 'imageHeight' => (int)$this->option('height'), + 'imageWidth' => (int)$this->option('width'), + 'userId' => $this->option('user') + ]; + $args = collect($args)->filter(function($value, $key){ + return $value !== null && $value !== 0; + })->all(); + + $amount = $this->argument('amount'); + if(!$amount) $amount = 1; + + try { + for($i = 0; $i < $amount; $i++) { + $mediaFile = app(\App\Services\MediaFileFactoryService::class)->create($args); + $this->info("Media file {$mediaFile->id} is generated"); + } + } catch (\Exception $e) { + $this->error($e->getMessage()); + } + + } +} diff --git a/app/Exceptions/MediaFileFactoryServiceException.php b/app/Exceptions/MediaFileFactoryServiceException.php new file mode 100644 index 0000000..9447cd2 --- /dev/null +++ b/app/Exceptions/MediaFileFactoryServiceException.php @@ -0,0 +1,10 @@ +mediaFileRepo = $mediaFileRepository; + + $this->faker = $faker; + } + + private function getException($message = '') + { + return (new MediaFileFactoryServiceException($message)); + } + + /** + * @param $args + * @return \App\MediaFile + * @throws \Exception + */ + public function create($args) + { + $defaultArgs = [ + 'isAppMedia' => true, + 'userId' => 1, + 'imageCategory' => null, + 'imageWidth' => 640, + 'imageHeight' => 640, + 'description' => null, + 'diskId' => null, + 'path' => null, + ]; + + $args = array_merge($defaultArgs, $args); + + $validator = Validator::make($args, [ + 'isAppMedia' => 'boolean', + 'userId' => 'exists:users,id', + 'imageWidth' => 'numeric', + 'imageHeight' => 'numeric' + ]); + + if($validator->fails()) { + throw $this->getException($validator->getMessageBag()->first()); + } + + $isAppMedia = $args['isAppMedia']; + $userId = $args['userId']; + $imageCategory = $args['imageCategory']; + $imageWidth = $args['imageWidth']; + $imageHeight = $args['imageHeight']; + $description = $args['description']; + $diskId = $args['diskId']; + $path = $args['path']; + + $dislModel = Disk::find($diskId); + if($dislModel && !$isAppMedia) { + $disk = Storage::disk($dislModel->name); + } else { + $disk = $isAppMedia ? Storage::disk('uploadedAppMedias') : Storage::disk('uploadedUserMedias'); + } + if($path && !$isAppMedia) { + $diskPath = $path; + } else { + $diskPath = $isAppMedia ? '' : $userId; + } + if(!$disk->exists($diskPath)) { + $disk->createDir($diskPath); + } + $imagePath = $disk->path($diskPath); + $mediaFile = $this->mediaFileRepo->createModel(); + $image = $this->faker->image($imagePath, $imageWidth, $imageHeight, $imageCategory); + $imageFile = File::get($image); + $fileSize = File::size($image); + $fileName = File::basename($image); + + $mediaFile->fill([ + 'path' => $path, + 'file_name' => $fileName, + 'ext' => 'jpg', + 'disk_id' => $dislModel && !$isAppMedia ? $diskId : ($isAppMedia ? 1 : 2), + 'size' => $fileSize, + 'mime_type' => 'image/jpeg', + 'is_app_media' => $isAppMedia, + 'width' => $imageWidth, + 'height' => $imageHeight, + 'user_id' => $userId, + 'description' => $description + ]); + $mediaFile->save(); + return $mediaFile; + } +} diff --git a/database/factories/MediaCategoryFactory.php b/database/factories/MediaCategoryFactory.php new file mode 100644 index 0000000..54d6d13 --- /dev/null +++ b/database/factories/MediaCategoryFactory.php @@ -0,0 +1,21 @@ +define(MediaCategory::class, function (Faker $faker) { + return [ + 'name' => $faker->firstName, + 'is_app_media_category' => false, + 'user_id' => 1 + ]; +}); + +$factory->state(MediaCategory::class, 'app', function (Faker $faker) { + return [ + 'is_app_media_category' => true, + ]; +}); +