feat: add uploader

This commit is contained in:
JaguarJack 2023-01-11 17:17:36 +08:00
parent 68d378b4ef
commit d823f74015
15 changed files with 457 additions and 9 deletions

View File

@ -1,9 +1,9 @@
<?php
namespace Modules\Options\Http;
namespace Modules\Common\Http\Controllers;
use Exception;
use Modules\Options\Repository\Factory;
use Modules\Common\Repository\Options\Factory;
class OptionController
{

View File

@ -0,0 +1,34 @@
<?php
namespace Modules\Common\Http\Controllers;
use Illuminate\Http\Request;
use Modules\Common\Support\Upload\Uploader;
/**
* image
*/
class UploadController
{
/**
* @param Request $request
* @param Uploader $uploader
* @return string
*/
public function file(Request $request, Uploader $uploader)
{
return $uploader->upload($request->file('file'));
}
/**
* image
*
* @param Request $request
* @param Uploader $uploader
* @return string
*/
public function image(Request $request, Uploader $uploader)
{
return $uploader->upload($request->file('image'));
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace Modules\Common\Providers;
use Catch\CatchAdmin;
use Catch\Providers\CatchModuleServiceProvider;
use Modules\User\Events\Login;
use Modules\User\Listeners\Login as LoginListener;
use Modules\User\Middlewares\OperatingMiddleware;
class CommonServiceProvider extends CatchModuleServiceProvider
{
/**
* route path
*
* @return string|array
*/
public function routePath(): string|array
{
// TODO: Implement path() method.
return CatchAdmin::getModuleRoutePath('common');
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace Modules\Options\Repository;
namespace Modules\Common\Repository\Options;
use Catch\CatchAdmin;
use Illuminate\Support\Facades\File;

View File

@ -1,6 +1,6 @@
<?php
namespace Modules\Options\Repository;
namespace Modules\Common\Repository\Options;
use Catch\CatchAdmin;
use Illuminate\Support\Facades\File;

View File

@ -1,6 +1,6 @@
<?php
namespace Modules\Options\Repository;
namespace Modules\Common\Repository\Options;
use Modules\Permissions\Enums\DataRange as DataRangeEnum;

View File

@ -1,6 +1,6 @@
<?php
namespace Modules\Options\Repository;
namespace Modules\Common\Repository\Options;
use Exception;
use Illuminate\Support\Str;

View File

@ -1,6 +1,6 @@
<?php
namespace Modules\Options\Repository;
namespace Modules\Common\Repository\Options;
use Catch\Support\Module\ModuleRepository;

View File

@ -1,6 +1,6 @@
<?php
namespace Modules\Options\Repository;
namespace Modules\Common\Repository\Options;
interface OptionInterface
{

View File

@ -1,6 +1,6 @@
<?php
namespace Modules\Options\Repository;
namespace Modules\Common\Repository\Options;
use Catch\Enums\Status as StatusEnum;

View File

@ -0,0 +1,78 @@
<?php
namespace Modules\Common\Support\Upload;
use Catch\Exceptions\FailedException;
use Modules\Common\Support\Upload\Uses\LocalUpload;
use Illuminate\Http\UploadedFile;
class Uploader
{
protected string $driver = 'local';
/**
* path
*
* @var string
*/
protected string $path = '';
/**
* upload
*
* @param UploadedFile $file
* @return array
*/
public function upload(UploadedFile $file): array
{
try {
return $this->getDriver()->setUploadedFile($file)->upload();
} catch (\Exception $exception) {
throw new FailedException($exception->getMessage());
}
}
/**
* get driver
*
*/
public function getDriver()
{
$drivers = $this->getDrivers();
$driver = $drivers[$this->driver] ?? null;
if (! $driver) {
throw new FailedException('Upload Driver Not Found');
}
return app($driver);
}
/**
* set driver
*
* @param string $driver
* @return $this
*/
public function setDriver(string $driver): static
{
$this->driver = $driver;
return $this;
}
/**
* get drivers
*
* @return string[]
*/
public function getDrivers(): array
{
return [
'local' => LocalUpload::class
];
}
}

View File

@ -0,0 +1,54 @@
<?php
namespace Modules\Common\Support\Upload\Uses;
use Illuminate\Support\Facades\Storage;
class LocalUpload extends Upload
{
/**
* upload
*
* @return array
*/
public function upload(): array
{
return $this->addUrl($this->getUploadPath());
}
/**
* app url
*
* @param $path
* @return mixed
*/
protected function addUrl($path): mixed
{
$path['path'] = config('app.url') . '/'. $path['path'];
return $path;
}
/**
* local upload
*
* @return string
*/
protected function localUpload(): string
{
$this->checkSize();
$storePath = 'uploads' . DIRECTORY_SEPARATOR . $this->getUploadedFileMimeType() . DIRECTORY_SEPARATOR . date('Y-m-d', time());
$filename = $this->generateImageName($this->getUploadedFileExt());
Storage::build([
'driver' => 'local',
'root' => $storePath
])->put($filename, $this->file->getContent());
return $storePath . DIRECTORY_SEPARATOR . $filename;
}
}

View File

@ -0,0 +1,237 @@
<?php
namespace Modules\Common\Support\Upload\Uses;
use Catch\Exceptions\FailedException;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Str;
abstract class Upload
{
/**
* uploadFile object
*
* @var mixed
*/
protected mixed $file;
/**
*
* @var array
*/
protected array $params;
public abstract function upload();
/**
*
* @return mixed|true
*/
protected function dealBeforeUpload(): mixed
{
$this->checkExt();
$this->checkSize();
// 如果是上传图片资源的的话保存
// 如果是由其他方式上传的图片路径就直接返回
if (!$this->file instanceof UploadedFile) {
return $this->file;
}
// if ($this instanceof OssUploadService) {
// return $this->file->getPathname();
// }
return true;
}
/**
*
* @return array
*/
public function getUploadPath(): array
{
$method = $this->getUploadMethod();
return $this->info($this->{$method}());
}
/**
* 生成文件名称
*
* @time 2019年07月26日
* @param string $ext
* @return string
*/
protected function generateImageName(string $ext): string
{
$filename = $this->params['filename'] ?? '';
$randomString = date('Y') . Str::random(10) . time();
if ($filename) {
$randomString = $filename . '_' . $randomString;
}
return $randomString . '.' . $ext;
}
/**
* upload method
*
* @return string
*/
protected function getUploadMethod(): string
{
$class = get_called_class();
$class = explode('\\', $class);
$className = array_pop($class);
$method = lcfirst($className);
if (!method_exists($this, $method)) {
throw new FailedException(sprintf('Method %s in Class %s Not Found~', $method, $className));
}
return $method;
}
/**
* get uploaded file info
*
* @param $path
* @return array
*/
protected function info($path): array
{
return [
'path' => $path,
'ext' => $this->getUploadedFileExt(),
'type' => $this->getUploadedFileMimeType(),
'size' => $this->getUploadedFileSize(),
'originalName' => $this->getOriginName(),
];
}
/**
* check extension
*/
protected function checkExt(): void
{
$extensions = config(sprintf('upload.%s.ext', $this->getUploadedFileMimeType()));
$fileExt = $this->getUploadedFileExt();
if (!in_array($fileExt, $extensions)) {
throw new FailedException(sprintf('不支持该上传文件类型(%s)类型', $fileExt));
}
}
/**
* check file size
*/
protected function checkSize(): void
{
$size = 10 * 1024 * 1024;
if ($this->getUploadedFileSize() > $size) {
throw new FailedException('超过文件最大支持的大小');
}
}
/**
* get uploaded file mime type
*
* @return string
*/
protected function getUploadedFileMimeType(): string
{
if ($this->file instanceof UploadedFile) {
$imageMimeType = [
'image/gif', 'image/jpeg', 'image/png', 'application/x-shockwave-flash',
'image/psd', 'image/bmp', 'image/tiff', 'image/jp2',
'application/x-shockwave-flash', 'image/iff', 'image/vnd.wap.wbmp', 'image/xbm',
'image/vnd.microsoft.icon', 'image/x-icon', 'image/*', 'image/jpg',
];
return in_array($this->file->getClientMimeType(), $imageMimeType) ? 'image' : 'file';
}
return in_array($this->getUploadedFileExt(), config('upload.image.ext')) ? 'image' : 'file';
}
/**
* get uploaded file extension
*
* @return array|string
*/
protected function getUploadedFileExt(): array|string
{
if ($this->file instanceof UploadedFile) {
return strtolower($this->file->getClientOriginalExtension());
}
// 直传文件
return pathinfo($this->file, PATHINFO_EXTENSION);
}
/**
* get uploaded file size
*
* @return false|int
*/
protected function getUploadedFileSize(): bool|int
{
if ($this->file instanceof UploadedFile) {
return $this->file->getSize();
}
return 0;
}
/**
* get origin name
*
* @return string|null
*/
public function getOriginName(): ?string
{
// 上传图片获取
if ($this->file instanceof UploadedFile) {
return $this->file->getClientOriginalName();
}
return '';
}
/**
* 参数设置
*
* @time 2019年07月25日
* @param $name
* @param $value
*/
public function __set($name, $value)
{
$this->{$name} = $value;
}
/**
* set uploaded file
*
* @param mixed $file
* @return $this
*/
public function setUploadedFile(mixed $file): static
{
$this->file = $file;
return $this;
}
}

22
modules/Common/route.php Normal file
View File

@ -0,0 +1,22 @@
<?php
use Illuminate\Support\Facades\Route;
use Modules\Common\Http\Controllers\OptionController;
use Modules\Common\Http\Controllers\UploadController;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::get('options/{name}', [OptionController::class, 'index']);
Route::post('upload/file', [UploadController::class, 'file']);
Route::post('upload/image', [UploadController::class, 'image']);