feat: 七牛云上传开发

This commit is contained in:
2025-07-23 17:55:30 +08:00
parent 62a67bf7a0
commit 10c1f86708
2 changed files with 159 additions and 1 deletions

157
extend/uploader/Qiniu.php Normal file
View File

@@ -0,0 +1,157 @@
<?php
namespace uploader;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use Qiniu\Storage\BucketManager;
class QiniuUploader
{
private $bucket = 'orico-opd';
private $accessKey = 'dOsTum4a5qvhPTBbZRPX0pIOU7PZWRX7htKjztms';
private $secretKey = 'KFxsGbnErkALFfeGdMa8QWTdodJbamMX0iznLe-q';
private $rule = [
'fileSize' => 1024 * 1024 * 5, // 默认最大上传5M
'fileExt' => 'jpeg,jpg,png', // 默认上传文件后缀
'fileMime' => 'image/jpeg,image/png,image/gif' // 默认上传文件mime
];
private $dir = true;
private $originalName = false;
private $pathPrefix = '';
private $fileNamePrefix = 'orico';
static public $domain = 'http://opdfile.f2b211.com/';
public function __construct($conf = [])
{
if (!empty($conf['bucket'])) {
$this->bucket = $conf['bucket'];
}
if (!empty($conf['accessKey'])) {
$this->accessKey = $conf['accessKey'];
}
if (!empty($conf['secretKey'])) {
$this->secretKey = $conf['secretKey'];
}
if (!empty($conf['pathPrefix'])) {
$this->pathPrefix = trim($conf['pathPrefix'], '/');
}
}
/**
* 生成随机字符串
*/
private function random($length, $type = "string", $convert = "0")
{
$conf = [
'number' => '0123456789',
'string' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
'all' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789='
];
$string = $conf[$type];
if (!$string) {
$string = $conf['string'];
}
$strlen = strlen($string) - 1;
$char = '';
for ($i = 0; $i < $length; $i++) {
$char .= $string[mt_rand(0, $strlen)];
}
if ($convert > 0) {
$res = strtoupper($char);
} elseif ($convert == 0) {
$res = $char;
} elseif ($convert < 0) {
$res = strtolower($char);
}
return $res;
}
/**
* 组装文件名
*/
private function buildFileName()
{
return $this->fileNamePrefix . time() . substr(time(), -5) . substr(microtime(), 2, 3) . $this->random(8);
}
/**
* 上传验证规则
*/
public function validate($rule)
{
$this->rule = $rule;
}
/**
* 上传文件到七牛云
*/
public function uploadFile($name)
{
// 构建鉴权对象
$auth = new Auth($this->accessKey, $this->secretKey);
// 生成上传 Token
$token = $auth->uploadToken($this->bucket);
// 初始化 UploadManager 对象并进行文件的上传。
$uploadMgr = new UploadManager();
$file = request()->file($name);
$aspectRatio = [];
if (!empty($this->rule['aspectRatio'])) {
$aspectRatio = $this->rule['aspectRatio'];
unset($this->rule['aspectRatio']);
}
$validate = validate([$name => $this->rule]);
if (!$validate->check([$name => $file])) {
throw new \Exception($validate->getError());
}
$fileName = $file->getOriginalName(); // 文件原名
if (!$this->originalName) {
$fileName = $this->buildFileName() . '.' . $file->extension();
if (!$this->dir && !empty($this->pathPrefix)) {
$fileName = $this->pathPrefix . '/' . $fileName;
}
}
if ($this->dir) {
$fileName = date('Y') . '/' . date('m') . '/' . date('d') . '/' . $fileName;
if (!empty($this->pathPrefix)) {
$fileName = $this->pathPrefix . '/' . $fileName;
}
}
$filePath = $file->getPathname(); // 临时路径
if (!empty($aspectRatio)) { // 验证图片宽高
list($width, $height, $type, $attr) = getimagesize($file);
if ($width != $aspectRatio['width'] || $height != $aspectRatio['height']) {
throw new \Exception('图片宽高不符合');
}
}
$fileType = $file->getOriginalMime();
list($ret, $err) = $uploadMgr->putFile($token, $fileName, $filePath, null, $fileType, false);
if ($err !== null) {
throw new \Exception($err);
} else {
return ['hash' => $ret['hash'], 'filename' => $ret['key'], 'remote_url' => self::$domain . $ret['key']];
}
}
/**
* 上传文件到七牛云
*/
public function deleteFile($name)
{
// 构建鉴权对象
$auth = new Auth($this->accessKey, $this->secretKey);
// 初始化 BucketManager 对象并进行文件的删除。
$bucketManager = new BucketManager($auth);
$ret = $bucketManager->delete($this->bucket, $name);
return $ret;
}
}