mirror of
https://github.com/crmeb/CRMEB.git
synced 2026-10-30 06:20:50 +00:00
Merge branch 'v5.1.0dev' of https://gitee.com/ZhongBangKeJi/CRMEB into v5.1.0dev
This commit is contained in:
commit
30ccff71ac
@ -20,6 +20,7 @@ use think\facade\Config;
|
||||
* @mixin \crmeb\services\upload\storage\OSS
|
||||
* @mixin \crmeb\services\upload\storage\COS
|
||||
* @mixin \crmeb\services\upload\storage\Qiniu
|
||||
* @mixin \crmeb\services\upload\storage\Jdoss
|
||||
*/
|
||||
class Upload extends BaseManager
|
||||
{
|
||||
|
||||
@ -2,9 +2,12 @@
|
||||
|
||||
namespace crmeb\services\upload\storage;
|
||||
|
||||
use Aws\Acm\Exception\AcmException;
|
||||
use Aws\S3\S3Client;
|
||||
use crmeb\exceptions\AdminException;
|
||||
use crmeb\exceptions\UploadException;
|
||||
use crmeb\services\upload\BaseUpload;
|
||||
use Guzzle\Http\EntityBody;
|
||||
|
||||
/**
|
||||
* 京东云COS文件上传
|
||||
@ -121,17 +124,82 @@ class Jdoss extends BaseUpload
|
||||
|
||||
public function move(string $file = 'file')
|
||||
{
|
||||
// TODO: Implement move() method.
|
||||
$fileHandle = app()->request->file($file);
|
||||
if (!$fileHandle) {
|
||||
return $this->setError('上传的文件不存在');
|
||||
}
|
||||
if ($this->validate) {
|
||||
if (!in_array(pathinfo($fileHandle->getOriginalName(), PATHINFO_EXTENSION), $this->validate['fileExt'])) {
|
||||
return $this->setError('不合法的文件后缀');
|
||||
}
|
||||
if (filesize($fileHandle) > $this->validate['filesize']) {
|
||||
return $this->setError('文件过大');
|
||||
}
|
||||
if (!in_array($fileHandle->getOriginalMime(), $this->validate['fileMime'])) {
|
||||
return $this->setError('不合法的文件类型');
|
||||
}
|
||||
}
|
||||
$key = $this->saveFileName($fileHandle->getRealPath(), $fileHandle->getOriginalExtension());
|
||||
$key = $this->getUploadPath($key);
|
||||
try {
|
||||
$uploadInfo = $this->app()->putObject([
|
||||
'Bucket' => $this->storageName,
|
||||
'Key' => $key,
|
||||
'SourceFile' => $fileHandle->getRealPath()
|
||||
]);
|
||||
if (!isset($uploadInfo['info']['url'])) {
|
||||
return $this->setError('Upload failure');
|
||||
}
|
||||
$this->fileInfo->uploadInfo = $uploadInfo;
|
||||
$this->fileInfo->realName = $fileHandle->getOriginalName();
|
||||
$this->fileInfo->filePath = ($this->cdn ?: $this->uploadUrl) . '/' . $key;
|
||||
$this->fileInfo->fileName = $key;
|
||||
$this->fileInfo->filePathWater = $this->water($this->fileInfo->filePath);
|
||||
$this->authThumb && $this->thumb($this->fileInfo->filePath);
|
||||
return $this->fileInfo;
|
||||
} catch (UploadException $e) {
|
||||
return $this->setError($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function stream($fileContent, string $key = null)
|
||||
{
|
||||
// TODO: Implement stream() method.
|
||||
try {
|
||||
if (!$key) {
|
||||
$key = $this->saveFileName();
|
||||
}
|
||||
$key = $this->getUploadPath($key);
|
||||
$fileContent = (string)EntityBody::factory($fileContent);
|
||||
$uploadInfo = $this->app()->putObject([
|
||||
'Bucket' => $this->storageName,
|
||||
'Key' => $key,
|
||||
'Body' => $fileContent
|
||||
]);
|
||||
if (!isset($uploadInfo['info']['url'])) {
|
||||
return $this->setError('Upload failure');
|
||||
}
|
||||
$this->fileInfo->uploadInfo = $uploadInfo;
|
||||
$this->fileInfo->realName = $key;
|
||||
$this->fileInfo->filePath = ($this->cdn ?: $this->uploadUrl) . '/' . $key;
|
||||
$this->fileInfo->fileName = $key;
|
||||
$this->fileInfo->filePathWater = $this->water($this->fileInfo->filePath);
|
||||
$this->thumb($this->fileInfo->filePath);
|
||||
return $this->fileInfo;
|
||||
} catch (UploadException $e) {
|
||||
return $this->setError($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function delete(string $filePath)
|
||||
public function delete(string $key)
|
||||
{
|
||||
// TODO: Implement delete() method.
|
||||
try {
|
||||
return $this->app()->deleteObject([
|
||||
'Bucket' => $this->storageName,
|
||||
'Key' => $key
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->setError($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -204,33 +272,190 @@ class Jdoss extends BaseUpload
|
||||
];
|
||||
}
|
||||
|
||||
public function deleteBucket(string $name)
|
||||
public function deleteBucket(string $name, string $region = '')
|
||||
{
|
||||
// TODO: Implement deleteBucket() method.
|
||||
try {
|
||||
$this->storageRegion = $region;
|
||||
$this->app()->deleteBucket([
|
||||
'bucketName' => $name, // REQUIRED
|
||||
'forceDelete' => false
|
||||
]);
|
||||
return true;
|
||||
} catch (AcmException $e) {
|
||||
return $this->setError($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function getDomian(string $name, string $region = null)
|
||||
{
|
||||
try {
|
||||
$this->storageRegion = $region;
|
||||
$res = $this->app()->getBucketWebsite([
|
||||
'Bucket' => $name
|
||||
]);
|
||||
return $res['DomainName'] ?? [];
|
||||
} catch (\Throwable $e) {
|
||||
return $this->setError($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function bindDomian(string $name, string $domain, string $region = null)
|
||||
{
|
||||
// TODO: Implement bindDomian() method.
|
||||
try {
|
||||
$this->storageRegion = $region;
|
||||
$this->app()->putBucketWebsite([
|
||||
'Bucket' => $name,
|
||||
'WebsiteConfiguration' => [
|
||||
'RedirectAllRequestsTo' => [
|
||||
'HostName' => $domain,
|
||||
'Protocol' => 'http'
|
||||
]
|
||||
]
|
||||
]);
|
||||
return true;
|
||||
} catch (\Throwable $e) {
|
||||
return $this->setError($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function setBucketCors(string $name, string $region)
|
||||
{
|
||||
// TODO: Implement setBucketCors() method.
|
||||
$this->storageRegion = $region;
|
||||
try {
|
||||
$this->app()->putBucketCors([
|
||||
'Bucket' => $name, // REQUIRED
|
||||
'CORSConfiguration' => [ // REQUIRED
|
||||
'CORSRules' => [ // REQUIRED
|
||||
[
|
||||
'AllowedHeaders' => ['Authorization'],
|
||||
'AllowedMethods' => ['POST', 'GET', 'PUT'], // REQUIRED
|
||||
'AllowedOrigins' => ['*'], // REQUIRED
|
||||
'ExposeHeaders' => [],
|
||||
'MaxAgeSeconds' => 3000
|
||||
],
|
||||
],
|
||||
]
|
||||
]);
|
||||
return true;
|
||||
} catch (\Throwable $e) {
|
||||
return $this->setError($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function getTempKeys()
|
||||
/**
|
||||
* 获取OSS上传密钥
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function getTempKeys($key = '', $path = '', $expires = '+10 minutes')
|
||||
{
|
||||
// TODO: Implement getTempKeys() method.
|
||||
try {
|
||||
$app = $this->app();
|
||||
$cmd = $app->getCommand([
|
||||
'PutObject', [
|
||||
'Bucket' => $this->storageName,
|
||||
'Key' => $key,
|
||||
'SourceFile' => $path
|
||||
]
|
||||
]);
|
||||
$request = $app->createPresignedRequest($cmd, $expires);
|
||||
return (string)$request->getUri();
|
||||
} catch (\Throwable $e) {
|
||||
return $this->setError($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function thumb(string $filePath = '')
|
||||
/**
|
||||
* 缩略图
|
||||
* @param string $filePath
|
||||
* @param string $fileName
|
||||
* @param string $type
|
||||
* @return array|mixed
|
||||
*/
|
||||
public function thumb(string $filePath = '', string $fileName = '', string $type = 'all')
|
||||
{
|
||||
// TODO: Implement thumb() method.
|
||||
$filePath = $this->getFilePath($filePath);
|
||||
$data = ['big' => $filePath, 'mid' => $filePath, 'small' => $filePath];
|
||||
$this->fileInfo->filePathBig = $this->fileInfo->filePathMid = $this->fileInfo->filePathSmall = $this->fileInfo->filePathWater = $filePath;
|
||||
if ($filePath) {
|
||||
$config = $this->thumbConfig;
|
||||
foreach ($this->thumb as $v) {
|
||||
if ($type == 'all' || $type == $v) {
|
||||
$height = 'thumb_' . $v . '_height';
|
||||
$width = 'thumb_' . $v . '_width';
|
||||
$key = 'filePath' . ucfirst($v);
|
||||
if (sys_config('image_thumbnail_status', 1) && isset($config[$height]) && isset($config[$width]) && $config[$height] && $config[$width]) {
|
||||
$this->fileInfo->$key = $filePath . '?x-oss-process=image/resize,h_' . $config[$height] . ',w_' . $config[$width];
|
||||
$this->fileInfo->$key = $this->water($this->fileInfo->$key);
|
||||
$data[$v] = $this->fileInfo->$key;
|
||||
} else {
|
||||
$this->fileInfo->$key = $this->water($this->fileInfo->$key);
|
||||
$data[$v] = $this->fileInfo->$key;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 水印
|
||||
* @param string $filePath
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function water(string $filePath = '')
|
||||
{
|
||||
// TODO: Implement water() method.
|
||||
$filePath = $this->getFilePath($filePath);
|
||||
$waterConfig = $this->waterConfig;
|
||||
$waterPath = $filePath;
|
||||
if ($waterConfig['image_watermark_status'] && $filePath) {
|
||||
if (strpos($filePath, '?x-oss-process') === false) {
|
||||
$filePath .= '?x-oss-process=image';
|
||||
}
|
||||
switch ($waterConfig['watermark_type']) {
|
||||
case 1://图片
|
||||
if (!$waterConfig['watermark_image']) {
|
||||
throw new AdminException(400722);
|
||||
}
|
||||
$waterPath = $filePath .= '/watermark,image_' . base64_encode($waterConfig['watermark_image']) . ',t_' . $waterConfig['watermark_opacity'] . ',g_' . ($this->position[$waterConfig['watermark_position']] ?? 'nw') . ',x_' . $waterConfig['watermark_x'] . ',y_' . $waterConfig['watermark_y'];
|
||||
break;
|
||||
case 2://文字
|
||||
if (!$waterConfig['watermark_text']) {
|
||||
throw new AdminException(400723);
|
||||
}
|
||||
$waterConfig['watermark_text_color'] = str_replace('#', '', $waterConfig['watermark_text_color']);
|
||||
$waterPath = $filePath .= '/watermark,text_' . base64_encode($waterConfig['watermark_text']) . ',color_' . $waterConfig['watermark_text_color'] . ',size_' . $waterConfig['watermark_text_size'] . ',g_' . ($this->position[$waterConfig['watermark_position']] ?? 'nw') . ',x_' . $waterConfig['watermark_x'] . ',y_' . $waterConfig['watermark_y'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $waterPath;
|
||||
}
|
||||
|
||||
public function setError(?string $error = null)
|
||||
{
|
||||
$this->error = $error ?: '未知错误';
|
||||
if (env('APP_DEBUG')) {
|
||||
throw new UploadException($this->xmlToArr($this->error));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function xmlToArr($errorXml)
|
||||
{
|
||||
|
||||
$pattern = '/<statusCode>(\d+)<\/statusCode><Code>(.*?)<\/Code><Message>(.*?)<\/Message><Resource>(.*?)<\/Resource><RequestId>(.*?)<\/RequestId>/';
|
||||
|
||||
preg_match($pattern, $errorXml, $matches);
|
||||
|
||||
$error = [
|
||||
'statusCode' => $matches[1],
|
||||
'Code' => $matches[2],
|
||||
'Message' => $matches[3],
|
||||
'Resource' => $matches[4],
|
||||
'RequestId' => $matches[5]
|
||||
];
|
||||
|
||||
return $error;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user