diff --git a/README.md b/README.md index 5e3ca69..a5f0062 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ - `/jm download ` 或 `/jm 下载 `:下载、归档,并按配置触发 Komga 扫描。 - `/jm status`:查看当前归档与 Komga 配置状态。 - `/jm scan`:手动触发一次 Komga 扫描。 +- `/jm cleanup`:清理超过 `cleanup_retention_days` 的临时工作目录。 ## 归档结构 @@ -27,6 +28,8 @@ CBZ 内部会包含: 当 `archive_format` 设置为 `pdf` 时,插件会按 `pdf_chunk_size` 分块生成临时 PDF,再合并为最终文件,避免大本子转换时一次性占用过多内存。 +归档成功后,插件默认删除本次临时下载目录;归档库中的 CBZ/PDF 不会被自动删除。历史临时目录可通过 `/jm cleanup` 清理。 + ## Komga 联动 在 AstrBot 插件配置里填写: diff --git a/_conf_schema.json b/_conf_schema.json index b7df1c1..cfeb0f1 100644 --- a/_conf_schema.json +++ b/_conf_schema.json @@ -35,6 +35,11 @@ "type": "bool", "default": true }, + "cleanup_retention_days": { + "description": "手动清理临时目录时保留最近多少天的工作目录;设为 0 会清理全部历史工作目录。", + "type": "int", + "default": 7 + }, "download_timeout_seconds": { "description": "单个下载任务的超时时间,单位秒。", "type": "int", diff --git a/main.py b/main.py index 8b316b0..2a77bad 100644 --- a/main.py +++ b/main.py @@ -98,6 +98,7 @@ class JMDownloadPlugin(Star): f"下载重试次数:{self.settings.download_retry_count}", f"进度提示间隔:{self.settings.progress_interval_seconds} 秒", f"发送归档文件:{self.settings.send_archive_file}", + f"临时目录保留:{self.settings.cleanup_retention_days} 天", f"Komga 自动扫描:{self.settings.komga.enabled}", f"Komga 配置完整:{self.settings.komga.can_scan}", f"Komga 地址:{self.settings.komga.base_url or '未配置'}", @@ -119,6 +120,23 @@ class JMDownloadPlugin(Star): yield event.plain_result(result.message) event.stop_event() + @jm.command("cleanup") + async def cleanup(self, event: AstrMessageEvent): + """Cleanup old JMDownload work directories.""" + if self._deny_non_admin(event): + yield event.plain_result("JMDownload 当前配置仅允许管理员执行清理命令。") + event.stop_event() + return + + try: + removed = await asyncio.to_thread(self.downloader.cleanup_old_work_dirs) + except JMDownloadError as exc: + yield event.plain_result(f"清理失败:{exc.user_message}") + event.stop_event() + return + yield event.plain_result(f"清理完成:删除 {removed} 个临时工作目录。") + event.stop_event() + def _deny_non_admin(self, event: AstrMessageEvent) -> bool: return bool(self.settings.admin_only and not event.is_admin()) diff --git a/services/archive.py b/services/archive.py index a315b7d..b2c2826 100644 --- a/services/archive.py +++ b/services/archive.py @@ -58,10 +58,15 @@ class ArchiveService: if tmp_path.exists(): tmp_path.unlink() - if self.settings.archive_format == "cbz": - self._create_cbz(tmp_path, downloaded, series_name, book_name) - else: - self._create_pdf(tmp_path, downloaded) + try: + if self.settings.archive_format == "cbz": + self._create_cbz(tmp_path, downloaded, series_name, book_name) + else: + self._create_pdf(tmp_path, downloaded) + except Exception: + if tmp_path.exists(): + tmp_path.unlink() + raise os.replace(tmp_path, target_path) return ArchiveResult( diff --git a/services/download.py b/services/download.py index c3885a8..dee9817 100644 --- a/services/download.py +++ b/services/download.py @@ -2,6 +2,7 @@ from __future__ import annotations import asyncio import shutil +import time from collections.abc import Awaitable, Callable from dataclasses import dataclass from datetime import datetime @@ -124,8 +125,31 @@ class JMDownloadService: def cleanup(self, downloaded: DownloadedComic) -> None: if not self.settings.delete_work_dir_after_archive: return - if downloaded.work_dir.exists(): - shutil.rmtree(downloaded.work_dir) + self._remove_work_dir(downloaded.work_dir) + + def cleanup_old_work_dirs(self, retention_days: int | None = None) -> int: + retention_days = self.settings.cleanup_retention_days if retention_days is None else retention_days + if not self.settings.work_root.exists(): + return 0 + + threshold = time.time() - retention_days * 86400 + removed = 0 + for child in self.settings.work_root.iterdir(): + if not child.is_dir(): + continue + if retention_days > 0 and child.stat().st_mtime >= threshold: + continue + self._remove_work_dir(child) + removed += 1 + return removed + + def _remove_work_dir(self, path: Path) -> None: + work_root = self.settings.work_root.resolve() + target = path.resolve() + if target == work_root or work_root not in target.parents: + raise DownloadError(f"拒绝清理工作目录之外的路径:{target}") + if target.exists(): + shutil.rmtree(target) def _download_sync(self, comic_id: str, config_path: Path) -> str: try: diff --git a/services/settings.py b/services/settings.py index 4659eb1..2d9c3de 100644 --- a/services/settings.py +++ b/services/settings.py @@ -56,6 +56,7 @@ class PluginSettings: send_archive_file: bool = False overwrite_existing: bool = False delete_work_dir_after_archive: bool = True + cleanup_retention_days: int = 7 download_timeout_seconds: int = 3600 download_retry_count: int = 2 progress_interval_seconds: int = 30 @@ -97,6 +98,10 @@ class PluginSettings: delete_work_dir_after_archive=bool( config.get("delete_work_dir_after_archive", True), ), + cleanup_retention_days=max( + 0, + _int_value(config.get("cleanup_retention_days"), 7), + ), download_timeout_seconds=max( 60, _int_value(config.get("download_timeout_seconds"), 3600),