119 lines
3.2 KiB
PHP
119 lines
3.2 KiB
PHP
<?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;
|
|
}
|
|
}
|