Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

Commit 2fafcad

Browse files
committed
⚡️ 增加缓存文件配置
1 parent 072dfa0 commit 2fafcad

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

config.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
$config=[
33
"CLUSTER_ID"=> "",
44
"CLUSTER_SECRET"=> "",
5+
"cache_dir"=> "./cache",//缓存文件夹
56
"MaxConcurrent"=> 10,//下载使用的线程
67
"Staging"=> false,//切换Staging环境
7-
"Debug"=> true,//Debug开关
8+
"Debug"=> false,//Debug开关
89
];

inc/cluster.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ public function __construct($token,$version){
8383
$this->version = $version;
8484
}
8585
public function getFileList() {
86-
if (!file_exists("./cache/filecache")) {
87-
mkdir("./cache/filecache",0777,true);
86+
global $DOWNLOAD_DIR;
87+
if (!file_exists($DOWNLOAD_DIR."/filecache")) {
88+
mkdir($DOWNLOAD_DIR."/filecache",0777,true);
8889
}
8990
$client = new Client(OPENBMCLAPI,443,true);
9091
$client->set(['timeout' => -1]);
@@ -95,14 +96,14 @@ public function getFileList() {
9596
'Authorization' => "Bearer {$this->token}"
9697
]);
9798
mlog("Start FileList Download");
98-
if (!$client->download('/openbmclapi/files',DOWNLOAD_DIR.'/filecache/filelist.zstd')) {
99+
if (!$client->download('/openbmclapi/files',$DOWNLOAD_DIR.'/filecache/filelist.zstd')) {
99100
mlog("FileList Download Failed",2);
100101
$client->close();
101102
}
102103
else{
103104
mlog("FileList Download Success");
104105
$client->close();
105-
$this->compressedData = file_get_contents("compress.zstd://".DOWNLOAD_DIR."/filecache/filelist.zstd");
106+
$this->compressedData = file_get_contents("compress.zstd://".$DOWNLOAD_DIR."/filecache/filelist.zstd");
106107
}
107108
$parser = new ParseFileList($this->compressedData);
108109
$files = $parser->parse();
@@ -122,7 +123,8 @@ public function __construct($filesList, $maxConcurrent) {
122123
}
123124

124125
private function downloader(Swoole\Coroutine\Http\Client $client, $file,$bar) {
125-
$filePath = DOWNLOAD_DIR . '/' . substr($file->hash, 0, 2) . '/';
126+
global $DOWNLOAD_DIR;
127+
$filePath = $DOWNLOAD_DIR . '/' . substr($file->hash, 0, 2) . '/';
126128
if (!file_exists($filePath)) {
127129
mkdir($filePath, 0777, true);
128130
}
@@ -139,10 +141,10 @@ private function downloader(Swoole\Coroutine\Http\Client $client, $file,$bar) {
139141
]);
140142
$client->setHeaders([
141143
'Host' => $location_url['host'],
142-
'User-Agent' => 'openbmclapi-cluster/' . VERSION . ' ' . 'PHP-OpenBmclApi/0.0.1-dev',
144+
'User-Agent' => USERAGENT,
143145
'Accept' => '*/*',
144146
]);
145-
$downloadr = $client->download($location_url['path'].'?'.($location_url['query']??''),DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash);
147+
$downloadr = $client->download($location_url['path'].'?'.($location_url['query']??''),$DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash);
146148
if (in_array($client->statusCode, [301, 302])) {
147149
while(in_array($client->statusCode, [301, 302])){
148150
$location_url = parse_url($client->getHeaders()['location']);
@@ -156,10 +158,10 @@ private function downloader(Swoole\Coroutine\Http\Client $client, $file,$bar) {
156158
]);
157159
$client->setHeaders([
158160
'Host' => $location_url['host'],
159-
'User-Agent' => 'openbmclapi-cluster/' . VERSION . ' ' . 'PHP-OpenBmclApi/0.0.1-dev',
161+
'User-Agent' => USERAGENT,
160162
'Accept' => '*/*',
161163
]);
162-
$downloadr = $client->download($location_url['path'].'?'.($location_url['query']??''),DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash);
164+
$downloadr = $client->download($location_url['path'].'?'.($location_url['query']??''),$DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash);
163165
}
164166
if (!$downloadr) {
165167
echo PHP_EOL;
@@ -206,7 +208,7 @@ public function downloadFiles() {
206208
]);
207209
$client->setHeaders([
208210
'Host' => 'openbmclapi.bangbang93.com',
209-
'User-Agent' => 'openbmclapi-cluster/' . VERSION . ' ' . 'PHP-OpenBmclApi/0.0.1-dev',
211+
'User-Agent' => USERAGENT,
210212
'Accept' => '*/*',
211213
]);
212214
if ($this->downloader($client, $file,$bar)) {
@@ -237,10 +239,11 @@ public function FilesCheckerhash() {
237239
$bar->display();
238240
foreach ($this->filesList as $file) {
239241
global $shouldExit;
242+
global $DOWNLOAD_DIR;
240243
if ($shouldExit) {
241244
break;
242245
}
243-
if (!file_exists(DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash)){
246+
if (!file_exists($DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash)){
244247
$this->Missfile[] = new BMCLAPIFile(
245248
$file->path,
246249
$file->hash,
@@ -249,7 +252,7 @@ public function FilesCheckerhash() {
249252
);
250253
}
251254
else{
252-
if (hash_file('sha1',DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash) != $file->hash) {
255+
if (hash_file('sha1',$DOWNLOAD_DIR.'/'.substr($file->hash, 0, 2).'/'.$file->hash) != $file->hash) {
253256
$this->Missfile[] = new BMCLAPIFile(
254257
$file->path,
255258
$file->hash,

main.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
declare(ticks=1)
66
require './config.php';
77
const VERSION = '1.10.3';
8-
const DOWNLOAD_DIR = "./cache";
8+
global $DOWNLOAD_DIR;
9+
$DOWNLOAD_DIR = $config['cache_dir'];
10+
const USERAGENT = 'openbmclapi-cluster/' . VERSION . ' ' . 'PHP-OpenBmclApi/0.0.1-dev';
911
if ($config['Staging'] == true){
1012
define('OPENBMCLAPI', 'openbmclapi.staging.bangbang93.com');
1113
}

0 commit comments

Comments
 (0)