修改上传
This commit is contained in:
@@ -2,6 +2,9 @@
|
|||||||
namespace catcher;
|
namespace catcher;
|
||||||
|
|
||||||
use catchAdmin\system\model\Attachments;
|
use catchAdmin\system\model\Attachments;
|
||||||
|
use catcher\exceptions\FailedException;
|
||||||
|
use catcher\exceptions\ValidateFailedException;
|
||||||
|
use think\exception\ValidateException;
|
||||||
use think\facade\Filesystem;
|
use think\facade\Filesystem;
|
||||||
use think\file\UploadedFile;
|
use think\file\UploadedFile;
|
||||||
|
|
||||||
@@ -45,20 +48,50 @@ class CatchUpload
|
|||||||
* upload files
|
* upload files
|
||||||
*
|
*
|
||||||
* @param UploadedFile $file
|
* @param UploadedFile $file
|
||||||
* @return array
|
* @return string
|
||||||
* @author JaguarJack
|
* @author JaguarJack
|
||||||
* @email njphper@gmail.com
|
* @email njphper@gmail.com
|
||||||
* @time 2020/1/25
|
* @time 2020/1/25
|
||||||
*/
|
*/
|
||||||
public function upload(UploadedFile $file): array
|
public function upload(UploadedFile $file): string
|
||||||
{
|
{
|
||||||
$path = Filesystem::disk($this->getDriver())->putFile($this->getPath(), $file);
|
$path = Filesystem::disk($this->getDriver())->putFile($this->getPath(), $file);
|
||||||
|
|
||||||
if ($path) {
|
if ($path) {
|
||||||
Attachments::create(array_merge(['path' => $path], $this->data($file)));
|
$url = Utils::getCloudDomain($this->getDriver()) . $path;
|
||||||
|
|
||||||
|
Attachments::create(array_merge([
|
||||||
|
'path' => $path,
|
||||||
|
'url' => $url,
|
||||||
|
], $this->data($file)));
|
||||||
|
|
||||||
|
return $url;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ['path' => Utils::getCloudDomain($this->getDriver()) . $path];
|
throw new FailedException('Upload Failed, Try Again!');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 多文件上传
|
||||||
|
*
|
||||||
|
* @author JaguarJack
|
||||||
|
* @email njphper@gmail.com
|
||||||
|
* @time 2020/2/1
|
||||||
|
* @param $attachments
|
||||||
|
* @return array|string
|
||||||
|
*/
|
||||||
|
public function multiUpload($attachments)
|
||||||
|
{
|
||||||
|
if (!is_array($attachments)) {
|
||||||
|
return $this->upload($attachments);
|
||||||
|
}
|
||||||
|
|
||||||
|
$paths = [];
|
||||||
|
foreach ($attachments as $attachment) {
|
||||||
|
$paths[] = $this->upload($attachment);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $paths;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -133,9 +166,49 @@ class CatchUpload
|
|||||||
return [
|
return [
|
||||||
'file_size' => $file->getSize(),
|
'file_size' => $file->getSize(),
|
||||||
'mime_type' => $file->getMime(),
|
'mime_type' => $file->getMime(),
|
||||||
'file_ext' => $file->getExtension(),
|
'file_ext' => $file->getOriginalExtension(),
|
||||||
'filename' => $file->getOriginalName(),
|
'filename' => $file->getOriginalName(),
|
||||||
'driver' => $this->getDriver(),
|
'driver' => $this->getDriver(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证图片
|
||||||
|
*
|
||||||
|
* @author JaguarJack
|
||||||
|
* @email njphper@gmail.com
|
||||||
|
* @time 2020/2/1
|
||||||
|
* @param array $images
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function checkImages(array $images)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
validate(['image' => config('catch.upload.image')])->check($images);
|
||||||
|
} catch (ValidateException $e) {
|
||||||
|
throw new ValidateFailedException($e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证文件
|
||||||
|
*
|
||||||
|
* @author JaguarJack
|
||||||
|
* @email njphper@gmail.com
|
||||||
|
* @time 2020/2/1
|
||||||
|
* @param array $files
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function checkFiles(array $files)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
validate(['file' => config('catch.upload.file')])->check($files);
|
||||||
|
} catch (ValidateException $e) {
|
||||||
|
throw new ValidateFailedException($e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user