<?php
require_once dirname ( __FILE__ ) . '/' . 'baiduRequestCore.class.php';
class BaiduPCS {
private $_pcs_uri_prefixs = array ('https' => 'https://pcs.baidu.com/rest/2.0/pcs/' );
private $_accessToken = '';
public function __construct($accessToken) {
$this->_accessToken = $accessToken;
}
public function setAccessToken($accessToken) {
$this->_accessToken = $accessToken;
return $this;
}
public function getAccessToken() {
return $this->_accessToken;
}
private function _baseControl($apiMethod, $params, $method = 'GET', $headers = array()) {
$method = strtoupper ( $method );
if (is_array ( $params )) {
$params = http_build_query ( $params, '', '&' );
}
$url = $this->_pcs_uri_prefixs ['https'] . $apiMethod . ($method == 'GET' ? '&' . $params : '');
if($headers['returnurl']) return $url;
$baiduRequestCore = new baiduRequestCore ();
$baiduRequestCore->set_request_url ( $url );
$baiduRequestCore->set_method ( $method );
if ($method == 'POST') {
$baiduRequestCore->set_body ( $params );
}
foreach ( $headers as $key => $value ) {
$baiduRequestCore->add_header ( $key, $value );
}
$baiduRequestCore->send_request ();
$result = $baiduRequestCore->get_response_body ();
return $result;
}
public function getQuota() {
$result = $this->_baseControl ( 'quota?method=info' . '&access_token=' . $this->_accessToken, array () );
return $result;
}
public function upload($fileContent, $targetPath, $fileName, $newFileName = null, $isCreateSuperFile = FALSE,$ondup = 'newcopy') {
$boundary = md5 ( time () );
$postContent .= "--" . $boundary . "\r\n";
$postContent .= "Content-Disposition: form-data; name=\"file\"; filename=\"{$fileName}\"\r\n";
$postContent .= "Content-Type: application/octet-stream\r\n\r\n";
$postContent .= $fileContent . "\r\n";
$postContent .= "--" . $boundary . "\r\n";
$requestStr = 'file?method=upload&path=' . urlencode ( $targetPath . (empty ( $newFileName ) ? $fileName : $newFileName) ) . '&ondup='.$ondup.'&access_token=' . $this->_accessToken;
if ($isCreateSuperFile === TRUE) {
$requestStr .= '&type=tmpfile';
}
$result = $this->_baseControl ( $requestStr, $postContent, 'POST', array ('Content-Type' => 'multipart/form-data; boundary=' . $boundary ) );
return $result;
}
public function createSuperFile($targetPath, $fileName, array $params, $newFileName = null,$ondup = 'newcopy') {
$result = $this->_baseControl ( 'file?method=createsuperfile&path=' . urlencode ( $targetPath . (empty ( $newFileName ) ? $fileName : $newFileName) ) . '&ondup='.$ondup.'&access_token=' . $this->_accessToken, array ('param' => json_encode ( array ('block_list' => $params ) ) ), 'POST' );
return $result;
}
public function download($path) {
$result = $this->_baseControl ( 'file?method=download' . '&access_token=' . $this->_accessToken, array ('path' => $path ), 'GET' );
return $result;
}
public function makeDirectory($path) {
$result = $this->_baseControl ( 'file?method=mkdir' . '&access_token=' . $this->_accessToken, array ('path' => $path ), 'POST' );
return $result;
}
public function getMeta($path) {
$result = $this->_baseControl ( 'file?method=meta' . '&access_token=' . $this->_accessToken, array ('path' => $path ) );
return $result;
}
public function getBatchMeta(array $paths) {
$list = array ();
foreach ( $paths as $value ) {
array_push ( $list, array ('path' => $value ) );
}
$list = array ('list' => $list );
$list = json_encode ( $list );
$result = $this->_baseControl ( 'file?method=meta' . '&access_token=' . $this->_accessToken, array ('param' => $list ), 'POST' );
return $result;
}
public function listFiles($path, $by = 'name', $order = 'asc', $limit = '0-9') {
$result = $this->_baseControl ( 'file?method=list' . '&access_token=' . $this->_accessToken, array ('path' => $path, 'by' => $by, 'order' => $order, 'limit' => $limit ) );
return $result;
}
public function moveSingle($from, $to) {
$result = $this->_baseControl ( 'file?method=move' . '&access_token=' . $this->_accessToken, array ('from' => $from, 'to' => $to ), 'POST' );
return $result;
}
public function moveBatch(array $from, array $to) {
$list = array ();
for($i = 0; $i < count ( $from ); $i ++) {
array_push ( $list, array ('from' => $from [$i], 'to' => $to [$i] ) );
}
$list = array ('list' => $list );
$list = json_encode ( $list );
$result = $this->_baseControl ( 'file?method=move' . '&access_token=' . $this->_accessToken, array ('param' => $list ), 'POST' );
return $result;
}
public function copySingle($from, $to) {
$result = $this->_baseControl ( 'file?method=copy' . '&access_token=' . $this->_accessToken, array ('from' => $from, 'to' => $to ), 'POST' );
return $result;
}
public function copyBatch(array $from, array $to) {
$list = array ();
for($i = 0; $i < count ( $from ); $i ++) {
array_push ( $list, array ('from' => $from [$i], 'to' => $to [$i] ) );
}
$list = array ('list' => $list );
$list = json_encode ( $list );
$result = $this->_baseControl ( 'file?method=copy' . '&access_token=' . $this->_accessToken, array ('param' => $list ), 'POST' );
return $result;
}
public function deleteSingle($path) {
$result = $this->_baseControl ( 'file?method=delete' . '&access_token=' . $this->_accessToken, array ('path' => $path ), 'POST' );
return $result;
}
public function deleteBatch(array $paths) {
$list = array ();
foreach ( $paths as $value ) {
array_push ( $list, array ('path' => $value ) );
}
$list = array ('list' => $list );
$list = json_encode ( $list );
$result = $this->_baseControl ( 'file?method=delete' . '&access_token=' . $this->_accessToken, array ('param' => $list ), 'POST' );
return $result;
}
public function search($path, $wd, $re = 1) {
$result = $this->_baseControl ( 'file?method=search' . '&access_token=' . $this->_accessToken, array ('path' => $path, 'wd' => $wd, 're' => $re ) );
return $result;
}
public function thumbnail($path, $width, $height, $quality = 100) {
$result = $this->_baseControl ( 'thumbnail?method=generate' . '&access_token=' . $this->_accessToken, array ('path' => $path, 'width' => $width, 'height' => $height, 'quality' => $quality ), 'GET' );
return $result;
}
public function diff($cursor) {
$result = $this->_baseControl ( 'file?method=diff' . '&access_token=' . $this->_accessToken, array ('cursor' => $cursor ) );
return $result;
}
public function downloadStream($path) {
$result = $this->_baseControl ( 'stream?method=download' . '&access_token=' . $this->_accessToken, array ('path' => $path ) );
return $result;
}
public function getStreamUri($path) {
$result = $this->_baseControl ( 'stream?method=download' . '&access_token=' . $this->_accessToken, array ('path' => $path ),'GET',array('returnurl'=>true) );
return $result;
}
public function getStreamingUri($path,$type) {
$result = $this->_baseControl ( 'file?method=streaming' . '&access_token=' . $this->_accessToken, array ('path' => $path, 'type' => $type ),'GET',array('returnurl'=>true) );
return $result;
}
public function listStream($type, $start = 0, $limit = '1000', $filterPath = '') {
$result = $this->_baseControl ( 'stream?method=list' . '&access_token=' . $this->_accessToken, array ('type' => $type, 'start' => $start, 'limit' => $limit, 'filter_path' => $filterPath ) );
return $result;
}
public function streaming($path, $type) {
$result = $this->_baseControl ( 'file?method=streaming' . '&access_token=' . $this->_accessToken, array ('path' => $path, 'type' => $type ) );
return $result;
}
public function cloudMatch($path, $contentLength, $contentMd5, $sliceMd5, $contentCrc32) {
$result = $this->_baseControl ( 'file?method=rapidupload' . '&access_token=' . $this->_accessToken, array ('path' => $path, 'content-length' => $contentLength, 'content-md5' => $contentMd5, 'slice-md5' => $sliceMd5, 'content-crc32' => $contentCrc32 ) );
return $result;
}
public function addOfflineDownloadTask($savePath, $sourceUrl, $rateLimit = '', $timeout = 3600, $callback='', $expires = ''){
$result = $this->_baseControl ( 'services/cloud_dl?method=add_task' . '&access_token=' . $this->_accessToken, array ('save_path' => $savePath, 'source_url' => $sourceUrl, 'rate_limit' => $rateLimit, 'timeout' => $timeout, 'callback' =>$callback ), 'POST' );
return $result;
}
public function queryOfflineDownloadTask($taskIds, $opType = 1, $expires = ''){
$result = $this->_baseControl ( 'services/cloud_dl?method=query_task' . '&access_token=' . $this->_accessToken, array ('task_ids' => $taskIds, 'op_type' => $opType) );
return $result;
}
public function listOfflineDownloadTask($start = 0, $limit = 10, $asc = 0, $sourceURL = '', $savePath = '', $createTime = '', $status = 1, $needTaskInfo = 1, $expires = ''){
$result = $this->_baseControl ( 'services/cloud_dl?method=list_task' . '&access_token=' . $this->_accessToken,
array ('start' => $start, 'limit' => $limit, 'asc' => $asc, 'source_url' => $sourceURL,
'save_path' => $savePath, 'create_time' => $createTime, 'status' => $status, 'need_task_info' =>$needTaskInfo), 'POST' );
return $result;
}
public function cancelOfflineDownloadTask($taskId, $expires = ''){
$result = $this->_baseControl ( 'services/cloud_dl?method=cancel_task' . '&access_token=' . $this->_accessToken, array ('task_id' => $taskId) );
return $result;
}
}
?>