예제 #1
0
파일: Base.php 프로젝트: ZhuJingfa/HuiLib
 /**
  * 生成一个头像储存路径
  * 
  * 需要将size替换为相应规格号
  */
 protected function getAvatarPath($meta)
 {
     if (empty($meta['size']) || empty($meta['type']) || empty($meta['name']) || empty($meta['uid'])) {
         throw new Exception(Front::getInstance()->getHuiLang()->_('cdn.upload.files.error'));
     }
     $uid = $meta['uid'];
     $config = $this->getConfig();
     $type = Param::post('type', Param::TYPE_STRING);
     //替换为10位,刚好10亿 1000,000,000
     $uidStr = sprintf("%010d", $uid);
     $filePath = $config['save_path'] . $type . SEP . substr($uidStr, 0, 4) . SEP . substr($uidStr, 4, 3) . SEP . substr($uidStr, 7, 3) . SEP;
     //创建目录
     if (!is_dir($filePath)) {
         mkdir($filePath, 0777, TRUE);
     }
     $ext = $this->getExt($meta['name']);
     $file = $filePath . '{size}' . $ext;
     $result = array();
     $result['file'] = $file;
     $result['url'] = str_ireplace(SEP, URL_SEP, str_ireplace($config['save_path'], '', $file));
     return $result;
 }
예제 #2
0
 protected function preCheck()
 {
     try {
         $huiLang = Front::getInstance()->getHuiLang();
         $appId = Param::post('app_id', Param::TYPE_STRING);
         if (!$appId) {
             return $this->format(self::API_FAIL, $huiLang->_('cdn.upload.app_id.null'));
         }
         if (empty($_FILES)) {
             return $this->format(self::API_FAIL, $huiLang->_('cdn.upload.files.empty'));
         }
         $secret = $this->getAppSecret($appId);
         $post = $this->remapPostArray($_POST);
         //字符安全解密
         if (!Utility::decrypt($post, $secret)) {
             return $this->format(self::API_FAIL, $huiLang->_('cdn.upload.decode.failed'));
         }
         $config = $this->getConfig();
         //上传文件校验处理 一个有错,全部终止
         foreach ($_FILES as $key => $file) {
             $meta = Param::post($key, Param::TYPE_ARRAY);
             if (empty($meta['size']) || empty($meta['type']) || empty($meta['name']) || empty($meta['sha1'])) {
                 return $this->format(self::API_FAIL, $huiLang->_('cdn.upload.files.error'));
             }
             if ($file['error'] || $file['size'] != $meta['size'] || sha1_file($file['tmp_name']) != $meta['sha1']) {
                 return $this->format(self::API_FAIL, $huiLang->_('cdn.upload.files.finger.print.error'));
             }
             if (!empty($config['max_filesize']) && $file['size'] > $config['max_filesize'] * 1024 * 1024) {
                 return $this->format(self::API_FAIL, $huiLang->_('cdn.upload.files.maxsize.error', $config['max_filesize']));
             }
         }
         return $this->format(self::API_SUCCESS, 'ok');
     } catch (Exception $e) {
         return $this->format(self::API_FAIL, $e->getMessage());
     }
 }