forked from qupb/oneindex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
one.php
163 lines (144 loc) · 4.7 KB
/
one.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
if( php_sapi_name() !== "cli" ){
die( "NoAccess" );
}
require 'init.php';
ini_set('memory_limit', '128M');
class one{
static function cache_clear(){
$dir=opendir(CACHE_PATH);
while ($file=readdir($dir)) {
@unlink(CACHE_PATH.$file);
}
}
static function cache_refresh(){
self::_refresh_cache(get_absolute_path(config('onedrive_root')));
}
static function _refresh_cache($path){
echo $path.PHP_EOL;
$items = onedrive::dir($path);
if(is_array($items)){
cache('dir_'.$path, $items);
}
foreach((array)$items as $item){
if($item['folder']){
self::_refresh_cache($path.urlencode($item['name']).'/');
}
}
}
static function token_refresh(){
$refresh_token = config('refresh_token');
$token = onedrive::get_token($refresh_token);
if(!empty($token['refresh_token'])){
config('@token', $token);
}
}
static function upload_file($localfile, $remotefile=null){
if(!file_exists($localfile)){
exit('file not exists');
}
print '本地文件:'.$localfile.PHP_EOL;
if(empty($remotefile)){
$remotepath = pathinfo($localfile, PATHINFO_BASENAME);
}elseif(substr($remotefile, -1) == '/'){
$remotepath = get_absolute_path($remotefile);
$remotepath = substr($remotepath,1).pathinfo($localfile, PATHINFO_BASENAME);
}else{
$remotepath = ltrim($remotefile, '/');
}
print '远程文件:'.$remotepath.PHP_EOL;
$filesize = onedrive::_filesize($localfile) OR die('无法获取文件大小');
if($filesize < 10485760){
print '上传方式:直接上传'.PHP_EOL;
$begin_time = microtime(true);
$result = onedrive::upload($remotepath, file_get_contents($localfile));
if(!empty($result)){
$upload_time = microtime(true) - $begin_time;
print '上传成功:'.onedrive::human_filesize($filesize/$upload_time).'/s'.PHP_EOL;
}else{
print '上传失败!'.PHP_EOL;
}
}else{
print '上传方式:分块上传'.PHP_EOL;
return self::upload_large_file($localfile, $remotepath);
}
return;
}
static function upload_large_file($localfile, $remotepath){
fetch::init([CURLOPT_TIMEOUT=>200]);
$upload = config('@upload');
$info = $upload[$remotepath];
if(empty($info['url'])){
print '创建上传会话'.PHP_EOL;
$data = onedrive::create_upload_session($remotepath);
if(!empty($data['uploadUrl'])){
$info['url'] = $data['uploadUrl'];
$info['localfile'] = $localfile;
$info['remotepath'] = $remotepath;
$info['filesize'] = onedrive::_filesize($localfile);
$info['offset'] = 0;
$info['length'] = 327680;
$info['update_time'] = time();
$upload[$remotepath] = $info;
config('@upload', $upload);
}elseif ( $data === false ){
print '文件已存在!'.PHP_EOL;
return;
}
}
if(empty($info['url'])){
print '获取会话失败!'.PHP_EOL;
sleep(3);
return self::upload_large_file($localfile, $remotepath);
}
print '上传分块'.onedrive::human_filesize($info['length']).' ';
$begin_time = microtime(true);
$data = onedrive::upload_session($info['url'], $info['localfile'], $info['offset'], $info['length']);
if(!empty($data['nextExpectedRanges'])){
$upload_time = microtime(true) - $begin_time;
print onedrive::human_filesize($info['length']/$upload_time).'/s'.' '.round(($info['offset']/$info['filesize'])*100).'% '.PHP_EOL;
$info['length'] = intval($info['length']/$upload_time/32768*2)*327680;
$info['length'] = ($info['length']>104857600)?104857600:$info['length'];
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;
self::_refresh_cache(pathinfo($remotepath,PATHINFO_DIRNAME));
return;
}else{
print '失败!'.PHP_EOL;
$data = onedrive::upload_session_status($info['url']);
if($data === false){
onedrive::delete_upload_session($info['url']);
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);
}
}
array_shift($argv);
$action = str_replace(':', '_',array_shift($argv));
if(is_callable(['one',$action])){
@call_user_func_array(['one',$action], $argv);
exit();
}
?>
oneindex commands :
cache
cache:clear clear cache
cache:refresh refresh cache
token
token:refresh refresh token
upload
upload:file upload a file to onedrive