perf: 优化删除临时文件

This commit is contained in:
kuaifan 2024-10-30 15:41:33 +08:00
parent 1a7591314f
commit b03fb9f1de
4 changed files with 87 additions and 120 deletions

View File

@ -1401,7 +1401,7 @@ class SystemController extends AbstractController
if ($isMain || $isApp) { if ($isMain || $isApp) {
$path = 'js/build/'; $path = 'js/build/';
$list = Base::readDir(public_path($path), false); $list = Base::recursiveFiles(public_path($path), false);
foreach ($list as $item) { foreach ($list as $item) {
if (is_file($item) && filesize($item) > 50 * 1024) { if (is_file($item) && filesize($item) > 50 * 1024) {
$array[] = $path . basename($item); $array[] = $path . basename($item);

View File

@ -155,7 +155,7 @@ class IndexController extends InvokeController
Task::deliver(new DeleteTmpTask('task_worker', 12)); Task::deliver(new DeleteTmpTask('task_worker', 12));
Task::deliver(new DeleteTmpTask('tmp')); Task::deliver(new DeleteTmpTask('tmp'));
Task::deliver(new DeleteTmpTask('file')); Task::deliver(new DeleteTmpTask('file'));
Task::deliver(new DeleteTmpTask('file_pack')); Task::deliver(new DeleteTmpTask('tmp_file', 24));
// 删除机器人消息 // 删除机器人消息
Task::deliver(new DeleteBotMsgTask()); Task::deliver(new DeleteBotMsgTask());
// 周期任务 // 周期任务
@ -205,15 +205,22 @@ class IndexController extends InvokeController
$uploadSuccessFileNum = $uploadSuccessFileNum + 1; $uploadSuccessFileNum = $uploadSuccessFileNum + 1;
Cache::set($publishVersion, $uploadSuccessFileNum, 7200); Cache::set($publishVersion, $uploadSuccessFileNum, 7200);
} }
if ($uploadSuccessFileNum >= $fileNum){ if ($uploadSuccessFileNum >= $fileNum) {
$directoryPath = public_path("uploads/desktop"); // 删除旧版本
$files = array_filter(scandir($directoryPath), function($file) use($directoryPath) { $dirs = Base::recursiveDirs(public_path("uploads/desktop"), false);
return preg_match("/^\d+\.\d+\.\d+$/", $file) && is_dir($directoryPath . '/' . $file) && $file != '.' && $file != '..'; sort($dirs);
}); $num = 0;
sort($files); foreach ($dirs as $dir) {
foreach ($files as $key => $file) { if (!preg_match("/\/\d+\.\d+\.\d+$/", $dir)) {
if ($file != $publishVersion && $key < count($files) - 2) { continue;
Base::deleteDirAndFile($directoryPath . '/' . $file); }
$num++;
if ($num < 2) {
continue;
}
$time = filemtime($dir);
if ($time < time() - 3600 * 24 * 30) {
Base::deleteDirAndFile($dir);
} }
} }
} }
@ -224,7 +231,7 @@ class IndexController extends InvokeController
if (preg_match("/^\d+\.\d+\.\d+$/", $name)) { if (preg_match("/^\d+\.\d+\.\d+$/", $name)) {
$path = "uploads/desktop/{$name}"; $path = "uploads/desktop/{$name}";
$dirPath = public_path($path); $dirPath = public_path($path);
$lists = Base::readDir($dirPath); $lists = Base::recursiveFiles($dirPath, false);
$files = []; $files = [];
foreach ($lists as $file) { foreach ($lists as $file) {
if (str_ends_with($file, '.yml') || str_ends_with($file, '.yaml') || str_ends_with($file, '.blockmap')) { if (str_ends_with($file, '.yml') || str_ends_with($file, '.yaml') || str_ends_with($file, '.blockmap')) {
@ -252,7 +259,7 @@ class IndexController extends InvokeController
} }
} }
} }
return abort(404); abort(404);
} }
/** /**
@ -363,85 +370,4 @@ class IndexController extends InvokeController
{ {
return '<!-- Deprecated -->'; return '<!-- Deprecated -->';
} }
/**
* 提取所有中文
* @return array|string
*/
public function allcn()
{
if (!Base::is_internal_ip(Base::getIp())) {
// 限制内网访问
return "Forbidden Access";
}
$list = Base::readDir(resource_path());
$array = [];
foreach ($list as $item) {
$content = file_get_contents($item);
preg_match_all("/\\\$L\((.*?)\)/", $content, $matchs);
if ($matchs) {
foreach ($matchs[1] as $text) {
$array[trim(trim($text, '"'), "'")] = trim(trim($text, '"'), "'");
}
}
}
return array_values($array);
}
/**
* 提取所有中文
* @return array|string
*/
public function allcn__php()
{
if (!Base::is_internal_ip(Base::getIp())) {
// 限制内网访问
return "Forbidden Access";
}
$list = Base::readDir(app_path());
$array = [];
foreach ($list as $item) {
$content = file_get_contents($item);
preg_match_all("/(retSuccess|retError|ApiException)\((.*?)[,|)]/", $content, $matchs);
if ($matchs) {
foreach ($matchs[2] as $text) {
$array[trim(trim($text, '"'), "'")] = trim(trim($text, '"'), "'");
}
}
}
return array_values($array);
}
/**
* 提取所有中文
* @return array|string
*/
public function allcn__all()
{
if (!Base::is_internal_ip(Base::getIp())) {
// 限制内网访问
return "Forbidden Access";
}
$list = array_merge(Base::readDir(app_path()), Base::readDir(resource_path()));
$array = [];
foreach ($list as $item) {
if (Base::rightExists($item, ".php") || Base::rightExists($item, ".vue") || Base::rightExists($item, ".js")) {
$content = file_get_contents($item);
preg_match_all("/(['\"])(.*?)[\u{4e00}-\u{9fa5}\u{FE30}-\u{FFA0}]+([\s\S]((?!\n).)*)\\1/u", $content, $matchs);
if ($matchs) {
foreach ($matchs[0] as $text) {
$tmp = preg_replace("/\/\/(.*?)$/", "", $text);
$tmp = preg_replace("/\/\/(.*?)\n/", "", $tmp);
$tmp = str_replace("", "", $tmp);
if (!preg_match("/[\u{4e00}-\u{9fa5}\u{FE30}-\u{FFA0}]/u", $tmp)){
continue; // 没有中文
}
$val = trim(trim($text, '"'), "'");
$array[md5($val)] = $val;
}
}
}
}
return implode("\n", array_values($array));
}
} }

View File

@ -2652,27 +2652,71 @@ class Base
} }
/** /**
* 遍历获取文件 * 路径拼接
* @param ...$segments
* @return string
*/
public static function joinPath(...$segments)
{
$trimmedSegments = array_map(function ($segment) {
return trim($segment, DIRECTORY_SEPARATOR);
}, $segments);
return implode(DIRECTORY_SEPARATOR, $trimmedSegments);
}
/**
* 递归获取所有文件
* @param $dir * @param $dir
* @param bool $subdirectory 是否遍历子目录 * @param bool|int $recursive
* @return array * @return array
*/ */
public static function readDir($dir, $subdirectory = true) public static function recursiveFiles($dir, $recursive = true)
{ {
$files = array(); if ($recursive && is_numeric($recursive)) {
$dir_list = scandir($dir); $recursive--;
foreach ($dir_list as $file) { }
if ($file != '..' && $file != '.') { $array = [];
if (is_dir($dir . '/' . $file)) { $items = scandir($dir);
if ($subdirectory) { foreach ($items as $item) {
$files = array_merge($files, self::readDir($dir . '/' . $file, $subdirectory)); if ($item != '..' && $item != '.') {
$itemPath = self::joinPath($dir, $item);
if (is_dir($itemPath)) {
if ($recursive) {
$array = array_merge($array, self::recursiveFiles($itemPath, $recursive));
} }
} else { } else {
$files[] = $dir . "/" . $file; $array[] = $itemPath;
} }
} }
} }
return $files; return $array;
}
/**
* 递归获取所有目录
* @param $dir
* @param bool|int $recursive
* @return array
*/
public static function recursiveDirs($dir, $recursive = true)
{
if ($recursive && is_numeric($recursive)) {
$recursive--;
}
$array = [];
$items = scandir($dir);
foreach ($items as $item) {
if ($item != '..' && $item != '.') {
$itemPath = self::joinPath($dir, $item);
if (is_dir($itemPath)) {
$array[] = $itemPath;
if ($recursive) {
$array = array_merge($array, self::recursiveDirs($itemPath, $recursive));
}
}
}
}
return $array;
} }
/** /**

View File

@ -6,8 +6,8 @@ use App\Models\File;
use App\Models\TaskWorker; use App\Models\TaskWorker;
use App\Models\Tmp; use App\Models\Tmp;
use App\Models\WebSocketTmpMsg; use App\Models\WebSocketTmpMsg;
use App\Module\Base;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Support\Facades\File as SupportFile;
/** /**
* 删除过期临时数据任务 * 删除过期临时数据任务
@ -99,22 +99,19 @@ class DeleteTmpTask extends AbstractTask
break; break;
/** /**
* file_pack 临时压缩下载文件 * tmp_file 删除临时文件
*/ */
case 'file_pack': case 'tmp_file':
{ {
$path = public_path('tmp/file/'); $day = intval(env("AUTO_EMPTY_TEMP_FILE", 30));
if (!SupportFile::exists($path)) { if ($day <= 0) {
return; return;
} }
$dirIterator = new \RecursiveDirectoryIterator($path); $files = Base::recursiveFiles(public_path('uploads/tmp'));
$iterator = new \RecursiveIteratorIterator($dirIterator); foreach ($files as $file) {
foreach ($iterator as $file) { $time = filemtime($file);
if ($file->isFile()) { if ($time < time() - 3600 * 24 * $day) {
$time = $file->getMTime(); unlink($file);
if ($time < time() - 3600 * 24) {
unlink($file->getPathname());
}
} }
} }
} }