109 lines
3.8 KiB
PHP
109 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace admin\models\forms;
|
|
|
|
use common\models\oldDb\Attachment;
|
|
use OSS\OssClient;
|
|
use yii\web\UploadedFile;
|
|
|
|
class AttachmentUpload extends \yii\base\Model
|
|
{
|
|
public $storage;
|
|
public $file;
|
|
public $attachment_group_id;
|
|
public $type;
|
|
public $mall_id;
|
|
public $mch_id;
|
|
|
|
public $saveFileFolder;
|
|
public $saveThumbFolder;
|
|
public $saveFileName;
|
|
public $url;
|
|
public $thumbUrl;
|
|
|
|
protected $docExt = ['txt', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', 'csv', 'pdf', 'md'];
|
|
protected $imageExt = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp',];
|
|
protected $videoExt = ['mp4', 'ogg', 'm4a',];
|
|
|
|
|
|
/**
|
|
* @return Attachment
|
|
* @throws \Exception
|
|
*/
|
|
public function upload()
|
|
{
|
|
$dateFolder = date('Ymd');
|
|
$this->saveFileFolder = '/uploads/' . \Yii::$app->mallId.'/' . $dateFolder;
|
|
$this->saveThumbFolder = '/uploads/thumbs/' . \Yii::$app->mallId.'/' . $dateFolder;
|
|
$this->saveFileName = md5_file($this->file->tempName) . '.' . $this->file->getExtension();
|
|
$this->saveToAliOss();
|
|
return $this->attachmentSave();
|
|
}
|
|
|
|
public function attachmentSave()
|
|
{
|
|
$attachment = new Attachment();
|
|
$attachment->storage_id = $this->storage ? $this->storage->id : 0;
|
|
$attachment->user_id = 0;
|
|
$attachment->name = $this->file->name;
|
|
$attachment->size = $this->file->size;
|
|
$attachment->is_delete = 0;
|
|
$attachment->url = $this->url;
|
|
$attachment->thumb_url = $this->thumbUrl;
|
|
$attachment->attachment_group_id = $this->attachment_group_id;
|
|
$attachment->type = $this->type;
|
|
$attachment->mall_id = $this->mall_id;
|
|
$attachment->mch_id = $this->mch_id;
|
|
if (!$attachment->save()) {
|
|
throw new \Exception(json_encode($attachment->getErrors()));
|
|
}
|
|
return $attachment;
|
|
}
|
|
|
|
public function saveToAliOss()
|
|
{
|
|
$config = \Yii::$app->params['upload']['alioss'];
|
|
$isCName = (isset($config['is_cname']) && $config['is_cname'] == 1) ? true : false;
|
|
$client = new OssClient($config['access_key'], $config['secret_key'], $config['domain'], $isCName);
|
|
|
|
$object = trim($this->saveFileFolder . '/' . $this->saveFileName, '/');
|
|
$client->uploadFile($config['bucket'], $object, $this->file->tempName);
|
|
if (!$isCName) {
|
|
$endpointNameStart = mb_stripos($config['domain'], '://') + 3;
|
|
$urlPrefix = mb_substr($config['domain'], 0, $endpointNameStart)
|
|
. $config['bucket']
|
|
. '.'
|
|
. mb_substr($config['domain'], $endpointNameStart);
|
|
} else {
|
|
$urlPrefix = $config['domain'];
|
|
}
|
|
$this->url = $urlPrefix . $this->saveFileFolder . '/' . $this->saveFileName;
|
|
if (in_array($this->file->extension, $this->imageExt) && isset($config['style_api']) && $config['style_api']) {
|
|
$this->url = $this->url . $config['style_api'];
|
|
}
|
|
$this->thumbUrl = $this->url;
|
|
}
|
|
|
|
public static function getInstanceFromFile($localFilePath)
|
|
{
|
|
if (!is_string($localFilePath)) {
|
|
throw new \Exception('文件名称不是字符串。');
|
|
}
|
|
if (!file_exists($localFilePath)) {
|
|
throw new \Exception('文件`' . $localFilePath . '`不存在。');
|
|
}
|
|
$localFilePath = str_replace('\\', '/', $localFilePath);
|
|
$pathInfo = pathinfo($localFilePath);
|
|
$name = $pathInfo['basename'];
|
|
$size = filesize($localFilePath);
|
|
$type = mimetype_from_filename($localFilePath);
|
|
return new UploadedFile([
|
|
'name' => $name,
|
|
'type' => $type,
|
|
'tempName' => $localFilePath,
|
|
'error' => 0,
|
|
'size' => $size,
|
|
]);
|
|
}
|
|
}
|