修改上传

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

View File

@ -1,6 +1,7 @@
<?php <?php
namespace catcher; namespace catcher;
use catchAdmin\system\model\Attachments;
use think\facade\Filesystem; use think\facade\Filesystem;
use think\file\UploadedFile; use think\file\UploadedFile;
@ -43,15 +44,21 @@ class CatchUpload
/** /**
* upload files * upload files
* *
* @param UploadedFile $file
* @return array
* @author JaguarJack * @author JaguarJack
* @email njphper@gmail.com * @email njphper@gmail.com
* @time 2020/1/25 * @time 2020/1/25
* @param UploadedFile $file
* @return mixed
*/ */
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) {
Attachments::create(array_merge(['path' => $path], $this->data($file)));
}
return ['path' => Utils::getCloudDomain($this->getDriver()) . $path];
} }
/** /**
@ -62,7 +69,7 @@ class CatchUpload
* @time 2020/1/25 * @time 2020/1/25
* @return string * @return string
*/ */
protected function getDriver() protected function getDriver(): string
{ {
return $this->driver; return $this->driver;
} }
@ -77,7 +84,7 @@ class CatchUpload
* @throws \Exception * @throws \Exception
* @return $this * @return $this
*/ */
public function setDriver($driver) public function setDriver($driver): self
{ {
if (!in_array($driver, [self::OSS, self::QCLOUD, self::QIQNIU, self::LOCAL])) { if (!in_array($driver, [self::OSS, self::QCLOUD, self::QIQNIU, self::LOCAL])) {
throw new \Exception(sprintf('Upload Driver [%s] Not Supported', $driver)); throw new \Exception(sprintf('Upload Driver [%s] Not Supported', $driver));
@ -114,4 +121,21 @@ class CatchUpload
return $this; 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(),
];
}
} }