新增產生媒體及媒體分類假資料的指令

This commit is contained in:
kroutony 2020-02-23 21:47:53 +08:00
parent dee8c07bc0
commit d7b097d061
5 changed files with 270 additions and 0 deletions

View File

@ -0,0 +1,58 @@
<?php
namespace App\Console\Commands;
use App\MediaCategory;
use Illuminate\Console\Command;
class GenerateMediaCategory extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'media-category:generate {amount?} {--user=}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate media categories';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$userId = $this->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
]);
}
}
}
}

View File

@ -0,0 +1,63 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class GenerateMediaFiles extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'media:generate {amount?} {--user=} {--width=} {--height=} {--category=}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate media files';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$args = [
'imageCategory' => $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());
}
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace App\Exceptions;
use Exception;
class MediaFileFactoryServiceException extends Exception
{
//
}

View File

@ -0,0 +1,118 @@
<?php
namespace App\Services;
use App\Repositories\MediaFileRepository;
use Illuminate\Validation\ValidationException;
use Faker\Generator as Faker;
use File;
use mysql_xdevapi\Exception;
use Storage;
use App\User;
use Validator;
use App\Exceptions\MediaFileFactoryServiceException;
use App\Disk;
/**
* 產生假的媒體檔案
*
* Class MediaFileFactoryService
* @package App\Services
*/
class MediaFileFactoryService
{
private $mediaFileRepo;
private $faker;
public function __construct(MediaFileRepository $mediaFileRepository, Faker $faker)
{
$this->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;
}
}

View File

@ -0,0 +1,21 @@
<?php
/* @var $factory \Illuminate\Database\Eloquent\Factory */
use App\MediaCategory;
use Faker\Generator as Faker;
$factory->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,
];
});