zlibproxy/.history/app/controller/File_20230102183433.php

99 lines
3.8 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');
$path=$request->input('path','blank');
if($key=='blank' || $id=='blank'){
return json(['code'=>509,'msg'=>'未登录或参数错误']);
}
$name=base64_encode($path);
$redis = Redis::connection('default');
if($redis->exist('BookCache_'.$name)){
if($redis->hget('BookCache_'.$name,'Status')=='OK'){
return json(['code'=>200, 'msg'=>'找到文件', 'size'=>$redis->hget('BookCache_'.$name,'Size'),'name'=>$redis->hget('BookCache_'.$name,'Name')]);
}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');
$path=$request->input('path','blank');
if($key=='blank' || $id=='blank'){
return json(['code'=>509,'msg'=>'未登录或参数错误']);
}
$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');
$name=base64_encode($path);
$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);
}
$filepath=base_path().'/books/'.date("Y/m/").$name.'.bin';
$return=$get->get($url);
$file = fopen("$filepath","w");
fwrite($file,$return);
fclose($file);
$filerealname=explode('"',explode(";",$head->header('Content-Disposition'))[1])[1];
$redis->hset('BookCache_'.$name,'Path',$filepath);
$redis->hset('BookCache_'.$name,'Size',$size);
$redis->hset('BookCache_'.$name,'Name',$filerealname);
$redis->hset('BookCache_'.$name,'Status','OK');
return json(['code'=>200,'name'=>"$filerealname",'size'=>"$size"]);
}
public function get(Request $request)
{
$id=$request->cookie('id', 'blank');
$key=$request->cookie('key', 'blank');
$path=$request->input('path','blank');
if($key=='blank' || $id=='blank'){
return json(['code'=>509,'msg'=>'未登录或参数错误']);
}
$redis = Redis::connection('default');
$name=base64_encode($path);
if($redis->exist('BookCache_'.$name)){
if($redis->hget('BookCache_'.$name,'Status')=='OK'){
return response()->download($redis->hget('BookCache_'.$name,'Path'),$redis->hget('BookCache_'.$name,'Name'));
}else{
return json(['code'=>202,'msg'=>'文件仍在下载中']);
}
}else {
return json(['code'=>201, 'msg'=>'未找到文件']);
}
}
}