From 160c736b38f691cf9bec101e54677be80f11b4ba Mon Sep 17 00:00:00 2001 From: kuaifan Date: Sat, 25 Mar 2023 23:25:50 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E8=87=AA=E5=8A=A8=E6=B8=85=E7=A9=BA?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=9B=9E=E6=94=B6=E7=AB=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/IndexController.php | 3 +- app/Models/File.php | 21 ++++++++++++ app/Models/FileContent.php | 16 +++++++++ app/Tasks/DeleteTmpTask.php | 42 ++++++++++++++++++++---- 4 files changed, 74 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/IndexController.php b/app/Http/Controllers/IndexController.php index ee80fa817..b2a19105c 100755 --- a/app/Http/Controllers/IndexController.php +++ b/app/Http/Controllers/IndexController.php @@ -193,7 +193,8 @@ class IndexController extends InvokeController // 删除过期的临时表数据 Task::deliver(new DeleteTmpTask('wg_tmp_msgs', 1)); Task::deliver(new DeleteTmpTask('task_worker', 12)); - Task::deliver(new DeleteTmpTask('tmp', 24)); + Task::deliver(new DeleteTmpTask('tmp')); + Task::deliver(new DeleteTmpTask('file')); // 删除机器人消息 Task::deliver(new DeleteBotMsgTask()); // 周期任务 diff --git a/app/Models/File.php b/app/Models/File.php index 48830485f..c38daa603 100644 --- a/app/Models/File.php +++ b/app/Models/File.php @@ -287,6 +287,27 @@ class File extends AbstractModel return true; } + /** + * 强制删除文件 + * @return true + */ + public function forceDeleteFile() + { + AbstractModel::transaction(function () { + $this->forceDelete(); + FileContent::withTrashed() + ->whereFid($this->id) + ->orderBy('id') + ->chunk(500, function ($contents) { + /** @var FileContent $content */ + foreach ($contents as $content) { + $content->forceDeleteContent(); + } + }); + }); + return true; + } + /** * 获取文件分享链接 * @param $userid diff --git a/app/Models/FileContent.php b/app/Models/FileContent.php index 0bddedf14..d6f46b616 100644 --- a/app/Models/FileContent.php +++ b/app/Models/FileContent.php @@ -39,6 +39,22 @@ class FileContent extends AbstractModel { use SoftDeletes; + /** + * 强制删除文件内容 + * @return void + */ + public function forceDeleteContent() + { + $this->forceDelete(); + $content = Base::json2array($this->content ?: []); + if (str_starts_with($content['url'], 'uploads/')) { + $path = public_path($content['url']); + if (file_exists($path)) { + @unlink($path); + } + } + } + /** * 转预览地址 * @param array $array diff --git a/app/Tasks/DeleteTmpTask.php b/app/Tasks/DeleteTmpTask.php index 057dedd28..42f6a9c6a 100644 --- a/app/Tasks/DeleteTmpTask.php +++ b/app/Tasks/DeleteTmpTask.php @@ -2,6 +2,7 @@ namespace App\Tasks; +use App\Models\File; use App\Models\TaskWorker; use App\Models\Tmp; use App\Models\WebSocketTmpMsg; @@ -17,7 +18,11 @@ class DeleteTmpTask extends AbstractTask protected $data; protected $hours; // 多久后删除,单位小时 - public function __construct(string $data, int $hours) + /** + * @param string $data + * @param int $hours + */ + public function __construct(string $data, int $hours = 24) { parent::__construct(...func_get_args()); $this->data = $data; @@ -28,13 +33,14 @@ class DeleteTmpTask extends AbstractTask { switch ($this->data) { /** - * 表pre_wg_tmp_msgs + * 表pre_tmp_msgs */ case 'wg_tmp_msgs': { - WebSocketTmpMsg::where('created_at', '<', Carbon::now()->subHours($this->hours)->toDateTimeString()) + WebSocketTmpMsg::where('created_at', '<', Carbon::now()->subHours($this->hours)) ->orderBy('id') ->chunk(500, function ($msgs) { + /** @var WebSocketTmpMsg $msg */ foreach ($msgs as $msg) { $msg->delete(); } @@ -43,13 +49,14 @@ class DeleteTmpTask extends AbstractTask break; /** - * 表pre_wg_tmp + * 表pre_tmp */ case 'tmp': { - Tmp::where('created_at', '<', Carbon::now()->subHours($this->hours)->toDateTimeString()) + Tmp::where('created_at', '<', Carbon::now()->subHours($this->hours)) ->orderBy('id') - ->chunk(2000, function ($tmps) { + ->chunk(500, function ($tmps) { + /** @var Tmp $tmp */ foreach ($tmps as $tmp) { $tmp->delete(); } @@ -63,11 +70,32 @@ class DeleteTmpTask extends AbstractTask case 'task_worker': { TaskWorker::onlyTrashed() - ->where('deleted_at', '<', Carbon::now()->subHours($this->hours)->toDateTimeString()) + ->where('deleted_at', '<', Carbon::now()->subHours($this->hours)) ->orderBy('id') ->forceDelete(); } break; + + /** + * 表pre_file + */ + case 'file': + { + $day = intval(env("AUTO_EMPTY_FILE_RECYCLE", 365)); + if ($day <= 0) { + return; + } + File::onlyTrashed() + ->where('deleted_at', '<', Carbon::now()->addDays($day)) + ->orderBy('id') + ->chunk(500, function ($files) { + /** @var File $file */ + foreach ($files as $file) { + $file->forceDeleteFile(); + } + }); + } + break; } }