Browse Source

main bot scan stats save

master
filesite 6 days ago
parent
commit
f8dc4c2c2a
  1. 70
      themes/beauty/controller/CommandController.php

70
themes/beauty/controller/CommandController.php

@ -7,6 +7,7 @@ require_once __DIR__ . '/../../../plugins/Parsedown.php';
require_once __DIR__ . '/../../../plugins/Common.php'; require_once __DIR__ . '/../../../plugins/Common.php';
Class CommandController extends Controller { Class CommandController extends Controller {
protected $logPrefix = '[MainBot]';
public function actionIndex() { public function actionIndex() {
$commands = <<<eof $commands = <<<eof
@ -100,7 +101,7 @@ eof;
//服务器端机器人程序,负责图片、视频文件拍摄时间等信息扫描,并缓存结果供前端使用 //服务器端机器人程序,负责图片、视频文件拍摄时间等信息扫描,并缓存结果供前端使用
public function actionMainBot() { public function actionMainBot() {
$thisTime = date('Y-m-d H:i:s'); $thisTime = date('Y-m-d H:i:s');
$botLogPrefix = '[MainBot]'; $botLogPrefix = $this->logPrefix;
echo "{$botLogPrefix} Main bot started @{$thisTime}\n"; echo "{$botLogPrefix} Main bot started @{$thisTime}\n";
@ -120,6 +121,9 @@ eof;
} }
//扫描媒体文件:图片、视频、音乐
//TODO: 把它们按年份、月份归类,并缓存到/runtime/cache/目录,方便前端展示读取
//把当前扫描进度保存到单独的缓存文件,便于用户随时获取
protected function scanMediaFiles($dirpath = '') { protected function scanMediaFiles($dirpath = '') {
$rootDir = __DIR__ . '/../../../www/' . FSC::$app['config']['content_directory']; $rootDir = __DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'];
if (empty($dirpath)) { if (empty($dirpath)) {
@ -144,6 +148,9 @@ eof;
$cacheKey = 'MainBotScanedDirs'; $cacheKey = 'MainBotScanedDirs';
if (!empty($scanResults)) { if (!empty($scanResults)) {
$scanIndex = 0;
$scanTotal = count($scanResults);
foreach ($scanResults as $id => $item) { foreach ($scanResults as $id => $item) {
$hadScanedDirs = Common::getCache($cacheKey); $hadScanedDirs = Common::getCache($cacheKey);
@ -153,22 +160,77 @@ eof;
&& in_array($item['extension'], $supportedImageExts) && in_array($item['extension'], $supportedImageExts)
&& !in_array($item['extension'], $scanner->exifSupportFileExtensions) && !in_array($item['extension'], $scanner->exifSupportFileExtensions)
) { ) {
echo "Image file no original_ctime: {$item['filename']}.{$item['extension']}, {$item['realpath']}\n"; //echo "Image file no original_ctime: {$item['filename']}.{$item['extension']}, {$item['realpath']}\n";
}else if ( }else if (
!empty($item['filename']) !empty($item['filename'])
&& empty($item['original_ctime']) && empty($item['original_ctime'])
&& (in_array($item['extension'], $supportedVideoExts) || in_array($item['extension'], $supportedAudioExts)) && (in_array($item['extension'], $supportedVideoExts) || in_array($item['extension'], $supportedAudioExts))
) { ) {
echo "Video or audio file no original_ctime: {$item['filename']}.{$item['extension']}, {$item['realpath']}\n"; //echo "Video or audio file no original_ctime: {$item['filename']}.{$item['extension']}, {$item['realpath']}\n";
}else if (!empty($item['directory']) && empty($hadScanedDirs[$id])) { //if it's directory }else if (!empty($item['directory']) && empty($hadScanedDirs[$id])) { //if it's directory
$hadScanedDirs[$id] = true; $hadScanedDirs[$id] = true;
Common::setCache($cacheKey, $hadScanedDirs); Common::setCache($cacheKey, $hadScanedDirs);
sleep(1); //sleep(1);
$this->scanMediaFiles($item['realpath']); $this->scanMediaFiles($item['realpath']);
} }
$scanIndex ++;
$stats = $this->updateScanStats($dirpath, $scanTotal, $scanIndex);
}
sleep(1);
}
}
//更新扫描进度
protected function updateScanStats($dirpath, $total, $index) {
if (empty($total)) {return false;}
$stats = array(
'currentDir' => $dirpath,
'total' => $total,
'current' => $index,
'percent' => round($index/$total, 2) * 100,
);
$botLogPrefix = $this->logPrefix;
$cacheDir = __DIR__ . '/../../../runtime/cache/';
$statsFile = "{$cacheDir}stats_scan.json";
if (!is_dir($cacheDir)) {
mkdir($cacheDir, 0777, true);
}
$rootDir = __DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'];
if ($dirpath == $rootDir) {
echo "{$botLogPrefix} {$dirpath} scan has finished {$stats['percent']}%, total {$stats['total']}, current {$stats['current']}\n";
//保存进度文件
file_put_contents($statsFile, json_encode($stats) . "\n");
}else if (file_exists($statsFile)) { //更新当前扫描目录
$json = file_get_contents($statsFile);
if (!empty($json)) {
$jsonData = json_decode(trim($json), true);
if ($jsonData['currentDir'] != $dirpath) {
$jsonData['currentDir'] = $dirpath;
$json = json_encode($jsonData) . "\n";
file_put_contents($statsFile, $json);
}
} }
} }
return $stats;
}
public function actionTest() {
$cacheKey = 'TestSTData';
$time = Common::getCache($cacheKey);
if (empty($time)) {
$time = date('Y-m-d H:i:s');
Common::setCache($cacheKey, $time);
}
echo "Cache time {$time}\n";
} }
} }

Loading…
Cancel
Save