Source code of filesite.io. https://filesite.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
8.1 KiB

<?php
/**
* Command Controller
*/
2 weeks ago
require_once __DIR__ . '/../../../lib/DirScanner.php';
require_once __DIR__ . '/../../../plugins/Parsedown.php';
require_once __DIR__ . '/../../../plugins/Common.php';
Class CommandController extends Controller {
protected $logPrefix = '[MainBot]';
public function actionIndex() {
$commands = <<<eof
Actions:
- config [do=get&key=theme] //获取或修改系统配置
- mainBot //扫描机器人程序
Usage:
php command.php action parameters
eof;
echo $commands;
exit;
}
//查看、修改/runtime/custom_config.json里的内容
public function actionConfig() {
$themeName = FSC::$app['config']['theme'];
$code = 1;
$data = '';
//修改配置文件
$param_do = $this->get('do', 'set'); //支持:set, get, all, del
$param_key = $this->get('key', '');
$param_value = $this->get('val', '');
if ($param_do == 'set' && empty($param_value)) {
throw new Exception("缺少val参数!", 403);
}else if (in_array($param_do, array('set', 'get', 'del')) && empty($param_key)) {
throw new Exception("缺少key参数!", 403);
}
//val数据格式转换
if ($param_value === 'false') {
$param_value = false;
}else if ($param_value === 'true') {
$param_value = true;
}
$config_file = __DIR__ . "/../../../runtime/custom_config.json";
if (file_exists($config_file)) {
$content = file_get_contents($config_file);
$configs = @json_decode($content, true);
if (empty($configs)) {
$config_file_template = __DIR__ . "/../../../conf/custom_config_{$themeName}.json";
$content = file_get_contents($config_file_template);
$configs = @json_decode($content, true);
}
}
if (!empty($configs)) {
switch($param_do) {
case 'set':
$configs[$param_key] = $param_value;
file_put_contents($config_file, json_encode($configs, JSON_PRETTY_PRINT));
$data = $configs;
break;
case 'get':
$data = !empty($configs[$param_key]) ? $configs[$param_key] : '';
break;
case 'del':
unset($configs[$param_key]);
file_put_contents($config_file, json_encode($configs, JSON_PRETTY_PRINT));
$data = $configs;
break;
case 'all':
default:
$data = $configs;
break;
}
}
$res = compact('code', 'data');
echo "命令参数:\n";
print_r($this->get());
echo "\n";
echo "命令执行结果:\n";
print_r($res);
echo "\n\n";
exit;
}
//服务器端机器人程序,负责图片、视频文件拍摄时间等信息扫描,并缓存结果供前端使用
public function actionMainBot() {
$thisTime = date('Y-m-d H:i:s');
$botLogPrefix = $this->logPrefix;
2 weeks ago
echo "{$botLogPrefix} Main bot started @{$thisTime}\n";
$menus = array(); //菜单,一级目录
$htmlReadme = array(); //Readme.md 内容,底部网站详细介绍
$htmlCateReadme = ''; //当前目录下的Readme.md 内容
$menus_sorted = array(); //Readme_sort.txt 说明文件内容,一级目录菜单从上到下的排序
2 weeks ago
$this->scanMediaFiles();
2 weeks ago
//while (true) {
//$time = date('Y-m-d H:i:s');
//echo "{$botLogPrefix} {$time}\n";
//sleep(3);
//}
}
//扫描媒体文件:图片、视频、音乐
//TODO: 把它们按年份、月份归类,并缓存到/runtime/cache/目录,方便前端展示读取
//把当前扫描进度保存到单独的缓存文件,便于用户随时获取
2 weeks ago
protected function scanMediaFiles($dirpath = '') {
$rootDir = __DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'];
if (empty($dirpath)) {
$dirpath = $rootDir;
}
2 weeks ago
echo "\n\n== Scanning directory {$dirpath} ...\n\n";
$scanner = new DirScanner();
$scanner->setWebRoot(FSC::$app['config']['content_directory']);
$scanner->setRootDir($dirpath);
$maxScanDeep = 0; //最大扫描目录级数
$dirTree = $scanner->scan($dirpath, $maxScanDeep);
$scanResults = $scanner->getScanResults();
echo 'Total directories or files: ' . count($scanResults);
echo "\n\n";
$supportedImageExts = FSC::$app['config']['supportedImageExts'];
$supportedVideoExts = FSC::$app['config']['supportedVideoExts'];
$supportedAudioExts = FSC::$app['config']['supportedAudioExts'];
2 weeks ago
$cacheKey = 'MainBotScanedDirs';
if (!empty($scanResults)) {
$scanIndex = 0;
$scanTotal = count($scanResults);
2 weeks ago
foreach ($scanResults as $id => $item) {
$hadScanedDirs = Common::getCache($cacheKey);
if (
!empty($item['filename'])
&& empty($item['original_ctime'])
&& in_array($item['extension'], $supportedImageExts)
&& !in_array($item['extension'], $scanner->exifSupportFileExtensions)
) {
//echo "Image file no original_ctime: {$item['filename']}.{$item['extension']}, {$item['realpath']}\n";
2 weeks ago
}else if (
!empty($item['filename'])
&& empty($item['original_ctime'])
&& (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";
2 weeks ago
}else if (!empty($item['directory']) && empty($hadScanedDirs[$id])) { //if it's directory
$hadScanedDirs[$id] = true;
Common::setCache($cacheKey, $hadScanedDirs);
//sleep(1);
2 weeks ago
$this->scanMediaFiles($item['realpath']);
}
$scanIndex ++;
$stats = $this->updateScanStats($dirpath, $scanTotal, $scanIndex);
2 weeks ago
}
sleep(1);
2 weeks ago
}
}
//更新扫描进度
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";
}
}