zlibproxy/.history/app/controller/File_20230102173122.php

79 lines
2.9 KiB
PHP
Raw Normal View History

2023-01-02 19:56:44 +08:00
<?php
namespace app\controller;
use Webman\Captcha\CaptchaBuilder;
use Respect\Validation\Validator as v;
use support\Request;
use yzh52521\EasyHttp\Response;
use yzh52521\EasyHttp\RequestException;
use yzh52521\EasyHttp\Http;
use support\Redis;
use support\View;
class File
{
public function check(Request $request)
{
$id=$request->cookie('id', 'blank');
$key=$request->cookie('key', 'blank');
if($key=='blank' || $id=='blank'){
return json(['code'=>509,'msg'=>'未登录']);
}
$path=$request->input('path','blank');
if($path=='blank'){
return json(['code'=>500,'msg'=>'缺少参数']);
}
$name=base64_encode($path);
$redis = Redis::connection('default');
$file=base_path().'/books/'.$name.'bin';
if($redis->exist('BookCache_'.$name)){
if($redis->hget('BookCache_'.$name,'Status')=='OK'){
return json(['code'=>200, 'msg'=>'找到文件', 'size'=>$redis->hget('BookCache_'.$name,'Size')]);
}else{
return json(['code'=>202,'msg'=>'文件下载中'])
}
}else {
return json(['code'=>201, 'msg'=>'未找到文件']);
}
}
public function download(Request $request)
{
$id=$request->cookie('id', 'blank');
$key=$request->cookie('key', 'blank');
if($key=='blank' || $id=='blank'){
return json(['code'=>509,'msg'=>'未登录']);
}
$path=$request->input('path','blank');
$auth=array('remix_userid'=>"$id",'remix_userkey'=>"$key",'siteLanguageV2'=>'zh','selectedSiteMode'=>'books');
$url=getenv('Zlibrary')."$path";
$get=Http::withProxy('socks5h://'.getenv('proxy'))->withCookies($auth,getenv('Zlibrary'));
$head=$get->head($url);
$size=round(($head->header('Content-Length')/1048576),2);
if($size>=30){
return json(['code'=>500,'msg'=>'文件太大了!']);
}
if($head->header('Content-Type')=='text/html; charset=UTF-8'){
return json(['code'=>404,'msg'=>'链接错误']);
}
$redis = Redis::connection('default');
$redis->hset('BookCache_'.$name,'Time',time());
$redis->hset('BookCache_'.$name,'Date',date("Y/m/d H:i:s"));
$redis->hset('BookCache_'.$name,'Status','Downloading');
if(!is_dir(base_path().'/books/'.date("Y/m/"))){
mkdir(base_path().'/books/'.date("Y/m/"),0777,true);
}
$name=base64_encode($path);
$filepath=base_path().'/books/'.date("Y/m/").$name.'.bin';
$return=$get->get($url);
$file = fopen("$filepath","w");
fwrite($file,$return);
fclose($file);
$redis->hset('BookCache_'.$name,'Path',$filepath);
$redis->hset('BookCache_'.$name,'Size',$size);
$redis->hset('BookCache_'.$name,'Status','OK');
return json(['code'=>200]);
}
}