forked from banditsmile/baidu_pcs_ui
/
controller.php
277 lines (251 loc) · 7.99 KB
/
controller.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/**
* Created by PhpStorm.
* User: xubandit
* Date: 15/6/30
* Time: 下午11:11
*/
class base_controller{
public function __construct(){
}
}
class baidu_pcs_controller extends base_controller{
var $baidu_pcs;
var $default_download_path = DOWN_PATH;//默认下载目录
var $down_status=array();
var $down_status_file = BASE_PATH.'/data/download.php';
public function __construct(){
parent::__construct();
$this->baidu_pcs = baidu_pcs::instance();
}
public function index(){
return $this->render('list_body');
}
public function get_list(){
$path = get_post('path');
$path = rtrim($path,'/');
$result = $this->baidu_pcs->get_list($path);
$list = array();
foreach($result as $item){
$item['name'] = str_replace($path.'/','',$item['name']);
$item['size'] = human_filesize($item['size'],0);
$list[]=$item;
}
unset($result);
if($path !='/' && $path !=''){
$parent_dir = array('name'=>'..','size'=>'0B','date'=>max(array_column($list,'date')),'type'=>'d');
array_unshift($list,$parent_dir);
}
return include BASE_PATH.'/templates/list.php';
}
public function download(){
$file = rtrim(get_post('file'),'/ ');
$path = trim(get_post('path'));
//检查网盘是否存在该文件
$file_info = $this->baidu_pcs->meta($file);
if(empty($file_info) ){
return json_output(101,'文件不存在');
}
if($file_info['Is Dir']=='Yes'){
return json_output(101,'不支持目录下载');
}
//设置下载在路径
$file_name = substr($file,strrpos($file,'/')+1);
if(empty($path)){
$path = $this->default_download_path.'/';
}
if(is_dir($path)){
$path = rtrim($path,'/').'/'.$file_name;
}
if(file_exists($path)){
return json_output(103,'下载位置已存在文件'.$path);
}
$down_info = $this->baidu_pcs->download($file,$path);
if($down_info){
$this->save_download_info($down_info);
return json_output(0,'success',$down_info);
}else{
return json_output(104,'下载失败请重试');
}
}
/**
* @param $data
* @return int
* @param $full
* @desc 记录下载信息
*/
private function save_download_info($data,$full=false){
$info_file = $this->down_status_file;
if(!$full){//只修改部分
if(!is_writable(dirname($info_file))){
return false;
}
if(is_file($info_file)){
$info = include $info_file;
}else{
$info = array();
}
$data['status']=0;//开始下载
$info[md5($data['from'])]=$data;
}else{
$info = $data;
}
$info_str = '<?php'.PHP_EOL;
$info_str .= 'return '.PHP_EOL;
$info_str .=var_export($info,true);
$info_str .=';';
if(file_put_contents($info_file,$info_str)){
$this->down_status = array();
return true;
}else{
return false;
}
}
public function download_status(){
$down_status = $this->get_down_status();
$list = array();
$modified = false;
foreach($down_status as $key=>$status){
//下载状态文件被清除,那就也清除下载信息
if(!is_file($status['progress'])){
unset($down_status[$key]);
$modified = true;
continue;
}
//已下载完成和暂停的下载不作处理
if(isset($status['status']) and $status['status']==2){
$list[$key]=$status;
continue;
}
if(is_file($status['to'])){
$status['status'] = 2;//下载完成
$down_status[$key]['status']=2;
$modified=true;
}else{
$progress_info = $this->get_download_info($status['progress']);
if($progress_info===false){//无法获取下载信息
unset($down_status[$key]);
$modified = true;
continue;
}
$status['info'] = $progress_info;
//$status['status']=0;//正在下载
}
$list[$key]=$status;
}
//对下载状态做过修正,重新写回去
if($modified){$this->save_download_info($down_status,true);}
$status_array =array_column($list,'status');
//按照下载状态排序0正在下载 1暂停 2下载完成
array_multisort($status_array,$list);
$this->render('download_body',array('list'=>$list));
}
/**
* @desc 从下载状态文件解析下载进度
* @param $progress_file
* @return array|bool
*@todo 可能出现文件刚刚创建就获取文件信息fseek失败的情况
*/
protected function get_download_info($progress_file){
if(false === ($fh = fopen($progress_file,'r'))){
return false;
}
if(fseek($fh,-100,SEEK_END) !==0){
return false;
}
$str = fgets($fh,100);
fclose($fh);
$last = substr($str,strrpos($str,chr(13))+1);//取最后一行
$info=array_filter(explode("\t",$last));
return $info;
}
public function get_down_info(){
$file_key = get_post('file_key');
$down_status = $this->get_down_status();
if(!isset($down_status[$file_key])){
return json_output(101,'file not exist');
}
$info = $this->get_download_info($down_status[$file_key]['progress']);
if($info){
return json_output(0,'success',$info);
}else{
return json_output(101,'file not exist');
}
}
protected function get_down_status(){
if(empty($this->down_status)){
$this->down_status = include $this->down_status_file;
}
return $this->down_status;
}
public function render($template,$data=array()){
include BASE_PATH.'/templates/header.php';
extract($data);
include BASE_PATH.'/templates/'.$template.'.php';
include BASE_PATH.'/templates/footer.php';
return true;
}
public function pause(){
$file_key = get_post('file_key');
$down_status = $this->get_down_status();
if(empty($down_status) || !isset($down_status[$file_key])){
return json_output('101','参数错误');
}
if(empty($down_status[$file_key]['pid'])){
return json_output('102','系统错误');
}
if($this->baidu_pcs->kill($down_status[$file_key]['pid'])){
$down_status[$file_key]['status']=1;
$down_status[$file_key]['pid']=0;
$this->save_download_info($down_status,true);
return json_output(0,'success');
}else{
return json_output(103,'暂停失败请重试');
}
}
/**
* @desc 继续被暂停的下载
*/
public function continue_download(){
$file_key = get_post('file_key');
$down_status = $this->get_down_status();
if(empty($down_status) || !isset($down_status[$file_key])){
return json_output('101','参数错误');
}
if(!isset($down_status[$file_key]['from']) || !isset($down_status[$file_key]['to'])){
return json_output('102','系统错误');
}
$down_info = $this->baidu_pcs->download($down_status[$file_key]['from'],$down_status[$file_key]['to'],false,$down_status[$file_key]['progress']);
if($down_info){
$down_status[$file_key]=$down_info;
$this->save_download_info($down_status,true);
return json_output(0,'success',$down_info);
}else{
return json_output(101,'系统错误请重试');
}
}
/**
* @desc 删除一个下载任务,通常是已经被完成的下载任务
*/
public function remove(){
$file_key =get_post('file_key');
$down_status = $this->get_down_status();
if(empty($down_status) || !isset($down_status[$file_key])){
return json_output('101','参数错误');
}
if(!isset($down_status[$file_key]['progress']) ){
return json_output('102','系统错误');
}
if(is_file($down_status[$file_key]['pregress'])){
unlink($down_status[$file_key]['pregress']);
}
if($down_status[$file_key]['pid']){
$this->baidu_pcs->kill($down_status[$file_key]['pid']);
}
unset($down_status[$file_key]);
return json_output(0,'success');
}
public function setting(){
return $this->render('setting_body');
}
}