forked from xytoki/TCShare
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
243 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
<?php | ||
namespace xyToki\xyShare\Providers; | ||
use TC; | ||
use Flight; | ||
use Throwable; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Cookie\CookieJar; | ||
use xyToki\xyShare\Cache; | ||
use xyToki\xyShare\abstractInfo; | ||
use xyToki\xyShare\authProvider; | ||
use xyToki\xyShare\contentProvider; | ||
use xyToki\xyShare\fileInfo; | ||
use xyToki\xyShare\folderInfo; | ||
use xyToki\xyShare\Errors\NotFound; | ||
use xyToki\xyShare\Errors\NoPermission; | ||
use xyToki\xyShare\Errors\NotAuthorized; | ||
use xyToki\xyShare\Errors\NotConfigured; | ||
use Symfony\Contracts\Cache\ItemInterface; | ||
|
||
class caiyun implements contentProvider { | ||
private $sky; | ||
public $AK; | ||
public $SK; | ||
public $FD; | ||
public $BASE; | ||
public $token; | ||
public $keyPrefix="caiyun"; | ||
const rootId="00019700101000000001";//Magic Number.Why? | ||
function __construct($options){ | ||
if(empty($options['TOKEN'])){ | ||
throw new NotAuthorized(); | ||
} | ||
try{ | ||
$j = json_decode($options['TOKEN']); | ||
if($j)$options['TOKEN'] = $j->cyToken; | ||
}catch(Throwable $e){} | ||
$this->cookie = $options['TOKEN']; | ||
$cookieJar = CookieJar::fromArray([ | ||
'.mssc' => $options['TOKEN'] | ||
], 'caiyun.feixin.10086.cn'); | ||
$this->http = new Client([ | ||
'base_uri' => 'https://caiyun.feixin.10086.cn/portal/webdisk2', | ||
'timeout' => 5.0, | ||
'cookies' => $cookieJar, | ||
]); | ||
} | ||
private function getListById($id){ | ||
$cache = Cache::getInstance(); | ||
$key = $this->keyPrefix.".getListById.".md5($this->cookie).".".$id; | ||
$key = str_replace("/",".",$key); | ||
$cached=1; | ||
$res = $cache->get($key, function (ItemInterface $item) use(&$cached,$id) { | ||
$s_id = explode("/",$id); | ||
$cached=0; | ||
$item->expiresAfter(300); | ||
$res = $this->http->request("POST","/queryContentAndCatalog!disk.action", [ | ||
'form_params' => [ | ||
"startNumber"=>'1', | ||
"endNumber"=>'9999', | ||
"contentID"=>$s_id[count($s_id)-1], | ||
"path"=>$id | ||
] | ||
]); | ||
return (string)$res->getBody(); | ||
}, isset($_GET['_tcshare_renew'])?INF:1.0); | ||
Flight::response()->header("X-TCShare-Caiyun-".rand(0,999),$key."=".($cached?"cached":"refreshed")); | ||
$r = json_decode($res,true); | ||
return $r['dci']; | ||
} | ||
private function getByPath($path){ | ||
$path = TC::path("/".$this->BASE."/".$path,false); | ||
if($path=="/"){ | ||
return [ | ||
"path"=>$this::rootId | ||
]; | ||
}else{ | ||
$paths = explode("/",$path); | ||
array_shift($paths); | ||
$parent = [ | ||
"path"=>$this::rootId | ||
]; | ||
foreach($paths as $i=>$n){ | ||
if(!$parent['path']){ | ||
throw new NotFound; | ||
} | ||
$dir = $this->getListById($parent['path']); | ||
$current = false; | ||
foreach($dir['cataloginfos'] as $f){ | ||
if($f['catalogName']==$n){ | ||
$current = $f; | ||
break; | ||
} | ||
} | ||
foreach($dir['contents'] as $f){ | ||
if($f['contentName']==$n){ | ||
$current = $f; | ||
break; | ||
} | ||
} | ||
if(!$current){ | ||
throw new NotFound; | ||
} | ||
$parent = $current; | ||
} | ||
} | ||
return $current; | ||
} | ||
function getFileInfo($path){ | ||
$fileInfo=$this->getByPath($path); | ||
if(isset($fileInfo['contentName'])){ | ||
return new caiyunFileInfo($fileInfo,$this); | ||
}else{ | ||
return new caiyunFolderInfo($fileInfo); | ||
} | ||
} | ||
function listFiles($fileInfo){ | ||
if(!$fileInfo instanceof caiyunFolderInfo)throw new \Exception(); | ||
$list = $this->getListById($fileInfo->file['path']); | ||
$returns=[[],[]]; | ||
$folders=TC::toArr($list['cataloginfos']); | ||
$files=TC::toArr($list['contents']); | ||
foreach($folders as $one){ | ||
if(!$one)continue; | ||
$returns[0][]=new caiyunFolderInfo($one); | ||
} | ||
foreach($files as $one){ | ||
if(!$one)continue; | ||
$returns[1][]=new caiyunFileInfo($one,$this); | ||
} | ||
return $returns; | ||
} | ||
} | ||
class caiyunAuth { | ||
function __construct($options){ | ||
$this->user = $options['USER']; | ||
$this->user = $options['PASS']; | ||
$cookieJar = CookieJar::fromArray([ | ||
], 'caiyun.feixin.10086.cn'); | ||
$this->http = new Client([ | ||
'base_uri' => 'https://caiyun.feixin.10086.cn/portal/webdisk2', | ||
'timeout' => 5.0, | ||
'cookies' => $cookieJar, | ||
]); | ||
} | ||
} | ||
|
||
class caiyunHook{ | ||
function __construct($client){ | ||
$this->client = $client; | ||
$this->run(); | ||
} | ||
function run(){ | ||
|
||
} | ||
} | ||
|
||
class caiyunAbstractInfo implements abstractInfo{ | ||
public $file; | ||
function __construct($file){ | ||
$this->file=$file; | ||
} | ||
public function isFolder(){ | ||
|
||
} | ||
public function name(){ | ||
return isset($this->file['contentName'])?$this->file['contentName']:$this->file['catalogName']; | ||
} | ||
public function timeModified(){ | ||
return $this->formatCaiyunTime($this->file['updateTime']); | ||
} | ||
public function timeCreated(){ | ||
return $this->formatCaiyunTime($this->file['uploadTime']); | ||
} | ||
private function formatCaiyunTime($d){ | ||
$ds = str_split($d); | ||
array_splice($ds, 4, 0, ['-']); | ||
array_splice($ds, 7, 0, ['-']); | ||
array_splice($ds, 10, 0, [' ']); | ||
array_splice($ds, 13, 0, [':']); | ||
array_splice($ds, 16, 0, [':']); | ||
return implode("",$ds); | ||
} | ||
} | ||
class caiyunFileInfo extends caiyunAbstractInfo implements fileInfo{ | ||
function __construct($file,$client){ | ||
parent::__construct($file); | ||
$this->client=$client; | ||
} | ||
public function isFolder(){ | ||
return false; | ||
} | ||
public function url(){ | ||
$cached = 1; | ||
$key="caiyun.download.".$this->file['digest']; | ||
$res = $this->client->http->get('/downLoadAction!downloadToPC.action?shareContentIDs='.$this->file['contentID']); | ||
return json_decode($res->getBody(),true)['redirectURL']; | ||
} | ||
public function size(){ | ||
return $this->file['contentSize']; | ||
} | ||
public function extension(){ | ||
return strtolower($this->file['contentSuffix']); | ||
} | ||
public function thumbnail(){ | ||
if(!isset($this->file['thumbnailURL'])){ | ||
return false; | ||
} | ||
return $this->file['thumbnailURL']; | ||
} | ||
} | ||
class caiyunFolderInfo extends caiyunAbstractInfo implements folderInfo{ | ||
function __construct($file){ | ||
parent::__construct($file); | ||
} | ||
public function isFolder(){ | ||
return true; | ||
} | ||
public function hasIndex(){ | ||
//Not Implemented. | ||
//TODO | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters