作者 Karson

上传又拍云时可扩展参数

@@ -117,26 +117,13 @@ class Backend extends Controller @@ -117,26 +117,13 @@ class Backend extends Controller
117 $this->view->engine->layout('layout/' . $this->layout); 117 $this->view->engine->layout('layout/' . $this->layout);
118 } 118 }
119 119
120 - // 上传参数配置配置  
121 - $uploadcfg = Configvalue::upload();  
122 -  
123 - $upload = [  
124 - 'uploadurl' => $uploadcfg['uploadurl'],  
125 - 'cdnurl' => $uploadcfg['cdnurl'],  
126 - 'multipart' => [  
127 - 'policy' => $uploadcfg['policy'],  
128 - 'signature' => $uploadcfg['signature']  
129 - ],  
130 - 'maxsize' => $uploadcfg['maxsize'],  
131 - 'mimetype' => $uploadcfg['mimetype'],  
132 - ];  
133 - 120 + // 语言检测
134 $lang = Lang::detect(); 121 $lang = Lang::detect();
135 122
136 // 配置信息 123 // 配置信息
137 $config = [ 124 $config = [
138 'site' => Config::get("site"), 125 'site' => Config::get("site"),
139 - 'upload' => $upload, 126 + 'upload' => Configvalue::upload(),
140 'modulename' => $modulename, 127 'modulename' => $modulename,
141 'controllername' => $controllername, 128 'controllername' => $controllername,
142 'actionname' => $actionname, 129 'actionname' => $actionname,
@@ -21,34 +21,53 @@ class Configvalue extends Model @@ -21,34 +21,53 @@ class Configvalue extends Model
21 /** 21 /**
22 * 加载上传配置 22 * 加载上传配置
23 * 23 *
24 - * @param string $savekey 保存路径 例:/{year}/{mon}/{day}/{filemd5}{.suffix}  
25 - * @param mixed $mimetype 上传类型 例:image/*,application/zip  
26 - * @param int $maxsize 上传文件大小 例:10mb 24 + * @param array $params 扩展参数,常用字段savekey,mimetype,maxsize,ext-param,notify-url,return-url<br>
  25 + * 更多字段可参考http://docs.upyun.com/api/form_api/#_2
27 * 26 *
28 * @return array 27 * @return array
29 */ 28 */
30 - public static function upload($savekey = '', $mimetype = '', $maxsize = '') 29 + public static function upload($params = [])
31 { 30 {
32 $uploadcfg = Config::get('upload'); 31 $uploadcfg = Config::get('upload');
33 $uploadcfg = $uploadcfg ? $uploadcfg : []; 32 $uploadcfg = $uploadcfg ? $uploadcfg : [];
34 - $bucket = isset($uploadcfg['bucket']) ? $uploadcfg['bucket'] : '';  
35 - $savekey = $savekey ? $savekey : (isset($uploadcfg['savekey']) ? $uploadcfg['savekey'] : '');  
36 - $expiration = time() + (isset($uploadcfg['expire']) ? $uploadcfg['expire'] : 0);  
37 - $options = [  
38 - 'bucket' => $bucket,  
39 - 'save-key' => $savekey,  
40 - 'expiration' => $expiration 33 + $uploadcfg = array_merge($uploadcfg, $params);
  34 + $uploadcfg['bucket'] = isset($uploadcfg['bucket']) ? $uploadcfg['bucket'] : '';
  35 + $savekey = isset($uploadcfg['savekey']) ? $uploadcfg['savekey'] : '';
  36 + $uploadcfg['save-key'] = isset($uploadcfg['save-key']) ? $uploadcfg['save-key'] : $savekey;
  37 + $expiration = time() + (isset($uploadcfg['expire']) ? $uploadcfg['expire'] : 600);
  38 + $uploadcfg['expiration'] = isset($uploadcfg['expiration']) ? $uploadcfg['expiration'] : $expiration;
  39 + $notifyurl = isset($uploadcfg['notifyurl']) ? $uploadcfg['notifyurl'] : '';
  40 + $returnurl = isset($uploadcfg['returnurl']) ? $uploadcfg['returnurl'] : '';
  41 + if ($notifyurl)
  42 + $uploadcfg['notify-url'] = $notifyurl;
  43 + else
  44 + unset($uploadcfg['notify-url']);
  45 + if ($returnurl)
  46 + $uploadcfg['return-url'] = $returnurl;
  47 + else
  48 + unset($uploadcfg['return-url']);
  49 +
  50 + //设置允许的附加字段
  51 + $allowfields = [
  52 + 'bucket', 'save-key', 'expiration', 'date', 'content-md5', 'notify-url', 'return-url', 'content-secret', 'content-type', 'allow-file-type', 'content-length-range',
  53 + 'image-width-range', 'image-height-range', 'x-gmkerl-thumb', 'x-gmkerl-type', 'apps', 'b64encoded', 'ext-param'
41 ]; 54 ];
42 - $policy = base64_encode(json_encode($options)); 55 + $params = array_intersect_key($uploadcfg, array_flip($allowfields));
  56 + $policy = base64_encode(json_encode($params));
43 $signature = md5($policy . '&' . (isset($uploadcfg['formkey']) ? $uploadcfg['formkey'] : '')); 57 $signature = md5($policy . '&' . (isset($uploadcfg['formkey']) ? $uploadcfg['formkey'] : ''));
  58 + $multipart = [
  59 + 'policy' => $policy,
  60 + 'signature' => $signature,
  61 + ];
  62 +
  63 + $multipart = array_merge($multipart, $params);
44 return [ 64 return [
45 'cdnurl' => isset($uploadcfg['cdnurl']) ? $uploadcfg['cdnurl'] : '', 65 'cdnurl' => isset($uploadcfg['cdnurl']) ? $uploadcfg['cdnurl'] : '',
46 'uploadurl' => isset($uploadcfg['uploadurl']) ? $uploadcfg['uploadurl'] : url('ajax/upload'), 66 'uploadurl' => isset($uploadcfg['uploadurl']) ? $uploadcfg['uploadurl'] : url('ajax/upload'),
47 - 'bucket' => isset($uploadcfg['bucket']) ? $uploadcfg['bucket'] : '',  
48 - 'maxsize' => $maxsize ? $maxsize : (isset($uploadcfg['maxsize']) ? $uploadcfg['maxsize'] : ''),  
49 - 'mimetype' => $mimetype ? $mimetype : (isset($uploadcfg['mimetype']) ? $uploadcfg['mimetype'] : ''),  
50 - 'policy' => $policy,  
51 - 'signature' => $signature, 67 + 'bucket' => $uploadcfg['bucket'],
  68 + 'maxsize' => isset($uploadcfg['maxsize']) ? $uploadcfg['maxsize'] : '',
  69 + 'mimetype' => isset($uploadcfg['mimetype']) ? $uploadcfg['mimetype'] : '',
  70 + 'multipart' => $multipart,
52 ]; 71 ];
53 } 72 }
54 73