Skip to content

Commit

Permalink
完成大文件分割上传相关
Browse files Browse the repository at this point in the history
  • Loading branch information
oott123 committed Jul 23, 2013
1 parent dfb52bd commit 1818f51
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 10 deletions.
18 changes: 14 additions & 4 deletions _bpcs_files_/common.inc.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
<?php
function echon($str){
echo $str."\n";
function echon($str,$debug=false){
if($debug){
echo $str."\n";
}else{
echo chr(27)."[0;34m".$str.chr(27)."[m\n";
}
}
function getline(){
return trim(fgets(STDIN));
}
function getpath($path){
return '/apps/'.urlencode(file_get_contents(CONFIG_DIR.'/appname').'/'.$path);
}
function oaerr($arr,$exitonerror = 1){
if(isset($arr['error'])){
echon('OAuth error '.$arr['error'].' : '.$arr['error_description']);
Expand Down Expand Up @@ -38,7 +45,7 @@ function continueornot(){
}
function cmd($cfe) {
$res = '';
echon($cfe);
echon($cfe,1);
$cfe = $cfe;
if ($cfe) {
if(function_exists('exec')) {
Expand All @@ -64,6 +71,7 @@ function cmd($cfe) {
@pclose($f);
}
}
echon($res,1);
return $res;
}
function do_api($url,$param,$method = 'POST'){
Expand All @@ -78,7 +86,9 @@ function do_api($url,$param,$method = 'POST'){
function error_handle($errno, $errstr, $errfile, $errline){
switch ($errno) {
case E_USER_ERROR:
echon("Fatal ERROR : $errfile ($errline) $errstr");die('Exiting with a fatal error.'."\n");
echon("Fatal ERROR : $errfile ($errline) $errstr");
echon('Exiting with a fatal error.'."\n");
die(9002);
break;
case E_USER_WARNING:
echon("WARNING : $errfile ($errline) $errstr");
Expand Down
59 changes: 56 additions & 3 deletions _bpcs_files_/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function get_quota($access_token){
return $quota;
}
function upload_file($access_token,$path,$localfile,$ondup='newcopy'){
$path='/apps/'.urlencode(file_get_contents(CONFIG_DIR.'/appname').'/'.$path);
$path = getpath($path);
$url = "https://c.pcs.baidu.com/rest/2.0/pcs/file?method=upload&access_token=$access_token&path=$path&ondup=$ondup";
$add = "--form file=@$localfile";
$cmd = "curl -X POST -k -L $add \"$url\"";
Expand All @@ -159,16 +159,69 @@ function upload_file($access_token,$path,$localfile,$ondup='newcopy'){
return $cmd;
}
function delete_file($access_token,$path){
$path='/apps/'.urlencode(file_get_contents(CONFIG_DIR.'/appname').'/'.$path);
$path = getpath($path);
$dele=do_api('https://pcs.baidu.com/rest/2.0/pcs/file',"method=delete&access_token=".$access_token.'&path='.$path,'GET');
$dele=json_decode($dele,1);
apierr($dele);
return $dele;
}
function fetch_file($access_token,$path,$url){
$path='/apps/'.urlencode(file_get_contents(CONFIG_DIR.'/appname').'/'.$path);
$path = getpath($path);
$fetch=do_api('https://pcs.baidu.com/rest/2.0/pcs/services/cloud_dl',"method=add_task&access_token=".$access_token.'&save_path='.$path.'&source_url='.$url,'GET');
$fetch=json_decode($fetch,1);
apierr($fetch);
return $fetch;
}
//分片上传
function super_file($access_token,$path,$localfile,$ondup='newcopy',$sbyte=1073741824,$temp_dir='/tmp/'){
//调用split命令进行切割
//split -b200 --verbose rubygems-1.8.25.zip rg/rg1
if(filesize($localfile)<=$sbyte){
echon('The file is not as big as it need to be created by superfile.');
upload_file($access_token,$path,$localfile,$ondup); //直接上传
}
$tempfdir = rtrim($temp_dir,'/').'/'.uniqid('bpcs_to_upload_');
if(!mkdir($tempfdir,0700,true)){
echon('Cannot create temp dir:'.$tempfdir);
die(9009);
}
$splitcmd = "split -b{$sbyte} $localfile $tempfdir/bpcs_toupload_";
$splitresult = cmd($splitcmd);
if(trim($splitresult)){
echon('Split quit with an message:'.$splitresult);
}
//遍历临时文件目录
$tempfiles = glob($tempfdir.'/bpcs_toupload_*');
if(count($tempfiles)<1){
//没有生成文件
echon('There are no files to upload.');
die(9010);
}elseif(count($tempfiles)==1){
//只有一个文件
unlink($tempfiles[0]); //删除它
echon('The file is not as big as it need to be created by superfile.');
upload_file($access_token,$path,$localfile,$ondup); //直接上传
return;
}
//开始上传进程
$block_list = array();
$count = 0;
foreach($tempfiles as $tempfile){
//上传临时文件,上传API与上传普通文件无异,只是多一个参数type=tmpfile,取消了其它几个参数。此处将“&type=tmpfile”作为ondup传递,将参数带在请求尾部。
echon('Uploading '.($count+1).' out of '.count($tempfiles).' file blocks ... ');
$count++;
$upload_res = upload_file($access_token,'',$tempfile,$ondup.'&type=tmpfile');
$block_list[] = $upload_res['md5'];
//删除临时文件
unlink($tempfile);
}
//删除临时文件夹
rmdir($tempfdir);
//准备提交API
$block_list = json_encode($block_list);
$param = '{"block_list":'.$block_list.'}';
$param = 'param='.urlencode($param);
$path = getpath($path);
$url = "https://pcs.baidu.com/rest/2.0/file?method=createsuperfile&path={$path}&access_token={$access_token}";
$res = do_api($url,$param);
}
25 changes: 22 additions & 3 deletions bpcs_uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
echo <<<EOF
===========================Baidu PCS Uploader===========================
usage : $argv[0] init|quickinit|quota
usage : $argv[0] upload|download [path_local] [path_remote]
usage : $argv[0] delete [path_remote]
usage : $argv[0] fetch [path_remote] [path_to_fetch]
usage : $argv[0] upload|download path_local path_remote
usage : $argv[0] delete path_remote
usage : $argv[0] uploadbig path_local path_remote [slice_size(def:1073741824)] [temp_dir(def:/tmp/)]
usage : $argv[0] fetch path_remote path_to_fetch
========================================================================
EOF;
Expand Down Expand Up @@ -94,4 +95,22 @@
}
fetch_file($access_token,$argv[2],$argv[3]);
break;
case 'uploadbig':
//uploadbig - 大文件上传
switch(count($argv)){
case 0:
case 1:
case 2:
case 3: //参数数目不够
echon('Parameters unmatched.');
die(9099);
case 4: //设置默认值(单个文件大小->1G)
$argv[4] = 1073741824;
//因为需要继续下面的操作所以这里没有break
case 5: //设置默认值(临时文件目录->/tmp/)
$argv[5] = '/tmp/';
//因为需要继续下面的操作所以这里没有break
default: //开始上传操作
super_file($access_token,$argv[3],$argv[2],'newcopy',$argv[4],$argv[5]);
}
}

0 comments on commit 1818f51

Please sign in to comment.