@6starlong/dsh-disk-cleanup 0.0.0-stage → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,68 @@
1
- # Temporary Holding Version
1
+ # @6starlong/dsh-disk-cleanup
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![npm](https://img.shields.io/npm/v/@6starlong/dsh-disk-cleanup?color=cb3837)](https://www.npmjs.com/package/@6starlong/dsh-disk-cleanup) [![downloads](https://img.shields.io/npm/dm/@6starlong/dsh-disk-cleanup?color=4c1)](https://www.npmjs.com/package/@6starlong/dsh-disk-cleanup) [![license](https://img.shields.io/npm/l/@6starlong/dsh-disk-cleanup?color=007ec6)](https://www.npmjs.com/package/@6starlong/dsh-disk-cleanup)
4
+
5
+ 临时文件集中写到缓存目录并自动过期回收,工作区只留用户要的文件。
6
+
7
+ 模型写出的探针脚本默认落在工作区内。这个插件给 shell 命令注入临时目录变量、补齐 scratch 目录的写入权限、在系统提示里说明临时文件该写哪里,并按保留天数清理过期文件与无主附件。不改写工具入参的路径:写哪里由提示词引导,不由插件替模型决定。
8
+
9
+ ## 它做什么
10
+
11
+ | 机制 | 做法 |
12
+ |---|---|
13
+ | shell 环境注入 | 在 `pwsh` / `bash` 命令串前设置 `TMP` / `TEMP` / `TMPDIR`,指向 `<DSH_HOME>/cache/scratch` |
14
+ | 权限补齐 | 注册会话可写根,并给 scratch 补访问控制项,让受限子进程也能写 |
15
+ | 系统提示 | 点明临时文件写 scratch、用户文件留工作区;路径每次现算,只读会话不出这段 |
16
+ | 过期清理 | 按保留天数清 scratch 过期条目与请求图片缓存,回收无主附件,加载时与之后定期各扫一次 |
17
+
18
+ ## 临时文件写哪
19
+
20
+ | 权限模式 | 位置 |
21
+ |---|---|
22
+ | workspace-write / danger-full-access | `<DSH_HOME>/cache/scratch` |
23
+ | read-only | 不启用 |
24
+ | 配置了 `scratchDir` | 配置指定的目录,不参与自动清理 |
25
+
26
+ `<DSH_HOME>` 默认 `~/.dsh`,跟随 `DSH_HOME` 环境变量;所有可写模式共用同一位置。workspace-write 下插件在会话创建时就注册可写根(文件工具)与访问控制项(子进程);非 Windows 只做前者,任一步失败退到 `<工作区>/cache/scratch`。
27
+
28
+ ## 过期清理
29
+
30
+ - **删什么**:`cache/scratch` 下最后修改时间早于分界线的顶层条目(整目录删),以及 scratch 内的空目录、`<DSH_HOME>` 下一级的空目录。
31
+ - **附件**:请求图片缓存按同样的保留天数清;原件先扫全部会话,只删零引用且过期的。会话读不全或没提取到引用时整批跳过,首次运行只统计不删。
32
+ - **何时清**:插件加载时一次,之后每 6 小时最多一次。
33
+ - 配置里自己指定 `scratchDir` 时,那个目录不自动清。
34
+
35
+ 「过期清理」配置区可以关掉自动清理、调整保留天数(1–365,留空恢复默认 3 天)。
36
+
37
+ ## 安装
38
+
39
+ 在 DSH 的插件管理页里安装,或者用命令行装:
40
+
41
+ ```powershell
42
+ dsh plugin --profile <profile> add @6starlong/dsh-disk-cleanup
43
+ ```
44
+
45
+ 安装后重启 DSH 生效。
46
+
47
+ ## 配置
48
+
49
+ 「过期清理」配置区只管 `autoCleanup` 与 `cleanupMaxAgeDays`;其余字段在 profile 的 `cordis.patch.yml` 里覆盖:
50
+
51
+ ```yaml
52
+ - id: dsh-disk-cleanup
53
+ name: '@6starlong/dsh-disk-cleanup'
54
+ config:
55
+ enabled: true
56
+ scratchDir: '' # 留空 = 默认暂存目录并自动清理
57
+ injectShellTemp: true # pwsh/bash 的 TMP/TEMP 指向 scratch
58
+ grantProcessWrite: true # 给 scratch 补访问控制项
59
+ promptSection: true # 往系统提示里加一段说明
60
+ autoCleanup: true
61
+ cleanupMaxAgeDays: 3 # 1–365
62
+ ```
63
+
64
+ ## 边界
65
+
66
+ - read-only 不注入、不建目录;清理照常。
67
+ - 只清 scratch 与 `<DSH_HOME>` 下一级空目录,系统临时目录里 DSH 自建的文件不接管。
68
+ - 单个条目被占用或读不到就跳过,不中断整批。
@@ -0,0 +1,24 @@
1
+ # dsh-disk-cleanup 组合包补丁:把插件行插入 profile。
2
+ # 本补丁按 id 覆盖;用户可在自己 profile 的 cordis.patch.yml 里覆盖本行整个 config。
3
+ # 「过期清理」配置区只改 autoCleanup 与 cleanupMaxAgeDays,其余字段在这里改。
4
+ #
5
+ # 示例(用户 profile 的 cordis.patch.yml):
6
+ # - id: dsh-disk-cleanup
7
+ # name: '@6starlong/dsh-disk-cleanup'
8
+ # config:
9
+ # enabled: true
10
+ # # scratchDir: '' # 留空 = 默认暂存目录并自动清理;自定义目录不清理
11
+ # injectShellTemp: true # pwsh/bash 的 TMP/TEMP 指向 scratch
12
+ # grantProcessWrite: true # 给 scratch 补访问控制项,让子进程也能写
13
+ # promptSection: true # 往系统提示里加一段 scratch 说明
14
+ # autoCleanup: true # 启动时清理 scratch(插件页也能改)
15
+ # cleanupMaxAgeDays: 3 # 保留天数(插件页也能改)
16
+ #
17
+ - insert:
18
+ - id: dsh-disk-cleanup
19
+ name: '@6starlong/dsh-disk-cleanup'
20
+ config:
21
+ enabled: true
22
+ injectShellTemp: true
23
+ grantProcessWrite: true
24
+ promptSection: true
package/icon.svg ADDED
@@ -0,0 +1,11 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" width="36" height="36" viewBox="0 0 36 36" fill="none">
2
+ <defs>
3
+ <linearGradient id="diskCleanupBlue" x1="6" y1="7" x2="30" y2="30" gradientUnits="userSpaceOnUse">
4
+ <stop stop-color="#69B9FF"/>
5
+ <stop offset="1" stop-color="#324DE2"/>
6
+ </linearGradient>
7
+ </defs>
8
+ <path d="M6 12.2A2.7 2.7 0 0 1 8.7 9.5h5.9l2.5 2.9h10.2a2.7 2.7 0 0 1 2.7 2.7v12.2a2.7 2.7 0 0 1-2.7 2.7H8.7a2.7 2.7 0 0 1-2.7-2.7V12.2Z" fill="url(#diskCleanupBlue)"/>
9
+ <circle cx="18" cy="21" r="6" fill="#FFFFFF"/>
10
+ <path d="M18 17.4v3.8l2.5 1.6" stroke="#324DE2" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/>
11
+ </svg>
package/lib/acl.js ADDED
@@ -0,0 +1,122 @@
1
+ /**
2
+ * 给受限子进程补写权限(仅 Windows)。
3
+ *
4
+ * workspace-write 模式下,子进程拿的是受限令牌,能写的只有工作区根和它自己的
5
+ * 私有临时目录。要让子进程也能写 scratch,必须把同一个工作区写标识加到该目录的
6
+ * 访问控制列表上——令牌里已经有这个标识,加上就能通过。
7
+ * @module dsh-disk-cleanup/acl
8
+ */
9
+ import { createRequire } from 'node:module'
10
+ import { realpathSync } from 'node:fs'
11
+ import { dirname, join } from 'node:path'
12
+ import { pathToFileURL } from 'node:url'
13
+
14
+ /** 内部包名。它只随 DSH 安装提供,不在 profile 的依赖里。 */
15
+ const ACL_PACKAGE = '@deepseek-ai/dsh-sandbox-windows-acl'
16
+
17
+ /**
18
+ * 按 DSH 的安装位置解析内部包。
19
+ * @returns {string | undefined} 包内入口的绝对路径;找不到时 undefined。
20
+ */
21
+ function resolveAclPackage() {
22
+ const installRoot = dirname(process.execPath)
23
+ const appRoot = join(installRoot, 'resources', 'app.asar', 'dsh', 'node_modules')
24
+ try {
25
+ const require = createRequire(join(appRoot, 'noop.js'))
26
+ return require.resolve(ACL_PACKAGE)
27
+ } catch {
28
+ return undefined
29
+ }
30
+ }
31
+
32
+ /** 已加载的包导出;null 表示确认不可用。 */
33
+ let aclModule
34
+
35
+ /**
36
+ * 加载 ACL 包(按需,只尝试一次)。
37
+ * @returns {Promise<object | undefined>} 包导出;不可用时 undefined。
38
+ */
39
+ async function loadAcl() {
40
+ if (aclModule !== undefined) return aclModule ?? undefined
41
+ if (process.platform !== 'win32') {
42
+ aclModule = null
43
+ return undefined
44
+ }
45
+ const entry = resolveAclPackage()
46
+ if (entry === undefined) {
47
+ aclModule = null
48
+ return undefined
49
+ }
50
+ try {
51
+ const mod = await import(pathToFileURL(entry).href)
52
+ const usable = typeof mod.AclWriteGrant?.create === 'function' && typeof mod.workspaceWriteSid === 'function'
53
+ aclModule = usable ? mod : null
54
+ } catch {
55
+ aclModule = null
56
+ }
57
+ return aclModule ?? undefined
58
+ }
59
+
60
+ /**
61
+ * 解析成符号链接展开后的物理路径(访问控制列表比较的就是它)。
62
+ * @param {string} target - 原始路径。
63
+ * @returns {string} 物理路径;解析失败时原样返回。
64
+ */
65
+ export function physicalPath(target) {
66
+ try {
67
+ return realpathSync.native(target)
68
+ } catch {
69
+ return target
70
+ }
71
+ }
72
+
73
+ /**
74
+ * 已经授权过的目录。键是工作区与目录的物理路径,同一工作区的多个会话共用一份授权。
75
+ * @type {Map<string, {grant: object}>}
76
+ */
77
+ const grants = new Map()
78
+
79
+ /**
80
+ * 给一个目录加上工作区写权限,让受限子进程能写进去。
81
+ *
82
+ * 幂等:同一组工作区与目录只建一次授权。
83
+ * 访问控制项按 standing 建,和 DSH 自己对可写目录的处理一致——撤销后下一个会话
84
+ * 就会被挡住,而这层授权本就是为了复用而存在,所以卸载时不撤销。revokeAll 只
85
+ * 释放授权对象持有的标识,不动目录上的访问控制项。
86
+ * @param {string} workspaceRoot - 会话工作区根。
87
+ * @param {string} directory - 需要可写的目录。
88
+ * @returns {Promise<boolean>} 是否可用(Windows 之外或内部包不可用时为 false)。
89
+ */
90
+ export async function grantProcessWrite(workspaceRoot, directory) {
91
+ const acl = await loadAcl()
92
+ if (acl === undefined) return false
93
+ const root = physicalPath(workspaceRoot)
94
+ const dir = physicalPath(directory)
95
+ const key = JSON.stringify([root, dir])
96
+ if (grants.has(key)) return true
97
+ try {
98
+ const grant = acl.AclWriteGrant.create(acl.workspaceWriteSid(root))
99
+ grant.add(dir, true)
100
+ grants.set(key, { grant })
101
+ return true
102
+ } catch {
103
+ return false
104
+ }
105
+ }
106
+
107
+ /**
108
+ * 释放全部授权对象。插件卸载时调用;单个失败不影响其余。
109
+ * 目录上的访问控制项是 standing 的,dispose 不会撤销它们(见 AclWriteGrant 的约定),
110
+ * 所以这里只是释放资源,不会让之后的会话失去写 scratch 的能力。
111
+ * @returns {void}
112
+ */
113
+ export function revokeAll() {
114
+ for (const [key, entry] of grants) {
115
+ try {
116
+ entry.grant.dispose()
117
+ } catch {
118
+ /* 单个撤销失败不阻塞其余 */
119
+ }
120
+ grants.delete(key)
121
+ }
122
+ }
@@ -0,0 +1,310 @@
1
+ /**
2
+ * 附件残留清理:已删会话留下的原件,以及过期的请求图片缓存。
3
+ *
4
+ * 原件删了不可恢复,必须解压全部会话确认零引用才动;请求图片是随时能重建的派生图,
5
+ * 按最后修改时间过期即可。两者都只清超过保留期的条目。
6
+ * @module dsh-disk-cleanup/attachments
7
+ */
8
+ import { mkdir, readdir, readFile, rm, stat, writeFile } from 'node:fs/promises'
9
+ import { basename, join } from 'node:path'
10
+ import { zstdDecompressSync } from 'node:zlib'
11
+ import { cleanEmptyDirs, cutoffTime } from './cleanup.js'
12
+ import { dshHome } from './scratch.js'
13
+
14
+ /** 会话文件的后缀。 */
15
+ const SESSION_SUFFIX = '.zstd'
16
+ /** 会话里引用原件的字段形状。 */
17
+ const REF_PREFIX = Buffer.from('"attachmentId":"sha256:', 'utf8')
18
+ /** zstd 帧头魔数;会话可能由多个连续帧组成。 */
19
+ const ZSTD_MAGIC = Buffer.from([0x28, 0xb5, 0x2f, 0xfd])
20
+ /** 原件文件名:64 位十六进制。 */
21
+ const OBJECT_NAME = /^[0-9a-f]{64}$/
22
+ /** 首次扫描标记的位置。 */
23
+ const MARKER_DIR = ['cache', 'dsh-disk-cleanup']
24
+ const MARKER_FILE = 'attachments'
25
+
26
+ /**
27
+ * 列出目录下的所有文件,递归但不跟符号链接。
28
+ * @param {string} dir - 目录绝对路径。
29
+ * @returns {Promise<{files: string[], failed: number}>} 文件列表与读取失败数。
30
+ */
31
+ async function listFiles(dir) {
32
+ const files = []
33
+ let failed = 0
34
+ try {
35
+ const entries = await readdir(dir, { withFileTypes: true })
36
+ for (const entry of entries) {
37
+ const path = join(dir, entry.name)
38
+ if (entry.isDirectory()) {
39
+ const sub = await listFiles(path)
40
+ files.push(...sub.files)
41
+ failed += sub.failed
42
+ } else if (entry.isFile()) {
43
+ files.push(path)
44
+ }
45
+ }
46
+ } catch (error) {
47
+ if (error?.code !== 'ENOENT') failed += 1
48
+ }
49
+ return { files, failed }
50
+ }
51
+
52
+ /**
53
+ * 解压会话:按 zstd 帧头切出每个帧,逐帧解压后拼接。
54
+ * DSH 会话是多个连续帧,单帧解压只能拿到会话头,会漏掉消息里的图片引用。
55
+ * @param {Buffer} buffer - 会话原始字节。
56
+ * @returns {Buffer} 全部帧解压后的内容。
57
+ */
58
+ function decompressSession(buffer) {
59
+ const starts = []
60
+ let at = 0
61
+ while (true) {
62
+ const i = buffer.indexOf(ZSTD_MAGIC, at)
63
+ if (i < 0) break
64
+ starts.push(i)
65
+ at = i + ZSTD_MAGIC.length
66
+ }
67
+ if (starts.length <= 1) return zstdDecompressSync(buffer)
68
+ const parts = []
69
+ for (let k = 0; k < starts.length; k += 1) {
70
+ const start = starts[k]
71
+ const end = k + 1 < starts.length ? starts[k + 1] : buffer.length
72
+ parts.push(zstdDecompressSync(buffer.subarray(start, end)))
73
+ }
74
+ return Buffer.concat(parts)
75
+ }
76
+
77
+ /**
78
+ * 从解压后的会话内容里提取原件 id。
79
+ *
80
+ * 按字节找字段前缀,避免把整段会话转成字符串;损坏的 id 直接跳过。
81
+ * @param {Buffer} buffer - 会话内容。
82
+ * @param {Set<string>} ids - 累计的 id 集合。
83
+ * @returns {void}
84
+ */
85
+ function scanReferences(buffer, ids) {
86
+ let from = 0
87
+ while (from < buffer.length) {
88
+ const at = buffer.indexOf(REF_PREFIX, from)
89
+ if (at < 0) break
90
+ const start = at + REF_PREFIX.length
91
+ if (start + 64 <= buffer.length) {
92
+ const id = buffer.toString('ascii', start, start + 64)
93
+ if (OBJECT_NAME.test(id)) ids.add(id)
94
+ }
95
+ from = start
96
+ }
97
+ }
98
+
99
+ /**
100
+ * 解压全部会话,收集仍在被引用的原件 id。
101
+ *
102
+ * 会话按工作区分目录存放,所以要递归遍历;子会话同样可能持有附件引用,
103
+ * 不能按来源或目录名跳过。单个会话损坏不影响其余,读不到任何一个时
104
+ * 由调用方决定跳过清理,不能把「读不到」当成「没人用」。
105
+ * @param {string} dir - 会话根目录。
106
+ * @returns {Promise<{ids: Set<string>, sessions: number, readable: number}>} 引用集合与计数。
107
+ */
108
+ export async function collectReferencedIds(dir) {
109
+ const ids = new Set()
110
+ if (typeof dir !== 'string' || dir === '') return { ids, sessions: 0, readable: 0 }
111
+ let names
112
+ try {
113
+ names = await readdir(dir, { recursive: true })
114
+ } catch (error) {
115
+ if (error?.code !== 'ENOENT') {
116
+ // 读不到会话根时不返回空集合伪装成「零引用」,让调用方跳过。
117
+ return { ids, sessions: -1, readable: 0 }
118
+ }
119
+ return { ids, sessions: 0, readable: 0 }
120
+ }
121
+ let sessions = 0
122
+ let readable = 0
123
+ for (const name of names) {
124
+ if (typeof name !== 'string' || !name.endsWith(SESSION_SUFFIX)) continue
125
+ sessions += 1
126
+ try {
127
+ scanReferences(decompressSession(await readFile(join(dir, name))), ids)
128
+ readable += 1
129
+ } catch {
130
+ /* 单个会话损坏就跳过 */
131
+ }
132
+ }
133
+ return { ids, sessions, readable }
134
+ }
135
+
136
+ /**
137
+ * 按最后修改时间清理目录里的过期文件,并删掉因此变空的目录。
138
+ *
139
+ * 用于请求图片缓存:它是按固定参数重建的派生图,删掉不会丢原始内容。
140
+ * @param {object} options - 清理输入。
141
+ * @param {string} options.dir - 缓存根目录。
142
+ * @param {number} options.maxAgeDays - 保留天数。
143
+ * @param {boolean} [options.dryRun] - 只统计不删除。
144
+ * @param {number} [options.now] - 参照时刻(毫秒)。
145
+ * @returns {Promise<{removed: number, failed: number, kept: number}>} 统计结果。
146
+ */
147
+ export async function cleanExpiredFiles({ dir, maxAgeDays, dryRun = false, now = Date.now() }) {
148
+ const result = { removed: 0, failed: 0, kept: 0 }
149
+ if (typeof dir !== 'string' || dir === '') return result
150
+ const { files, failed } = await listFiles(dir)
151
+ result.failed += failed
152
+ const cutoff = cutoffTime(maxAgeDays, now)
153
+ for (const path of files) {
154
+ let info
155
+ try {
156
+ info = await stat(path)
157
+ } catch {
158
+ result.failed += 1
159
+ continue
160
+ }
161
+ if (info.mtimeMs >= cutoff) {
162
+ result.kept += 1
163
+ continue
164
+ }
165
+ if (dryRun) {
166
+ result.removed += 1
167
+ continue
168
+ }
169
+ try {
170
+ await rm(path, { force: true })
171
+ result.removed += 1
172
+ } catch {
173
+ result.failed += 1
174
+ }
175
+ }
176
+ const empty = await cleanEmptyDirs({ dir, recursive: true, dryRun })
177
+ result.removed += empty.removed
178
+ result.failed += empty.failed
179
+ result.kept += empty.kept
180
+ return result
181
+ }
182
+
183
+ /**
184
+ * 清理没有任何会话引用的原件。
185
+ *
186
+ * 会话有任何一个读不到就整批跳过:读不全等于引用集合不全,不能据此删图。
187
+ * 只有「零引用」且「最后修改时间早于分界线」的才进候选。
188
+ * @param {object} options - 清理输入。
189
+ * @param {string} options.dir - 原件根目录。
190
+ * @param {string} options.sessionsDir - 会话根目录。
191
+ * @param {number} options.maxAgeDays - 保留天数。
192
+ * @param {boolean} [options.dryRun] - 只统计不删除。
193
+ * @param {number} [options.now] - 参照时刻(毫秒)。
194
+ * @returns {Promise<{removed: number, failed: number, kept: number, candidates: number, skipped: boolean}>} 统计结果。
195
+ */
196
+ export async function cleanOrphanObjects({ dir, sessionsDir, maxAgeDays, dryRun = false, now = Date.now() }) {
197
+ const result = { removed: 0, failed: 0, kept: 0, candidates: 0, skipped: false }
198
+ if (typeof dir !== 'string' || dir === '') {
199
+ result.skipped = true
200
+ return result
201
+ }
202
+ const { ids, sessions, readable } = await collectReferencedIds(sessionsDir)
203
+ if (sessions <= 0 || readable < sessions) {
204
+ result.skipped = true
205
+ return result
206
+ }
207
+ // 会话都在却一个引用都没读出来,几乎一定是解析出了问题,
208
+ // 不能拿空集合当成「谁都没用」,否则会把全部过期原件删光。
209
+ if (ids.size === 0) {
210
+ result.skipped = true
211
+ return result
212
+ }
213
+ const cutoff = cutoffTime(maxAgeDays, now)
214
+ const { files, failed } = await listFiles(dir)
215
+ result.failed += failed
216
+ for (const path of files) {
217
+ let info
218
+ try {
219
+ info = await stat(path)
220
+ } catch {
221
+ result.failed += 1
222
+ continue
223
+ }
224
+ const id = basename(path)
225
+ if (ids.has(id) || info.mtimeMs >= cutoff) {
226
+ result.kept += 1
227
+ continue
228
+ }
229
+ result.candidates += 1
230
+ if (dryRun) {
231
+ result.removed += 1
232
+ continue
233
+ }
234
+ try {
235
+ await rm(path, { force: true })
236
+ result.removed += 1
237
+ } catch {
238
+ result.failed += 1
239
+ }
240
+ }
241
+ const empty = await cleanEmptyDirs({ dir, recursive: true, dryRun })
242
+ result.removed += empty.removed
243
+ result.failed += empty.failed
244
+ result.kept += empty.kept
245
+ return result
246
+ }
247
+
248
+ /** 首次扫描标记的完整路径。 */
249
+ function markerPath(home) {
250
+ return join(home, ...MARKER_DIR, MARKER_FILE)
251
+ }
252
+
253
+ /**
254
+ * 判断是不是首次清理附件。标记读不到时按「不是首次」处理,避免反复只统计不删。
255
+ * @param {string} home - DSH home。
256
+ * @returns {Promise<boolean>} 是首次时为 true。
257
+ */
258
+ async function isFirstScan(home) {
259
+ try {
260
+ await stat(markerPath(home))
261
+ return false
262
+ } catch (error) {
263
+ return error?.code === 'ENOENT'
264
+ }
265
+ }
266
+
267
+ /**
268
+ * 写下首次扫描标记。
269
+ * @param {string} home - DSH home。
270
+ * @returns {Promise<void>} 完成信号。
271
+ */
272
+ async function markScanned(home) {
273
+ try {
274
+ await mkdir(join(home, ...MARKER_DIR), { recursive: true })
275
+ await writeFile(markerPath(home), String(Date.now()), 'utf8')
276
+ } catch {
277
+ /* 标记写不进去不影响清理本身,至多下次再走一次首轮 */
278
+ }
279
+ }
280
+
281
+ /**
282
+ * 一次清理两个附件目录。
283
+ *
284
+ * 缓存按保留期直接清;原件首轮只统计,给用户一次核对的机会,之后才真删。
285
+ * @param {object} options - 清理输入。
286
+ * @param {string} [options.home] - DSH home。
287
+ * @param {number} options.maxAgeDays - 保留天数。
288
+ * @param {boolean} [options.dryRun] - 只统计不删除。
289
+ * @param {number} [options.now] - 参照时刻(毫秒)。
290
+ * @returns {Promise<{cache: object, objects: object, first: boolean}>} 两批统计与是否首轮。
291
+ */
292
+ export async function cleanAttachmentLeftovers({ home = dshHome(), maxAgeDays, dryRun = false, now = Date.now() }) {
293
+ const first = !dryRun && await isFirstScan(home)
294
+ const cache = await cleanExpiredFiles({
295
+ dir: join(home, 'cache', 'attachments', 'request-images'),
296
+ maxAgeDays,
297
+ dryRun,
298
+ now,
299
+ })
300
+ const objects = await cleanOrphanObjects({
301
+ dir: join(home, 'attachments', 'v1', 'objects'),
302
+ sessionsDir: join(home, 'sessions'),
303
+ maxAgeDays,
304
+ dryRun: dryRun || first,
305
+ now,
306
+ })
307
+ // 没有可清的东西就不落标记,免得只读会话也被创建一个缓存目录。
308
+ if (first && (objects.candidates > 0 || cache.removed > 0)) await markScanned(home)
309
+ return { cache, objects, first }
310
+ }