forked from Layne666/oneindex
-
Notifications
You must be signed in to change notification settings - Fork 2
/
one.php
204 lines (181 loc) · 6.16 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
if( php_sapi_name() !== "cli" ){
die( "NoAccess" );
}
require 'init.php';
ini_set('memory_limit', '128M');
class one{
static function cache_clear(){
cache::clear();
}
static function cache_refresh(){
//oneindex::refresh_cache(get_absolute_path(config('onedrive_root')));
cache::refresh_cache(get_absolute_path(config('onedrive_root')));
// 清除php文件缓存
cache::clear_opcache();
}
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){
$localfile = realpath($localfile);
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_folder($localfolder, $remotefolder='/'){
$localfolder = realpath($localfolder);
$remotefolder = get_absolute_path($remotefolder);
print ' 开始上传文件夹'.PHP_EOL;
self::folder2upload($localfolder,$remotefolder);
}
static function folder2upload($localfolder, $remotefolder){
$files = scandir($localfolder);
foreach ($files as $file) {
if ($file == '.' || $file == '..') {
continue;
}
if (is_dir($localfolder . '/' . $file)) {
self::folder2upload($localfolder . '/' . $file, $remotefolder.$file.'/');
}else{
$localfile = realpath($localfolder . '/' . $file);
$remotefile = $remotefolder.$file;
self::upload_file($localfile, $remotefile);
}
}
}
//static function add2uploading($path,$remotefolder) {
// $files = scandir($path);
// foreach ($files as $file) {
// if ($file != '.' && $file != '..') {
// if (is_dir($path . '/' . $file)) {
// self::add2uploading($path . '/' . $file, $remotefolder.$file.'/');
// } else {
// $localfile = realpath($path . '/' . $file);
// echo $localfile.PHP_EOL;
// $remotepath = $remotefolder.$file;
// $task = array(
// 'localfile'=>$localfile,
// 'remotepath' => $remotepath,
// 'filesize'=>onedrive::_filesize($localfile),
// 'update_time'=>0
// );
// $uploads = config('@upload');
// if(empty($uploads[$remotepath])){
// $uploads[$remotepath] = $task;
// config('@upload', $uploads);
// }
// }
// }
// }
//}
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;
$info['speed'] = $info['length']/$upload_time;
print onedrive::human_filesize($info['speed']).'/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']) || !empty($data['id'])){
unset($upload[$remotepath]);
config('@upload', $upload);
print ' 上传完成!'.PHP_EOL;
return;
}else{
print ' 失败!'.PHP_EOL;
$data = onedrive::upload_session_status($info['url']);
if(empty($data)|| $info['length']<100){
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;
$info['length'] = $info['length']/1.5;
$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
upload:folder upload a folder to onedrive