Ejemplo n.º 1
0
function file_writeBAE($file, $data) {
	global $_W;
	$file = str_replace(IA_ROOT.'/', '', $file);
	$file = $file[0] == '/' ? $file : '/'.$file;
	$pathinfo = pathinfo($file);
	$baiduBCS = new BaiduBCS($_W['config']['bae']['ak'], $_W['config']['bae']['sk']);
	$response = $baiduBCS->create_object_by_content($_W['config']['bae']['bucket'], $file, $data, array('acl' => BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_READ));
	if ($response->isOK()) {
		$baiduBCS->set_object_meta($_W['config']['bae']['bucket'], $file, array("Content-Type" => BCS_MimeTypes::get_mimetype($pathinfo['extension'])));
		$result['success'] = true;
	}
}
Ejemplo n.º 2
0
 /**
  * 将消息发往Baidu BCS.
  * @param array $opt
  * @return BCS_ResponseCore
  */
 private function authenticate($opt)
 {
     //set common param into opt
     $opt[self::AK] = $this->ak;
     $opt[self::SK] = $this->sk;
     // Validate the S3 bucket name, only list_bucket didnot need validate_bucket
     if (!('/' == $opt[self::OBJECT] && '' == $opt[self::BUCKET] && 'GET' == $opt[self::METHOD] && !isset($opt[self::QUERY_STRING][self::ACL])) && !self::validate_bucket($opt[self::BUCKET])) {
         throw new BCS_Exception($opt[self::BUCKET] . 'is not valid, please check!');
     }
     //Validate object
     if (isset($opt[self::OBJECT]) && !self::validate_object($opt[self::OBJECT])) {
         throw new BCS_Exception("Invalid object param[" . $opt[self::OBJECT] . "], please check.", -1);
     }
     //construct url
     $url = $this->format_url($opt);
     if ($url === false) {
         throw new BCS_Exception('Can not format url, please check your param!', -1);
     }
     $opt['url'] = $url;
     $this->log("[method:" . $opt[self::METHOD] . "][url:{$url}]", $opt);
     //build request
     $request = new BCS_RequestCore($opt['url']);
     $headers = array('Content-Type' => 'application/x-www-form-urlencoded');
     $request->set_method($opt[self::METHOD]);
     //Write get_object content to fileWriteTo
     if (isset($opt['fileWriteTo'])) {
         $request->set_write_file($opt['fileWriteTo']);
     }
     // Merge the HTTP headers
     if (isset($opt[self::HEADERS])) {
         $headers = array_merge($headers, $opt[self::HEADERS]);
     }
     // Set content to Http-Body
     if (isset($opt['content'])) {
         $request->set_body($opt['content']);
     }
     // Upload file
     if (isset($opt['fileUpload'])) {
         if (!file_exists($opt['fileUpload'])) {
             throw new BCS_Exception('File[' . $opt['fileUpload'] . '] not found!', -1);
         }
         $request->set_read_file($opt['fileUpload']);
         // Determine the length to read from the file
         $length = $request->read_stream_size;
         // The file size by default
         $file_size = $length;
         if (isset($opt["length"])) {
             if ($opt["length"] > $file_size) {
                 throw new BCS_Exception("Input opt[length] invalid! It can not bigger than file-size", -1);
             }
             $length = $opt['length'];
         }
         if (isset($opt['seekTo']) && !isset($opt["length"])) {
             // Read from seekTo until EOF by default, when set seekTo but not set $opt["length"]
             $length -= (int) $opt['seekTo'];
         }
         $request->set_read_stream_size($length);
         // Attempt to guess the correct mime-type
         if ($headers['Content-Type'] === 'application/x-www-form-urlencoded') {
             $extension = explode('.', $opt['fileUpload']);
             $extension = array_pop($extension);
             $mime_type = BCS_MimeTypes::get_mimetype($extension);
             $headers['Content-Type'] = $mime_type;
         }
         $headers['Content-MD5'] = '';
     }
     // Handle streaming file offsets
     if (isset($opt['seekTo'])) {
         // Pass the seek position to BCS_RequestCore
         $request->set_seek_position((int) $opt['seekTo']);
     }
     // Add headers to request and compute the string to sign
     foreach ($headers as $header_key => $header_value) {
         // Strip linebreaks from header values as they're illegal and can allow for security issues
         $header_value = str_replace(array("\r", "\n"), '', $header_value);
         // Add the header if it has a value
         if ($header_value !== '') {
             $request->add_header($header_key, $header_value);
         }
     }
     // Set the curl options.
     if (isset($opt['curlopts']) && count($opt['curlopts'])) {
         $request->set_curlopts($opt['curlopts']);
     }
     $request->send_request();
     require_once dirname(__FILE__) . "/requestcore.class.php";
     return new BCS_ResponseCore($request->get_response_header(), $request->get_response_body(), $request->get_response_code());
 }
Ejemplo n.º 3
0
function set_object_meta($baidu_bcs)
{
    global $bucket, $object;
    $meta = array("Content-Type" => BCS_MimeTypes::get_mimetype("pdf"));
    $response = $baidu_bcs->set_object_meta($bucket, $object, $meta);
    printResponse($response);
}
Ejemplo n.º 4
0
 public function set_object_meta($object)
 {
     $meta = array("Content-Type" => BCS_MimeTypes::get_mimetype("pdf"));
     $response = $this->baidu_bcs->set_object_meta($this->bucket, $object, $meta);
     return $response;
 }
Ejemplo n.º 5
0
 /**
  * 修改文件处理函数
  *
  * @access public
  * @param array $content 老文件
  * @param array $file 新上传的文件
  * @return mixed
  */
 public static function modifyHandle($content, $file)
 {
     if (empty($file['name'])) {
         return false;
     }
     $fileName = preg_split("(\\/|\\|:)", $file['name']);
     $file['name'] = array_pop($fileName);
     //获取扩展名
     $ext = '';
     $part = explode('.', $file['name']);
     if (($length = count($part)) > 1) {
         $ext = strtolower($part[$length - 1]);
     }
     if ($content['attachment']->type != $ext) {
         return false;
     }
     //获取文件名
     $fileName = $content['attachment']->path;
     $path = $fileName;
     $bcs = self::bcsInit();
     $options = Typecho_Widget::widget('Widget_Options');
     $bucket = $options->plugin('BaeUpload')->bucket;
     //空日志记录函数
     function bcs_log()
     {
     }
     if (isset($file['tmp_name'])) {
         //移动上传文件
         if (!$bcs->create_object($bucket, $path, $file['tmp_name'], array('acl' => BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_READ, BaiduBCS::IMPORT_BCS_LOG_METHOD => 'bcs_log'))->isOK()) {
             return false;
         }
     } else {
         if (isset($file['bits'])) {
             //直接写入文件
             if (!$bcs->create_object_by_content($bucket, $path, $file['bits'], array('acl' => BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_READ, BaiduBCS::IMPORT_BCS_LOG_METHOD => 'bcs_log'))->isOK()) {
                 return false;
             }
         } else {
             return false;
         }
     }
     //设置文件Content-Type
     $bcs->set_object_meta($bucket, $path, array('Content-Type' => BCS_MimeTypes::get_mimetype($ext)), array(BaiduBCS::IMPORT_BCS_LOG_METHOD => 'bcs_log'));
     if (!isset($file['size'])) {
         $file['size'] = $bcs->get_object_info($bucket, $path, array(BaiduBCS::IMPORT_BCS_LOG_METHOD => 'bcs_log'))->header['Content-Length'];
     }
     //返回相对存储路径
     return array('name' => $content['attachment']->name, 'path' => $content['attachment']->path, 'size' => $file['size'], 'type' => $content['attachment']->type, 'mime' => $content['attachment']->mime);
 }