123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\common\library\storage\engine;
- use app\common\exception\BaseException;
- use think\facade\Request;
- use think\file\UploadedFile;
- /**
- * 存储引擎抽象类
- * Class Basics
- * @package app\common\library\storage\drivers
- */
- abstract class Basics
- {
- // 当前存储引擎
- protected $storage;
- // 存储配置
- protected $config;
- // file对象句柄
- /* @var $file UploadedFile */
- protected $file;
- // 验证规则
- protected $validateRuleScene;
- // 磁盘配置
- protected $disk = 'public';
- // 保存的根文件夹名称
- protected $rootDirName;
- // 错误信息
- protected $error;
- /**
- * 构造函数
- * Server constructor.
- * @param string $storage 存储方式
- * @param array|null $config 存储配置
- */
- public function __construct(string $storage, array $config = null)
- {
- $this->storage = $storage;
- $this->config = $config;
- }
- /**
- * 设置上传的文件信息 (外部用户上传)
- * @param string $name
- * @return $this
- * @throws BaseException
- */
- public function setUploadFile(string $name)
- {
- // 接收上传的文件
- try {
- $this->file = Request::file($name);
- } catch (\Exception $e) {
- $this->throwFileError($e);
- }
- empty($this->file) && throwError('未找到上传文件的信息');
- return $this;
- }
- /**
- * 文件异常处理
- * @param \Exception $e
- * @throws BaseException
- */
- private function throwFileError(\Exception $e)
- {
- $maxSize = ini_get('upload_max_filesize');
- $myMsg = $e->getCode() === 1 ? "上传的文件超出了服务器最大限制: {$maxSize}" : false;
- throwError($myMsg ?: $e->getMessage());
- }
- /**
- * 设置上传的文件信息 (系统内部上传)
- * @param string $filePath 文件路径
- * @return $this
- * @throws BaseException
- */
- public function setUploadFileByReal(string $filePath)
- {
- // 接收上传的文件
- $this->file = new UploadedFile($filePath, basename($filePath));
- if (empty($this->file)) {
- throwError('未找到上传文件的信息');
- }
- return $this;
- }
- /**
- * 设置上传文件的验证规则
- * @param string $scene
- * @return mixed
- */
- public function setValidationScene(string $scene = '')
- {
- $this->validateRuleScene = $scene;
- return $this;
- }
- /**
- * 设置磁盘配置
- * @param string $disk
- * @return $this
- */
- public function setDisk(string $disk)
- {
- $this->disk = $disk;
- return $this;
- }
- /**
- * 设置上传的文件根目录名称
- * [通常是商城的id, 例如: 10001]
- * @param string $name
- * @return $this
- */
- public function setRootDirName(string $name)
- {
- $this->rootDirName = $name;
- return $this;
- }
- /**
- * 文件上传
- * @return mixed
- */
- abstract protected function upload();
- /**
- * 文件删除
- * @param string $filePath
- * @return mixed
- */
- abstract protected function delete(string $filePath);
- /**
- * 临时文件的绝对路径
- * @return mixed
- */
- protected function getRealPath(): string
- {
- return $this->file->getRealPath();
- }
- /**
- * 返回错误信息
- * @return mixed
- */
- public function getError(): string
- {
- return $this->error;
- }
- /**
- * 生成保存的文件信息
- * @return array
- */
- public function getSaveFileInfo(): array
- {
- // 自动生成的文件名称
- // $hashName = $this->file->hashName(null);
- $hashName = $this->hashName();
- // 存储目录
- $filePath = $this->getFilePath($hashName);
- // 文件名称
- // 去除扩展名的写法 stristr($this->file->getOriginalName(), '.', true)
- $fileName = $this->file->getOriginalName();
- // 文件扩展名
- $fileExt = strtolower($this->file->extension());
- return [
- 'storage' => $this->storage, // 存储方式
- 'domain' => $this->config['domain'] ?? '', // 存储域名
- 'file_path' => $filePath, // 文件路径
- 'file_name' => $fileName, // 文件名称
- 'file_size' => $this->file->getSize(), // 文件大小(字节)
- 'file_ext' => $fileExt, // 文件扩展名
- ];
- }
- /**
- * 自动生成文件名
- * @return string
- */
- private function hashName()
- {
- return $this->file->hashName(function () {
- return date('Ymd') . DIRECTORY_SEPARATOR . md5(uniqid((string)mt_rand(), true));
- });
- }
- /**
- * 获取hashName的路径
- * @param string $hashName
- * @return string
- */
- private function getFilePath(string $hashName): string
- {
- $filePath = empty($this->rootDirName) ? "{$hashName}" : "{$this->rootDirName}/{$hashName}";
- return convert_left_slash($filePath);
- }
- /**
- * 获取hashName的文件名
- * @param string $filePath
- * @return string
- */
- protected function getFileHashName(string $filePath): string
- {
- return basename($filePath);
- }
- /**
- * 获取hashName的文件目录
- * @param string $filePath
- * @return string
- */
- protected function getFileHashRoute(string $filePath): string
- {
- return dirname($filePath);
- }
- }
|