micloud-cli 0.0.0-stage → 0.5.1
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/LICENSE +661 -0
- package/README.en.md +118 -0
- package/README.md +117 -2
- package/dist/cli.js +59 -0
- package/dist/commands/account.js +76 -0
- package/dist/core/account.js +22 -0
- package/dist/core/auth.js +338 -0
- package/dist/core/config.js +81 -0
- package/dist/core/http.js +172 -0
- package/dist/core/lock.js +90 -0
- package/dist/core/output.js +137 -0
- package/dist/core/template.js +39 -0
- package/dist/core/utils.js +205 -0
- package/dist/note/client.js +298 -0
- package/dist/note/commands/create.js +44 -0
- package/dist/note/commands/delete.js +32 -0
- package/dist/note/commands/export.js +20 -0
- package/dist/note/commands/folder.js +101 -0
- package/dist/note/commands/get.js +35 -0
- package/dist/note/commands/list.js +57 -0
- package/dist/note/commands/move.js +37 -0
- package/dist/note/commands/pin.js +41 -0
- package/dist/note/commands/search.js +45 -0
- package/dist/note/commands/shared.js +37 -0
- package/dist/note/commands/sync.js +217 -0
- package/dist/note/commands/update.js +61 -0
- package/dist/note/commands/upload-image.js +27 -0
- package/dist/note/config.js +166 -0
- package/dist/note/converter.js +664 -0
- package/dist/note/extra-info.js +23 -0
- package/dist/note/images.js +100 -0
- package/dist/note/register.js +172 -0
- package/dist/note/sync-diff.js +143 -0
- package/dist/note/sync.js +748 -0
- package/dist/note/text-diff.js +71 -0
- package/dist/note/types.js +2 -0
- package/dist/recording/client.js +115 -0
- package/dist/recording/commands/config.js +73 -0
- package/dist/recording/commands/delete.js +171 -0
- package/dist/recording/commands/list.js +161 -0
- package/dist/recording/commands/shared.js +47 -0
- package/dist/recording/commands/transfer.js +192 -0
- package/dist/recording/commands/trash.js +140 -0
- package/dist/recording/config.js +104 -0
- package/dist/recording/dates.js +61 -0
- package/dist/recording/download.js +308 -0
- package/dist/recording/progress.js +77 -0
- package/dist/recording/pull.js +433 -0
- package/dist/recording/recording.js +294 -0
- package/dist/recording/register.js +202 -0
- package/dist/recording/template.js +44 -0
- package/dist/recording/types.js +2 -0
- package/package.json +53 -4
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
import { stat, utimes } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { isFatalError } from "../core/http.js";
|
|
4
|
+
import { logInfo } from "../core/output.js";
|
|
5
|
+
import { formatBytes } from "../core/utils.js";
|
|
6
|
+
import { hasState, loadState, lockOutputDir, saveState } from "./config.js";
|
|
7
|
+
import { fetchRecording, matchesLocalFile } from "./download.js";
|
|
8
|
+
import { TransferProgress } from "./progress.js";
|
|
9
|
+
import { matchesFilter, parseRecording, sortByRecordedAt, } from "./recording.js";
|
|
10
|
+
import { pathKey, recordingRelPath } from "./template.js";
|
|
11
|
+
export const SKIP_REASONS = {
|
|
12
|
+
kept: "曾被本工具删除、后被恢复,视为有意保留(需要时按 id 删除)",
|
|
13
|
+
"cloud-changed": "云端内容与拉取时不一致(可 pull --force 重新拉取后再删)",
|
|
14
|
+
unverifiable: "云端缺少 SHA-1,无法校验,未删除",
|
|
15
|
+
"local-missing": "拉取目录里的文件已不在或已被修改(已转移到别处时加 --allow-moved)",
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* 为一批录音分配落盘路径。
|
|
19
|
+
*
|
|
20
|
+
* 按录音时间从早到晚分配,先到先得:模板路径被占用(本批其他录音,或磁盘上内容不同的文件)时改用 `_<id>` 后缀;
|
|
21
|
+
* 磁盘上已有大小与 SHA-1 都一致的文件则直接认领,不再下载。两个位置都被占用时报错,绝不覆盖已有文件。
|
|
22
|
+
*/
|
|
23
|
+
export async function planTargets(recordings, outputDir, template) {
|
|
24
|
+
const claimed = new Set();
|
|
25
|
+
const targets = [];
|
|
26
|
+
const failures = [];
|
|
27
|
+
for (const recording of sortByRecordedAt(recordings, "asc")) {
|
|
28
|
+
const base = recordingRelPath(template, recording);
|
|
29
|
+
let placed = false;
|
|
30
|
+
for (const relPath of [base, recordingRelPath(template, recording, `_${recording.id}`)]) {
|
|
31
|
+
const key = pathKey(relPath);
|
|
32
|
+
if (claimed.has(key))
|
|
33
|
+
continue;
|
|
34
|
+
const absPath = join(outputDir, relPath);
|
|
35
|
+
const st = await stat(absPath).catch(() => null);
|
|
36
|
+
const existing = !!st &&
|
|
37
|
+
!!recording.sha1 &&
|
|
38
|
+
(await matchesLocalFile(absPath, { size: recording.size, sha1: recording.sha1 }));
|
|
39
|
+
if (!st || existing) {
|
|
40
|
+
claimed.add(key);
|
|
41
|
+
targets.push({ recording, relPath, absPath, existing });
|
|
42
|
+
placed = true;
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (!placed) {
|
|
47
|
+
failures.push({
|
|
48
|
+
id: recording.id,
|
|
49
|
+
title: recording.title,
|
|
50
|
+
error: `目标位置已有内容不同的同名文件,未覆盖:${base}`,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return { targets, failures };
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* 判断一条已拉取的录音能否删除云端副本,返回不能删除的原因(可以删除时返回 null):
|
|
58
|
+
* 未被本工具删过(删过又恢复视为有意保留)、云端大小与 SHA-1 与拉取时一致,
|
|
59
|
+
* 且拉取目录里的文件仍完好——allowMoved 时跳过最后一项,以拉取记录为准。
|
|
60
|
+
*/
|
|
61
|
+
export async function checkCloudDeletable(r, record, outputDir, allowMoved) {
|
|
62
|
+
if (record.cloudDeletedAt)
|
|
63
|
+
return "kept";
|
|
64
|
+
if (!r.sha1 || !record.sha1)
|
|
65
|
+
return "unverifiable";
|
|
66
|
+
if (record.sha1 !== r.sha1 || record.size !== r.size)
|
|
67
|
+
return "cloud-changed";
|
|
68
|
+
if (!allowMoved && !(await matchesLocalFile(join(outputDir, record.file), { size: r.size, sha1: r.sha1 }))) {
|
|
69
|
+
return "local-missing";
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
/** 从拉取记录里挑出可以删除云端副本的录音(规则见 checkCloudDeletable) */
|
|
74
|
+
export async function selectPulled(cloud, state, outputDir, opts = {}) {
|
|
75
|
+
const targets = [];
|
|
76
|
+
const skipped = [];
|
|
77
|
+
for (const r of cloud) {
|
|
78
|
+
const record = state.recordings[r.id];
|
|
79
|
+
if (!record || (opts.predicate && !opts.predicate(r)))
|
|
80
|
+
continue;
|
|
81
|
+
const code = await checkCloudDeletable(r, record, outputDir, !!opts.allowMoved);
|
|
82
|
+
if (code)
|
|
83
|
+
skipped.push(skippedItem(r, code));
|
|
84
|
+
else
|
|
85
|
+
targets.push(r);
|
|
86
|
+
}
|
|
87
|
+
return { targets, skipped };
|
|
88
|
+
}
|
|
89
|
+
export function skippedItem(r, code) {
|
|
90
|
+
return { id: r.id, title: r.title, code, reason: SKIP_REASONS[code] };
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* 增量拉取:把云端录音下载到 outputDir,按拉取状态跳过之前拉取过的录音(即使本地文件已被移走)。
|
|
94
|
+
*
|
|
95
|
+
* 每条录音写 `.part` → 校验大小与 SHA-1 → 放到正式位置,成功后立即写入拉取状态,中断后重跑即可继续(`.part` 断点续传)。
|
|
96
|
+
* 运行期间独占拉取目录(dryRun 除外)。deleteCloud 时,本次校验通过的录音与之前已拉取、仍可校验的录音会被移到云端回收站。
|
|
97
|
+
*/
|
|
98
|
+
export async function pullRecordings(client, opts) {
|
|
99
|
+
const release = opts.dryRun ? null : await lockOutputDir(opts.outputDir);
|
|
100
|
+
try {
|
|
101
|
+
return await runPull(client, opts);
|
|
102
|
+
}
|
|
103
|
+
finally {
|
|
104
|
+
await release?.();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async function runPull(client, opts) {
|
|
108
|
+
const log = (message) => {
|
|
109
|
+
if (!opts.quiet)
|
|
110
|
+
logInfo(message);
|
|
111
|
+
};
|
|
112
|
+
const recordings = await listCloud(client, opts.quiet);
|
|
113
|
+
const matched = recordings.filter((r) => matchesFilter(r, opts.filter));
|
|
114
|
+
const state = await loadState(opts.outputDir);
|
|
115
|
+
const pending = [];
|
|
116
|
+
const previously = [];
|
|
117
|
+
for (const r of matched) {
|
|
118
|
+
if (state.recordings[r.id] && !opts.force)
|
|
119
|
+
previously.push(r);
|
|
120
|
+
else
|
|
121
|
+
pending.push(r);
|
|
122
|
+
}
|
|
123
|
+
let selected = sortByRecordedAt(pending, "desc");
|
|
124
|
+
if (opts.limit !== undefined)
|
|
125
|
+
selected = selected.slice(0, opts.limit);
|
|
126
|
+
const { targets, failures } = await planTargets(selected, opts.outputDir, opts.template);
|
|
127
|
+
const result = {
|
|
128
|
+
outputDir: opts.outputDir,
|
|
129
|
+
dryRun: !!opts.dryRun,
|
|
130
|
+
cancelled: false,
|
|
131
|
+
matched: matched.length,
|
|
132
|
+
alreadyPulled: previously.length,
|
|
133
|
+
pulled: [],
|
|
134
|
+
failed: [...failures],
|
|
135
|
+
deletedFromCloud: [],
|
|
136
|
+
deleteSkipped: [],
|
|
137
|
+
deleteFailed: [],
|
|
138
|
+
warnings: [],
|
|
139
|
+
downloadedBytes: 0,
|
|
140
|
+
};
|
|
141
|
+
// 本次拉取的录音在校验通过后删除云端;--force 重新拉取到的「删除后又被恢复」的录音仍视为保留。
|
|
142
|
+
// 带 --limit 时只清理本次拉取的录音,不顺带清理之前拉取过的(那是 delete --pulled 的事)
|
|
143
|
+
const deleteAfterPull = new Set();
|
|
144
|
+
const previouslyDeletable = [];
|
|
145
|
+
if (opts.deleteCloud) {
|
|
146
|
+
for (const t of targets) {
|
|
147
|
+
const r = t.recording;
|
|
148
|
+
if (state.recordings[r.id]?.cloudDeletedAt)
|
|
149
|
+
result.deleteSkipped.push(skippedItem(r, "kept"));
|
|
150
|
+
else if (!r.sha1)
|
|
151
|
+
result.deleteSkipped.push(skippedItem(r, "unverifiable"));
|
|
152
|
+
else
|
|
153
|
+
deleteAfterPull.add(r.id);
|
|
154
|
+
}
|
|
155
|
+
if (opts.limit === undefined) {
|
|
156
|
+
const selection = await selectPulled(previously, state, opts.outputDir, { allowMoved: opts.allowMoved });
|
|
157
|
+
previouslyDeletable.push(...selection.targets);
|
|
158
|
+
result.deleteSkipped.push(...selection.skipped);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (opts.dryRun) {
|
|
162
|
+
result.pulled = targets.map(toItem);
|
|
163
|
+
result.deletedFromCloud = [
|
|
164
|
+
...targets.filter((t) => deleteAfterPull.has(t.recording.id)).map((t) => t.recording),
|
|
165
|
+
...previouslyDeletable,
|
|
166
|
+
].map(brief);
|
|
167
|
+
return sortResult(result);
|
|
168
|
+
}
|
|
169
|
+
const deleteCount = deleteAfterPull.size + previouslyDeletable.length;
|
|
170
|
+
if (deleteCount > 0 && opts.confirmDelete && !(await opts.confirmDelete(deleteCount))) {
|
|
171
|
+
result.cancelled = true;
|
|
172
|
+
return result;
|
|
173
|
+
}
|
|
174
|
+
if (targets.length === 0 && previouslyDeletable.length === 0) {
|
|
175
|
+
log(failures.length ? "没有可拉取的录音" : `✨ 没有新录音(已拉取过 ${previously.length} 条)`);
|
|
176
|
+
for (const f of failures)
|
|
177
|
+
log(`❌ ${f.title}:${f.error}`);
|
|
178
|
+
return sortResult(result);
|
|
179
|
+
}
|
|
180
|
+
const downloadBytes = targets.filter((t) => !t.existing).reduce((sum, t) => sum + t.recording.size, 0);
|
|
181
|
+
if (targets.length > 0) {
|
|
182
|
+
log(`📥 待拉取 ${targets.length} 条(${formatBytes(downloadBytes)}),已拉取过 ${previously.length} 条 → ${opts.outputDir}`);
|
|
183
|
+
}
|
|
184
|
+
for (const f of failures)
|
|
185
|
+
log(`❌ ${f.title}:${f.error}`);
|
|
186
|
+
const progress = new TransferProgress(downloadBytes, targets.length, !!opts.showProgress, !!opts.quiet);
|
|
187
|
+
const serial = new SerialQueue();
|
|
188
|
+
let fatal = null;
|
|
189
|
+
const deleteOne = async (r) => {
|
|
190
|
+
if (fatal) {
|
|
191
|
+
result.deleteFailed.push({ id: r.id, title: r.title, error: fatal });
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
const record = state.recordings[r.id];
|
|
195
|
+
// 删除前再核对一次:规划之后(下载期间)本地文件可能已被转移或改动
|
|
196
|
+
const skip = record ? await checkCloudDeletable(r, record, opts.outputDir, !!opts.allowMoved) : "local-missing";
|
|
197
|
+
if (skip) {
|
|
198
|
+
result.deleteSkipped.push(skippedItem(r, skip));
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
try {
|
|
202
|
+
await client.deleteRecording(r.id);
|
|
203
|
+
}
|
|
204
|
+
catch (err) {
|
|
205
|
+
if (isFatalError(err))
|
|
206
|
+
fatal ??= errorMessage(err);
|
|
207
|
+
result.deleteFailed.push({ id: r.id, title: r.title, error: errorMessage(err) });
|
|
208
|
+
progress.log(`⚠️ 云端删除失败:${r.title}:${errorMessage(err)}`);
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
result.deletedFromCloud.push(brief(r));
|
|
212
|
+
progress.log(`🗑️ 已将云端录音移到回收站:${r.title}`);
|
|
213
|
+
record.cloudDeletedAt = Date.now();
|
|
214
|
+
try {
|
|
215
|
+
await serial.run(() => saveState(opts.outputDir, state));
|
|
216
|
+
}
|
|
217
|
+
catch (err) {
|
|
218
|
+
// 记不下删除标记时停止:后续操作同样无法记录
|
|
219
|
+
fatal ??= `无法写入拉取状态:${errorMessage(err)}`;
|
|
220
|
+
result.warnings.push(`已删除云端录音「${r.title}」,但${fatal}`);
|
|
221
|
+
progress.log(`⚠️ ${fatal}`);
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
progress.start();
|
|
225
|
+
try {
|
|
226
|
+
await runPool(targets, opts.concurrency, async (target) => {
|
|
227
|
+
const r = target.recording;
|
|
228
|
+
if (fatal) {
|
|
229
|
+
result.failed.push({ id: r.id, title: r.title, error: fatal });
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
try {
|
|
233
|
+
if (!target.existing) {
|
|
234
|
+
await fetchRecording(client, r, target.absPath, (bytes) => progress.update(r.id, bytes));
|
|
235
|
+
await setFileTime(target.absPath, r);
|
|
236
|
+
result.downloadedBytes += r.size;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
catch (err) {
|
|
240
|
+
if (isFatalError(err))
|
|
241
|
+
fatal ??= errorMessage(err);
|
|
242
|
+
result.failed.push({ id: r.id, title: r.title, error: errorMessage(err) });
|
|
243
|
+
progress.complete(`❌ ${target.relPath}:${errorMessage(err)}`);
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
const previous = state.recordings[r.id];
|
|
247
|
+
state.recordings[r.id] = {
|
|
248
|
+
sha1: r.sha1,
|
|
249
|
+
size: r.size,
|
|
250
|
+
file: target.relPath,
|
|
251
|
+
title: r.title,
|
|
252
|
+
pulledAt: Date.now(),
|
|
253
|
+
...(previous?.cloudDeletedAt ? { cloudDeletedAt: previous.cloudDeletedAt } : {}),
|
|
254
|
+
};
|
|
255
|
+
try {
|
|
256
|
+
await serial.run(() => saveState(opts.outputDir, state));
|
|
257
|
+
}
|
|
258
|
+
catch (err) {
|
|
259
|
+
// 状态写不进去(磁盘满等)时停止:后续拉取同样无法记录,也不再删除云端
|
|
260
|
+
fatal ??= `无法写入拉取状态:${errorMessage(err)}`;
|
|
261
|
+
result.failed.push({ id: r.id, title: r.title, error: fatal });
|
|
262
|
+
progress.complete(`❌ ${target.relPath}:${fatal}`);
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
result.pulled.push(toItem(target));
|
|
266
|
+
progress.complete(`${target.existing ? "☑️ 已存在(内容一致)" : "✅ 已下载"} ${target.relPath} ${formatBytes(r.size)}`);
|
|
267
|
+
if (deleteAfterPull.has(r.id))
|
|
268
|
+
await deleteOne(r);
|
|
269
|
+
});
|
|
270
|
+
for (const r of previouslyDeletable)
|
|
271
|
+
await deleteOne(r);
|
|
272
|
+
}
|
|
273
|
+
finally {
|
|
274
|
+
progress.stop();
|
|
275
|
+
}
|
|
276
|
+
return sortResult(result);
|
|
277
|
+
}
|
|
278
|
+
/** 读取拉取目录的状态;状态文件不存在时返回 null */
|
|
279
|
+
export async function loadExistingState(outputDir) {
|
|
280
|
+
return (await hasState(outputDir)) ? await loadState(outputDir) : null;
|
|
281
|
+
}
|
|
282
|
+
/** 按 id 下载指定录音到 outputDir(不读写拉取状态);本地已有相同内容的文件时直接跳过。运行期间独占 outputDir */
|
|
283
|
+
export async function downloadRecordings(client, ids, opts) {
|
|
284
|
+
const release = await lockOutputDir(opts.outputDir);
|
|
285
|
+
try {
|
|
286
|
+
return await runDownload(client, ids, opts);
|
|
287
|
+
}
|
|
288
|
+
finally {
|
|
289
|
+
await release();
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
async function runDownload(client, ids, opts) {
|
|
293
|
+
const recordings = await listCloud(client, true);
|
|
294
|
+
const { found, failures } = await resolveIds(client, recordings, ids);
|
|
295
|
+
const { targets, failures: planFailures } = await planTargets(found, opts.outputDir, opts.template);
|
|
296
|
+
const result = {
|
|
297
|
+
outputDir: opts.outputDir,
|
|
298
|
+
downloaded: [],
|
|
299
|
+
failed: [...failures, ...planFailures],
|
|
300
|
+
downloadedBytes: 0,
|
|
301
|
+
};
|
|
302
|
+
const downloadBytes = targets.filter((t) => !t.existing).reduce((sum, t) => sum + t.recording.size, 0);
|
|
303
|
+
const progress = new TransferProgress(downloadBytes, targets.length, !!opts.showProgress, !!opts.quiet);
|
|
304
|
+
for (const f of result.failed)
|
|
305
|
+
progress.log(`❌ ${f.title || f.id}:${f.error}`);
|
|
306
|
+
let fatal = null;
|
|
307
|
+
progress.start();
|
|
308
|
+
try {
|
|
309
|
+
await runPool(targets, opts.concurrency, async (target) => {
|
|
310
|
+
const r = target.recording;
|
|
311
|
+
if (fatal) {
|
|
312
|
+
result.failed.push({ id: r.id, title: r.title, error: fatal });
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
try {
|
|
316
|
+
if (!target.existing) {
|
|
317
|
+
await fetchRecording(client, r, target.absPath, (bytes) => progress.update(r.id, bytes));
|
|
318
|
+
await setFileTime(target.absPath, r);
|
|
319
|
+
result.downloadedBytes += r.size;
|
|
320
|
+
}
|
|
321
|
+
result.downloaded.push(toItem(target));
|
|
322
|
+
progress.complete(`${target.existing ? "☑️ 已存在(内容一致)" : "✅ 已下载"} ${target.absPath}`);
|
|
323
|
+
}
|
|
324
|
+
catch (err) {
|
|
325
|
+
if (isFatalError(err))
|
|
326
|
+
fatal ??= errorMessage(err);
|
|
327
|
+
result.failed.push({ id: r.id, title: r.title, error: errorMessage(err) });
|
|
328
|
+
progress.complete(`❌ ${target.relPath}:${errorMessage(err)}`);
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
finally {
|
|
333
|
+
progress.stop();
|
|
334
|
+
}
|
|
335
|
+
result.downloaded = sortByRecordedAt(result.downloaded, "desc");
|
|
336
|
+
return result;
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* 在云端列表里按 id 查找录音。找不到的 id 会再查回收站,给出「在回收站里」的明确提示。
|
|
340
|
+
* 重复的 id 只保留一次。
|
|
341
|
+
*/
|
|
342
|
+
export async function resolveIds(client, recordings, ids) {
|
|
343
|
+
const byId = new Map(recordings.map((r) => [r.id, r]));
|
|
344
|
+
const found = [];
|
|
345
|
+
const missing = [];
|
|
346
|
+
for (const id of new Set(ids.map((s) => s.trim()).filter(Boolean))) {
|
|
347
|
+
const r = byId.get(id);
|
|
348
|
+
if (r)
|
|
349
|
+
found.push(r);
|
|
350
|
+
else
|
|
351
|
+
missing.push(id);
|
|
352
|
+
}
|
|
353
|
+
if (missing.length === 0)
|
|
354
|
+
return { found, failures: [] };
|
|
355
|
+
let trashIds = new Set();
|
|
356
|
+
if (client.listTrash) {
|
|
357
|
+
try {
|
|
358
|
+
trashIds = new Set((await client.listTrash()).map((r) => String(r.id)));
|
|
359
|
+
}
|
|
360
|
+
catch {
|
|
361
|
+
// 查回收站只为给出更好的提示,失败不影响主流程
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
const failures = missing.map((id) => ({
|
|
365
|
+
id,
|
|
366
|
+
title: "",
|
|
367
|
+
error: trashIds.has(id)
|
|
368
|
+
? "该录音在回收站中,可先用 trash restore 恢复"
|
|
369
|
+
: "云端没有这条录音(id 有误或已被永久删除)",
|
|
370
|
+
}));
|
|
371
|
+
return { found, failures };
|
|
372
|
+
}
|
|
373
|
+
/** 拉取云端全部录音并解析 */
|
|
374
|
+
export async function listCloud(client, quiet = false) {
|
|
375
|
+
const raws = await client.listRecordings((count) => {
|
|
376
|
+
if (!quiet && process.stderr.isTTY)
|
|
377
|
+
process.stderr.write(`\r📋 已获取 ${count} 条录音...`);
|
|
378
|
+
});
|
|
379
|
+
if (!quiet && process.stderr.isTTY)
|
|
380
|
+
process.stderr.write("\r\x1b[2K");
|
|
381
|
+
return raws.map(parseRecording);
|
|
382
|
+
}
|
|
383
|
+
/** 以有限并发处理一批任务(worker 自行处理错误) */
|
|
384
|
+
export async function runPool(items, concurrency, worker) {
|
|
385
|
+
let next = 0;
|
|
386
|
+
const lane = async () => {
|
|
387
|
+
while (next < items.length) {
|
|
388
|
+
const item = items[next++];
|
|
389
|
+
await worker(item);
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
await Promise.all(Array.from({ length: Math.max(1, Math.min(concurrency, items.length)) }, lane));
|
|
393
|
+
}
|
|
394
|
+
export function errorMessage(err) {
|
|
395
|
+
return err instanceof Error ? err.message : String(err);
|
|
396
|
+
}
|
|
397
|
+
// ============ 内部 ============
|
|
398
|
+
/** 把文件修改时间设为手机上的录音文件时间,便于按时间排序 */
|
|
399
|
+
async function setFileTime(path, r) {
|
|
400
|
+
const time = r.endedAt || r.recordedAt;
|
|
401
|
+
if (!time)
|
|
402
|
+
return;
|
|
403
|
+
try {
|
|
404
|
+
await utimes(path, time / 1000, time / 1000);
|
|
405
|
+
}
|
|
406
|
+
catch {
|
|
407
|
+
// 设置失败不影响结果
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
function toItem(target) {
|
|
411
|
+
return {
|
|
412
|
+
...target.recording,
|
|
413
|
+
file: target.absPath,
|
|
414
|
+
relativePath: target.relPath,
|
|
415
|
+
existing: target.existing,
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
function brief(r) {
|
|
419
|
+
return { id: r.id, title: r.title };
|
|
420
|
+
}
|
|
421
|
+
function sortResult(result) {
|
|
422
|
+
result.pulled = sortByRecordedAt(result.pulled, "desc");
|
|
423
|
+
return result;
|
|
424
|
+
}
|
|
425
|
+
/** 串行执行异步任务(保证状态文件写入不交错) */
|
|
426
|
+
class SerialQueue {
|
|
427
|
+
tail = Promise.resolve();
|
|
428
|
+
run(task) {
|
|
429
|
+
const next = this.tail.then(task);
|
|
430
|
+
this.tail = next.catch(() => undefined);
|
|
431
|
+
return next;
|
|
432
|
+
}
|
|
433
|
+
}
|