Files
zyt/server/app/common/service/storage/engine/Server.php
T
2026-08-20 17:47:14 +08:00

163 lines
4.2 KiB
PHP
Executable File

<?php
namespace app\common\service\storage\engine;
use think\Request;
use think\Exception;
use think\File;
/**
* 存储引擎抽象类
* 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 = request()->file($name);
if (empty($this->file)) {
throw new Exception('未找到上传文件的信息');
}
// 校验上传文件后缀
$limit = array_merge(config('project.file_image'), config('project.file_video'), config('project.file_file'));
if (!in_array(strtolower($this->file->extension()), $limit)) {
throw new Exception('不允许上传' . $this->file->extension() . '后缀文件');
}
// 文件信息
$this->fileInfo = [
'ext' => $this->file->extension(),
'size' => $this->file->getSize(),
'mime' => $this->file->getMime(),
'name' => $this->file->getOriginalName(),
'realPath' => $this->file->getRealPath(),
];
// 生成保存文件名
$this->fileName = $this->buildSaveName();
}
/**
* 设置上传的文件信息
* @param string $filePath
*/
public function setUploadFileByReal($filePath)
{
// 设置为系统内部上传
$this->isInternal = true;
// 内部上传的是磁盘文件,不是 HTTP UploadedFile。这里统一成与
// 普通上传相同的存储契约:云存储需要 realPath,本地存储需要 File 对象。
$this->file = new File($filePath);
$realPath = $this->file->getRealPath();
if ($realPath === false || !$this->file->isReadable()) {
throw new Exception('内部上传文件不可读取');
}
// 文件信息
$this->fileInfo = [
'ext' => strtolower($this->file->getExtension()),
'size' => $this->file->getSize(),
'mime' => $this->file->getMime(),
'name' => basename($filePath),
'realPath' => $realPath,
'tmp_name' => $realPath,
'error' => UPLOAD_ERR_OK,
];
// 生成保存文件名
$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()
{
$realPath = $this->fileInfo['realPath'] ?? '';
if (!is_string($realPath) || $realPath === '' || !is_file($realPath)) {
throw new Exception('上传文件真实路径无效');
}
return $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}";
}
}