修改上传

This commit is contained in:
wuyanwen 2020-01-25 22:22:56 +08:00
parent 7dd40dc4aa
commit 04b491621e

View File

@ -1,117 +1,141 @@
<?php <?php
namespace catcher; namespace catcher;
use think\facade\Filesystem; use catchAdmin\system\model\Attachments;
use think\file\UploadedFile; use think\facade\Filesystem;
use think\file\UploadedFile;
class CatchUpload
{ class CatchUpload
/** {
* 阿里云 /**
*/ * 阿里云
public const OSS = 'oss'; */
public const OSS = 'oss';
/**
* 腾讯云 /**
*/ * 腾讯云
public const QCLOUD = 'qcloud'; */
public const QCLOUD = 'qcloud';
/**
* 七牛 /**
*/ * 七牛
public const QIQNIU = 'qiniu'; */
public const QIQNIU = 'qiniu';
/**
* 本地 /**
* * 本地
* @var string *
*/ * @var string
protected $driver = 'local'; */
protected $driver = 'local';
/**
* 本地 /**
*/ * 本地
public const LOCAL = 'local'; */
public const LOCAL = 'local';
/**
* path /**
* * path
* @var string *
*/ * @var string
protected $path = ''; */
protected $path = '';
/**
* upload files /**
* * upload files
* @author JaguarJack *
* @email njphper@gmail.com * @param UploadedFile $file
* @time 2020/1/25 * @return array
* @param UploadedFile $file * @author JaguarJack
* @return mixed * @email njphper@gmail.com
*/ * @time 2020/1/25
public function upload(UploadedFile $file) */
{ public function upload(UploadedFile $file): array
return Filesystem::disk($this->getDriver())->putFile($this->getPath(), $file); {
} $path = Filesystem::disk($this->getDriver())->putFile($this->getPath(), $file);
/** if ($path) {
* get upload driver Attachments::create(array_merge(['path' => $path], $this->data($file)));
* }
* @author JaguarJack
* @email njphper@gmail.com return ['path' => Utils::getCloudDomain($this->getDriver()) . $path];
* @time 2020/1/25 }
* @return string
*/ /**
protected function getDriver() * get upload driver
{ *
return $this->driver; * @author JaguarJack
} * @email njphper@gmail.com
* @time 2020/1/25
/** * @return string
* set driver */
* protected function getDriver(): string
* @author JaguarJack {
* @email njphper@gmail.com return $this->driver;
* @time 2020/1/25 }
* @param $driver
* @throws \Exception /**
* @return $this * set driver
*/ *
public function setDriver($driver) * @author JaguarJack
{ * @email njphper@gmail.com
if (!in_array($driver, [self::OSS, self::QCLOUD, self::QIQNIU, self::LOCAL])) { * @time 2020/1/25
throw new \Exception(sprintf('Upload Driver [%s] Not Supported', $driver)); * @param $driver
} * @throws \Exception
* @return $this
$this->driver = $driver; */
public function setDriver($driver): self
return $this; {
} if (!in_array($driver, [self::OSS, self::QCLOUD, self::QIQNIU, self::LOCAL])) {
throw new \Exception(sprintf('Upload Driver [%s] Not Supported', $driver));
/** }
*
* @author JaguarJack $this->driver = $driver;
* @email njphper@gmail.com
* @time 2020/1/25 return $this;
* @return string }
*/
protected function getPath() /**
{ *
return $this->path; * @author JaguarJack
} * @email njphper@gmail.com
* @time 2020/1/25
/** * @return string
* */
* @author JaguarJack protected function getPath()
* @email njphper@gmail.com {
* @time 2020/1/25 return $this->path;
* @param string $path }
* @return $this
*/ /**
public function setPath(string $path) *
{ * @author JaguarJack
$this->path = $path; * @email njphper@gmail.com
* @time 2020/1/25
return $this; * @param string $path
} * @return $this
} */
public function setPath(string $path)
{
$this->path = $path;
return $this;
}
/**
*
* @time 2020年01月25日
* @param UploadedFile $file
* @return array
*/
protected function data(UploadedFile $file)
{
return [
'file_size' => $file->getSize(),
'mime_type' => $file->getMime(),
'file_ext' => $file->getExtension(),
'filename' => $file->getOriginalName(),
'driver' => $this->getDriver(),
];
}
}