예제 #1
0
 /**
  * 上传操作
  */
 public function upload()
 {
     $uploaded = 0;
     while ($uploaded < $this->size) {
         $blockSize = $this->blockSize($uploaded);
         $data = fread($this->inputStream, $blockSize);
         if ($data === false) {
             fclose($this->inputStream);
             throw new \Exception("file read failed", 1);
         }
         $crc = Qiniu\crc32_data($data);
         $response = $this->makeBlock($data, $blockSize);
         $ret = null;
         if ($response->ok() && $response->json() != null) {
             $ret = $response->json();
         }
         if ($response->statusCode < 0) {
             $this->host = $this->config->getUpHostBackup();
         }
         if ($response->needRetry() || !isset($ret['crc32']) || $crc != $ret['crc32']) {
             $response = $this->makeBlock($data, $blockSize);
             $ret = $response->json();
         }
         if (!$response->ok() || !isset($ret['crc32']) || $crc != $ret['crc32']) {
             fclose($this->inputStream);
             return array(null, new Error($this->currentUrl, $response));
         }
         array_push($this->contexts, $ret['ctx']);
         $uploaded += $blockSize;
     }
     fclose($this->inputStream);
     return $this->makeFile();
 }
예제 #2
0
 /**
  * 上传二进制流到七牛, 内部使用
  *
  * @param $upToken    上传凭证
  * @param $key        上传文件名
  * @param $data       上传二进制流
  * @param $params     自定义变量,规格参考
  *                    http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
  * @param $mime       上传数据的mimeType
  * @param $checkCrc   是否校验crc32
  *
  * @return array    包含已上传文件的信息,类似:
  *                                              [
  *                                                  "hash" => "<Hash string>",
  *                                                  "key" => "<Key string>"
  *                                              ]
  */
 public static function put($upToken, $key, $data, $config, $params, $mime, $checkCrc)
 {
     $fields = array('token' => $upToken);
     if ($key === null) {
         $fname = 'filename';
     } else {
         $fname = $key;
         $fields['key'] = $key;
     }
     if ($checkCrc) {
         $fields['crc32'] = Qiniu\crc32_data($data);
     }
     if ($params) {
         foreach ($params as $k => $v) {
             $fields[$k] = $v;
         }
     }
     $response = Client::multipartPost($config->getUpHost(), $fields, 'file', $fname, $data, $mime);
     if (!$response->ok()) {
         return array(null, new Error($config->getUpHost(), $response));
     }
     return array($response->json(), null);
 }