| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- namespace app\common\service\storage\engine;
- use app\Request;
- use think\Exception;
- use app\common\service\ConfigService;
- use app\common\service\ImageCompressService;
- /**
- * 存储引擎抽象类
- * Class server
- * @package app\common\library\storage\drivers
- */
- abstract class Server
- {
- protected $file;
- protected $error;
- protected $fileName;
- protected $fileInfo;
- // 是否为内部上传
- protected $isInternal = false;
- /**
- * 构造函数
- * Server constructor.
- */
- protected function __construct()
- {
- }
- /**
- * 设置上传的文件信息
- * @param string $name
- * @throws Exception
- */
- public function setUploadFile($name)
- {
- // 接收上传的文件
- $this->file = app('\app\Request')->file($name);
-
- if (empty($this->file)) {
- throw new Exception('未找到上传文件的信息');
- }
- // 文件信息
- $this->fileInfo = [
- 'ext' => $this->file->extension(),
- 'size' => $this->file->getSize(),
- 'mime' => $this->file->getMime(),
- 'name' => $this->file->getOriginalName(),
- 'realPath' => $this->file->getRealPath(),
- ];
- // 如果是图片文件,进行压缩处理
- if ($this->isImageFile()) {
- $compressedPath = $this->compressImage();
- if ($compressedPath) {
- $this->fileInfo['realPath'] = $compressedPath;
- $this->fileInfo['size'] = filesize($compressedPath);
- }
- }
- // 生成保存文件名
- $this->fileName = $this->buildSaveName();
- }
- /**
- * 压缩图片
- */
- protected function compressImage()
- {
- // 获取压缩配置(可以从配置文件读取)
- $quality = ConfigService::get('upload', 'image_quality', 80);
- $maxWidth = ConfigService::get('upload', 'max_width', 1920);
- $maxHeight = ConfigService::get('upload', 'max_height', 1080);
-
- return ImageCompressService::compress(
- $this->fileInfo['realPath'],
- $quality,
- $maxWidth,
- $maxHeight
- );
- }
- /**
- * 判断是否为图片文件
- */
- protected function isImageFile()
- {
- $imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
- return in_array(strtolower($this->fileInfo['ext']), $imageExtensions);
- }
- /**
- * 设置上传的文件信息
- * @param string $filePath
- */
- public function setUploadFileByReal($filePath)
- {
- // 设置为系统内部上传
- $this->isInternal = true;
- // 文件信息
- $this->fileInfo = [
- 'name' => basename($filePath),
- 'size' => filesize($filePath),
- 'tmp_name' => $filePath,
- 'error' => 0,
- ];
- // 生成保存文件名
- $this->fileName = $this->buildSaveName();
- }
- /**
- * Notes: 抓取网络资源
- * @param $url
- * @param $key
- * @author 张无忌(2021/3/2 14:15)
- * @return mixed
- */
- abstract protected function fetch($url, $key);
- /**
- * 文件上传
- * @param $save_dir (保存路径)
- * @return mixed
- */
- abstract protected function upload($save_dir);
- /**
- * 文件删除
- * @param $fileName
- * @return mixed
- */
- abstract protected function delete($fileName);
- /**
- * 返回上传后文件路径
- * @return mixed
- */
- abstract public function getFileName();
- /**
- * 返回文件信息
- * @return mixed
- */
- public function getFileInfo()
- {
- return $this->fileInfo;
- }
- protected function getRealPath()
- {
- return $this->fileInfo['realPath'];
- }
- /**
- * 返回错误信息
- * @return mixed
- */
- public function getError()
- {
- return $this->error;
- }
- /**
- * 生成保存文件名
- */
- private function buildSaveName()
- {
- // 要上传图片的本地路径
- $realPath = $this->getRealPath();
- // 扩展名
- $ext = pathinfo($this->getFileInfo()['name'], PATHINFO_EXTENSION);
- // 自动生成文件名
- return date('YmdHis') . substr(md5($realPath), 0, 5)
- . str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT) . ".{$ext}";
- }
- }
|