|
@@ -13,14 +13,15 @@ use think\Exception;
|
|
|
*/
|
|
|
class Upload extends Base
|
|
|
{
|
|
|
- protected $id ;
|
|
|
+ protected $id;
|
|
|
protected $key;
|
|
|
protected $host;
|
|
|
|
|
|
- public function initialize(){
|
|
|
- $this->id = sysconf('storage_oss_keyid');
|
|
|
- $this->key = sysconf('storage_oss_secret');
|
|
|
- $this->host = "https://".sysconf('storage_oss_domain');
|
|
|
+ public function initialize()
|
|
|
+ {
|
|
|
+ $this->id = sysconf('storage_oss_keyid');
|
|
|
+ $this->key = sysconf('storage_oss_secret');
|
|
|
+ $this->host = "https://" . sysconf('storage_oss_domain');
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -32,7 +33,9 @@ class Upload extends Base
|
|
|
* @Apidoc\ParamType("formdata")
|
|
|
* @Apidoc\Param("file",type="file", require=true,desc="附件")
|
|
|
* @Apidoc\Returned("url", type="string", desc="上传后的文件路径")
|
|
|
- * @Apidoc\Returned("fullurl", type="string", desc="全路径")
|
|
|
+ * @Apidoc\Returned("fullurl", type="string", desc="文件地址")
|
|
|
+ * @Apidoc\Returned("name", type="string", desc="文件名称")
|
|
|
+ * @Apidoc\Returned("size", type="string", desc="文件大小")
|
|
|
*/
|
|
|
public function upload()
|
|
|
{
|
|
@@ -46,23 +49,29 @@ class Upload extends Base
|
|
|
if ($file->checkExt('php,sh')) {
|
|
|
$this->error('可执行文件禁止上传到本地服务器!');
|
|
|
}
|
|
|
+ $fileinfo = $file->getInfo();
|
|
|
$this->safe = boolval(input('safe'));
|
|
|
$this->uptype = $this->getUploadType();
|
|
|
$this->extend = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
|
|
|
-// $name = File::name($file->getPathname(), $this->extend, '', 'md5_file');
|
|
|
$subPath = date('Ymd');
|
|
|
+ $savePath = $subPath;
|
|
|
$filename = date('YmdHis') . uniqid();
|
|
|
- $suffix = '.'.$this->extend;
|
|
|
- $realpath = $subPath . '/' . $filename.$suffix;
|
|
|
- try{
|
|
|
+ $suffix = '.' . $this->extend;
|
|
|
+ $realpath = $savePath . '/' . $filename . $suffix;
|
|
|
+ try {
|
|
|
$info = File::instance($this->uptype)->save($realpath, file_get_contents($file->getRealPath()), $this->safe);
|
|
|
- } catch (Exception $e){
|
|
|
+ } catch (Exception $e) {
|
|
|
$this->error($e->getMessage());
|
|
|
}
|
|
|
if (is_array($info) && isset($info['url'])) {
|
|
|
$url = $this->safe ? $realpath : $info['url'];
|
|
|
- $data = ['url' => '/'.$info['key'], 'fullurl' => $info['url']];
|
|
|
- $this->success('上传成功',$data);
|
|
|
+ $data = [
|
|
|
+ 'url' => '/' . $info['key'],
|
|
|
+ 'fullurl' => $info['url'],
|
|
|
+ 'name' => $fileinfo['name'],
|
|
|
+ 'size' => $fileinfo['size'],
|
|
|
+ ];
|
|
|
+ $this->success('上传成功', $data);
|
|
|
} else {
|
|
|
$this->error('文件处理失败,请稍候再试!');
|
|
|
}
|
|
@@ -89,21 +98,23 @@ class Upload extends Base
|
|
|
* @Apidoc\Returned("expire", type="string", desc="有效截止时间")
|
|
|
* @Apidoc\Returned("key", type="string", desc="路径(前端生成的目录名称+/)")
|
|
|
*/
|
|
|
- public function getSignedUrl(){
|
|
|
+ public function getSignedUrl()
|
|
|
+ {
|
|
|
|
|
|
- $type=$this->request->get('type','image');
|
|
|
- $data=$this->getPolicy($type.'/');
|
|
|
- $this->success('操作成功',$data);
|
|
|
+ $type = $this->request->get('type', 'image');
|
|
|
+ $data = $this->getPolicy($type . '/');
|
|
|
+ $this->success('操作成功', $data);
|
|
|
}
|
|
|
|
|
|
- public function getPolicy($path,$maxSize=5000000){
|
|
|
+ public function getPolicy($path, $maxSize = 5000000)
|
|
|
+ {
|
|
|
$now = time();
|
|
|
$expire = 600; //设置该policy超时时间是10s. 即这个policy过了这个有效时间,将不能访问
|
|
|
$end = $now + $expire;
|
|
|
$expiration = $this->_gmt_iso8601($end);
|
|
|
$policy = [
|
|
|
- 'expiration'=>$expiration,
|
|
|
- 'conditions'=>[
|
|
|
+ 'expiration' => $expiration,
|
|
|
+ 'conditions' => [
|
|
|
['content-length-range', 1, $maxSize],
|
|
|
['starts-with', '$key', $path],
|
|
|
]
|
|
@@ -124,13 +135,14 @@ class Upload extends Base
|
|
|
return $response;
|
|
|
}
|
|
|
|
|
|
- protected function _gmt_iso8601($time) {
|
|
|
+ protected function _gmt_iso8601($time)
|
|
|
+ {
|
|
|
$dtStr = date("c", $time);
|
|
|
$mydatetime = new \DateTime($dtStr);
|
|
|
$expiration = $mydatetime->format(\DateTime::ISO8601);
|
|
|
$pos = strpos($expiration, '+');
|
|
|
$expiration = substr($expiration, 0, $pos);
|
|
|
- return $expiration."Z";
|
|
|
+ return $expiration . "Z";
|
|
|
}
|
|
|
|
|
|
}
|