Files
orico-official-website/extend/filesystem/Qiniu.php
2025-07-24 18:03:41 +08:00

166 lines
5.1 KiB
PHP

<?php
namespace filesystem;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use Qiniu\Storage\BucketManager;
class Qiniu
{
private $bucket = 'orico-official-website';
private $access_key = 'dOsTum4a5qvhPTBbZRPX0pIOU7PZWRX7htKjztms';
private $secret_key = 'KFxsGbnErkALFfeGdMa8QWTdodJbamMX0iznLe-q';
private $rule = [
'fileSize' => 1024 * 1024 * 50, // 默认最大上传5M
'fileExt' => 'jpeg,jpg,png,mp4', // 默认上传文件后缀
'fileMime' => 'image/jpeg,image/png,image/gif,video/mp4' // 默认上传文件mime
];
private $dir = true;
private $path_prefix = '';
private $file_name_prefix = 'orico';
private $keep_original_name = false;
private $base_url = '//szw73dlk3.hn-bkt.clouddn.com';
public function __construct($conf = [])
{
if (!empty($conf['base_url'])) {
$this->base_url = $conf['base_url'];
}
if (!empty($conf['bucket'])) {
$this->bucket = $conf['bucket'];
}
if (!empty($conf['access_key'])) {
$this->access_key = $conf['access_key'];
}
if (!empty($conf['secret_key'])) {
$this->secret_key = $conf['secret_key'];
}
if (!empty($conf['path_prefix'])) {
$this->path_prefix = trim($conf['path_prefix'], '/');
}
if (!empty($conf['file_name_prefix'])) {
$this->file_name_prefix = $conf['file_name_prefix'];
}
if (!empty($conf['keep_original_name'])) {
$this->keep_original_name = $conf['keep_original_name'];
}
}
/**
* 生成随机字符串
*/
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->file_name_prefix . 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->access_key, $this->secret_key);
// 生成上传 Token
$token = $auth->uploadToken($this->bucket);
// 初始化 UploadManager 对象并进行文件的上传。
$upload_mgr = new UploadManager();
$file = request()->file($name);
$aspect_ratio = [];
if (!empty($this->rule['aspectRatio'])) {
$aspect_ratio = $this->rule['aspectRatio'];
unset($this->rule['aspectRatio']);
}
$validate = validate([$name => $this->rule]);
if (!$validate->check([$name => $file])) {
throw new \Exception($validate->getError());
}
$file_name = $file->getOriginalName(); // 文件原名
if (!$this->keep_original_name) {
$file_name = $this->buildFileName() . '.' . $file->extension();
if (!$this->dir && !empty($this->path_prefix)) {
$file_name = $this->path_prefix . '/' . $file_name;
}
}
if ($this->dir) {
$file_name = date('Ymd') . '/' . $file_name;
if (!empty($this->path_prefix)) {
$file_name = $this->path_prefix . '/' . $file_name;
}
}
$file_path = $file->getPathname(); // 临时路径
if (!empty($aspect_ratio)) { // 验证图片宽高
list($width, $height, $type, $attr) = getimagesize($file);
if ($width != $aspect_ratio['width'] || $height != $aspect_ratio['height']) {
throw new \Exception('图片宽高不符合');
}
}
$file_type = $file->getOriginalMime();
list($ret, $err) = $upload_mgr->putFile($token, $file_name, $file_path, null, $file_type, false);
if ($err !== null) {
throw new \Exception($err);
} else {
return ['hash' => $ret['hash'], 'filename' => $ret['key'], 'remote_url' => $this->base_url . $ret['key']];
}
}
/**
* 上传文件到七牛云
*/
public function deleteFile($name)
{
// 构建鉴权对象
$auth = new Auth($this->access_key, $this->secret_key);
// 初始化 BucketManager 对象并进行文件的删除。
$bucket_mgr = new BucketManager($auth);
$ret = $bucket_mgr->delete($this->bucket, $name);
return $ret;
}
}