Skip to content

Commit

Permalink
动态调节上传分块大小,优化上传速率
Browse files Browse the repository at this point in the history
  • Loading branch information
donwa committed Apr 13, 2018
1 parent 9f7041c commit d63f872
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
3 changes: 3 additions & 0 deletions lib/onedrive.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ static function upload_session_status($url){
fetch::$headers = "Authorization: bearer {$token}\r\nContent-Type: application/json";
$resp = fetch::get($url);
$data = json_decode($resp->content, true);
if($resp->http_code == 404){
return false;
}
return $data;
}

Expand Down
36 changes: 23 additions & 13 deletions one.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ static function upload_file($localfile, $remotefile=null){
}

static function upload_large_file($localfile, $remotepath){
//$length = 327680;
$length = 10485760;
fetch::init([CURLOPT_TIMEOUT=>200]);
$upload = config('@upload');
$info = $upload[$remotepath];
Expand All @@ -87,6 +85,7 @@ static function upload_large_file($localfile, $remotepath){
$info['remotepath'] = $remotepath;
$info['filesize'] = filesize($localfile);
$info['offset'] = 0;
$info['length'] = 327680;
$info['update_time'] = time();
$upload[$remotepath] = $info;
config('@upload', $upload);
Expand All @@ -102,34 +101,45 @@ static function upload_large_file($localfile, $remotepath){
return self::upload_large_file($localfile, $remotepath);
}

print '上传分块'.round(($info['offset']/$info['filesize'])*100).'% ';
print '上传分块'.round(($info['offset']/$info['filesize'])*100).'% '.onedrive::human_filesize($info['length']).' ';
$begin_time = microtime(true);
$data = onedrive::upload_session($info['url'], $info['localfile'], $info['offset'], $length);
$data = onedrive::upload_session($info['url'], $info['localfile'], $info['offset'], $info['length']);

if(!empty($data['@content.downloadUrl'])){
unset($upload[$remotepath]);
config('@upload', $upload);
print '上传完成!'.PHP_EOL;
_refresh_cache(pathinfo($remotepath,PATHINFO_DIRNAME));
return;
}

if(!empty($data['nextExpectedRanges'])){
$upload_time = microtime(true) - $begin_time;
print onedrive::human_filesize($length/$upload_time).'/s'.PHP_EOL;
print onedrive::human_filesize($info['length']/$upload_time).'/s'.' '.intval($upload_time).'s'.PHP_EOL;
$info['length'] = intval($info['length']/$upload_time/100)*1024;

list($offset, $filesize) = explode('-',$data['nextExpectedRanges'][0]);
$info['offset'] = $offset;
$info['update_time'] = time();
$upload[$remotepath] = $info;
config('@upload', $upload);
}elseif(!empty($data['@content.downloadUrl'])){
unset($upload[$remotepath]);
config('@upload', $upload);
print '上传完成!'.PHP_EOL;
_refresh_cache(pathinfo($remotepath,PATHINFO_DIRNAME));
return;
}else{
print '失败!'.PHP_EOL;
$data = onedrive::upload_session_status($info['url']);
if($data === false){
unset($upload[$remotepath]);
config('@upload', $upload);
}elseif(!empty($data['nextExpectedRanges'])){
list($offset, $filesize) = explode('-',$data['nextExpectedRanges'][0]);
$info['offset'] = $offset;
$upload[$remotepath] = $info;
config('@upload', $upload);
}
}

return self::upload_large_file($localfile, $remotepath);

}


}


Expand Down

0 comments on commit d63f872

Please sign in to comment.