-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.js
More file actions
433 lines (385 loc) · 13.9 KB
/
scan.js
File metadata and controls
433 lines (385 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/**
* COS 智能解压迁移系统(v5.2.2 修正版:首图按时间 + 自动清理残留目录)
* --------------------------------------------------------------
* 作者: ChatGPT for hong lin
* 功能:
* ✅ 自动检测文件稳定,防止重复处理
* ✅ 多引擎解压 (7zz / unzip / unrar / bsdtar)
* ✅ 自动加载密码文件(含空密码)
* ✅ 清理非媒体文件,仅保留图片/视频
* ✅ 智能目录分组 + 单层上移
* ✅ 实时图片分组(20秒内同组,超时新组)
* ✅ 文件夹自动命名为首图名(按最早修改时间)
* ✅ 首图改名为1(保留扩展名)
* ✅ 自动清理空旧目录(防止重复Group)
* ✅ 自动清理30分钟前日志
* ✅ 优雅退出 (systemd 兼容)
*/
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
// === 配置路径 ===
const WATCH_DIR = process.env.WATCH_DIR || "/mnt/notify/shared/bot/cos";
const LOCAL_DIR = process.env.LOCAL_DIR || "/root/scan/chane";
const PASSWORD_FILE = process.env.PASSWORD_FILE || "/root/scan/passwords.txt";
const ERROR_LOG = process.env.ERROR_LOG || "/root/scan/error.log";
const SUCCESS_LOG = process.env.SUCCESS_LOG || "/root/scan/success.log";
const FAILED_DIR = process.env.FAILED_DIR || "/root/scan/failed";
// === 常量 ===
const IMAGE_EXTS = /\.(jpg|jpeg|png|gif|webp|bmp|tiff|heic)$/i;
const VIDEO_EXTS = /\.(mp4|mov|avi|mkv|webm|wmv|flv|mpeg|mpg)$/i;
const ARCHIVE_EXT = /\.(zip|7z|rar)$/i;
const INTERVAL_MS = 2000;
const STABLE_CHECK_MS = 5000;
const GROUP_TIMEOUT_MS = 20000; // 20 秒
const STALE_LOCK_MS = 30 * 60 * 1000;
let isProcessing = false;
// === 图片分组状态 ===
let lastImageTime = 0;
let currentGroupDir = "";
let groupActive = false;
// === 工具函数 ===
function log(msg) {
console.log(`[${new Date().toLocaleTimeString()}] ${msg}`);
}
function logError(file, msg) {
fs.appendFileSync(ERROR_LOG, `[${new Date().toLocaleString()}] ${file}: ${msg}\n`);
}
function logSuccess(file, msg) {
fs.appendFileSync(SUCCESS_LOG, `[${new Date().toLocaleString()}] ${file}: ${msg}\n`);
}
function safeDirName(name) {
return path.parse(name).name.replace(/[^\w\u4e00-\u9fa5-]/g, "_");
}
function ensureDir(dirPath, label) {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
log(`🛠️ 自动创建${label}: ${dirPath}`);
}
}
function ensureParentDir(filePath, label) {
ensureDir(path.dirname(filePath), label);
}
function isStaleLock(lockFile) {
if (!fs.existsSync(lockFile)) return false;
return Date.now() - fs.statSync(lockFile).mtimeMs > STALE_LOCK_MS;
}
function collectPendingArchives() {
const files = fs.readdirSync(WATCH_DIR).filter((f) => ARCHIVE_EXT.test(f));
const readyFiles = [];
for (const file of files) {
const lockFile = path.join(WATCH_DIR, `${file}.lock`);
if (fs.existsSync(lockFile)) {
if (isStaleLock(lockFile)) {
fs.unlinkSync(lockFile);
log(`🧹 清理过期锁文件: ${path.basename(lockFile)}`);
} else {
continue;
}
}
readyFiles.push(file);
}
return readyFiles;
}
/** 🧹 清理30分钟前日志 */
function cleanOldLogs() {
const logs = [ERROR_LOG, SUCCESS_LOG];
const cutoff = Date.now() - 30 * 60 * 1000;
for (const logFile of logs) {
if (!fs.existsSync(logFile)) continue;
const lines = fs
.readFileSync(logFile, "utf8")
.split("\n")
.filter((l) => {
const match = l.match(/\[(.*?)\]/);
if (!match) return true;
const date = new Date(match[1]);
return date.getTime() > cutoff;
});
fs.writeFileSync(logFile, lines.join("\n"));
}
}
/** 📏 文件夹大小与数量 */
function getDirStats(dir) {
let total = 0, count = 0;
for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
const p = path.join(dir, e.name);
if (e.isDirectory()) {
const s = getDirStats(p);
total += s.size;
count += s.count;
} else {
total += fs.statSync(p).size;
count++;
}
}
return { size: total, count };
}
/** 🧩 检测文件稳定 */
async function waitForStableFile(filePath) {
let lastSize = 0, stableTime = 0;
return new Promise((resolve) => {
const timer = setInterval(() => {
if (!fs.existsSync(filePath)) {
clearInterval(timer);
log(`⚠️ 文件在稳定检测期间消失,跳过: ${path.basename(filePath)}`);
resolve(false);
return;
}
let size = 0;
try {
size = fs.statSync(filePath).size;
} catch {
clearInterval(timer);
log(`⚠️ 文件在稳定检测期间不可访问,跳过: ${path.basename(filePath)}`);
resolve(false);
return;
}
if (size === lastSize) stableTime += INTERVAL_MS;
else {
lastSize = size;
stableTime = 0;
}
if (stableTime >= STABLE_CHECK_MS) {
clearInterval(timer);
log(`📦 文件大小稳定: ${path.basename(filePath)} (${size} bytes)`);
resolve(true);
}
}, INTERVAL_MS);
});
}
/** 🔐 加载密码列表 */
function loadPasswords() {
if (!fs.existsSync(PASSWORD_FILE)) return [""];
const raw = fs.readFileSync(PASSWORD_FILE, "utf8")
.split(/\r?\n/)
.map((p) => p.trim())
.filter(Boolean);
return [""].concat([...new Set(raw)]);
}
function hasArchiveFiles(dir) {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) {
if (hasArchiveFiles(full)) return true;
} else if (ARCHIVE_EXT.test(entry.name)) {
return true;
}
}
return false;
}
function cleanExtractedFiles(dir, keepArchivesOnly) {
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) {
cleanExtractedFiles(full, keepArchivesOnly);
if (fs.readdirSync(full).length === 0) fs.rmdirSync(full);
} else {
const keep = keepArchivesOnly
? ARCHIVE_EXT.test(entry.name)
: IMAGE_EXTS.test(entry.name) || VIDEO_EXTS.test(entry.name);
if (!keep) {
fs.unlinkSync(full);
log(`🧹 删除无关文件: ${entry.name}`);
}
}
}
}
/** 🚚 智能复制(单层化 + 编号) */
function smartCopyToTarget(src, dest) {
fs.mkdirSync(dest, { recursive: true });
const entries = fs.readdirSync(src, { withFileTypes: true });
if (entries.length === 1 && entries[0].isDirectory()) {
const inner = path.join(src, entries[0].name);
for (const item of fs.readdirSync(inner)) {
fs.cpSync(path.join(inner, item), path.join(dest, item), { recursive: true });
}
return;
}
let index = 1;
for (const entry of entries) {
const srcPath = path.join(src, entry.name);
if (entry.isDirectory()) {
const subDest = path.join(dest, `${index}_${entry.name}`);
fs.mkdirSync(subDest, { recursive: true });
fs.cpSync(srcPath, subDest, { recursive: true });
index++;
} else {
fs.copyFileSync(srcPath, path.join(dest, entry.name));
}
}
}
/** 🧩 多引擎解压 */
function tryExtractAllEngines(filePath, destDir) {
const PASSWORDS = loadPasswords();
const engines = {
"7zz": (pwd) => `7zz x "${filePath}" -o"${destDir}" -p"${pwd}" -y`,
unzip: (pwd) => `unzip -P "${pwd}" -qq -o "${filePath}" -d "${destDir}"`,
unrar: (pwd) => `unrar x -y -p"${pwd}" "${filePath}" "${destDir}"`,
bsdtar: (pwd) => `bsdtar -xf "${filePath}" -C "${destDir}" --password="${pwd}"`,
};
const available = Object.keys(engines).filter((cmd) => {
try {
execSync(`which ${cmd}`, { stdio: "ignore" });
return true;
} catch {
return false;
}
});
for (const pwd of PASSWORDS) {
for (const tool of available) {
try {
log(`🗜️ 解压(${tool}) 密码:${pwd || "(无)"}`);
execSync(engines[tool](pwd), { stdio: "ignore" });
if (fs.existsSync(destDir) && fs.readdirSync(destDir).length > 0) {
log(`✅ 解压成功 → ${tool} | 密码: ${pwd || "(无)"}`);
logSuccess(path.basename(filePath), `${tool}:${pwd || "(无)"}`);
return true;
}
} catch {}
}
}
return false;
}
/** 📸 实时图片分组监控(v5.2.2) */
function watchImageFolder() {
fs.watch(WATCH_DIR, { persistent: true }, (event, filename) => {
if (!filename || !IMAGE_EXTS.test(filename)) return;
const fullPath = path.join(WATCH_DIR, filename);
setTimeout(() => {
if (!fs.existsSync(fullPath)) return;
const now = Date.now();
// 新建分组
if (!groupActive || now - lastImageTime > GROUP_TIMEOUT_MS) {
const groupName = `Group_${new Date().toISOString().replace(/[:T]/g, "-").split(".")[0]}`;
currentGroupDir = path.join(WATCH_DIR, safeDirName(groupName));
fs.mkdirSync(currentGroupDir, { recursive: true });
groupActive = true;
log(`🆕 创建图片组: ${path.basename(currentGroupDir)}`);
// 延迟检测稳定
setTimeout(() => {
if (!fs.existsSync(currentGroupDir)) return;
const files = fs.readdirSync(currentGroupDir).filter(f => IMAGE_EXTS.test(f));
if (files.length === 0) return;
// 🩵 按修改时间排序获取首图
const firstImage = files
.map(f => ({
name: f,
time: fs.statSync(path.join(currentGroupDir, f)).mtimeMs,
}))
.sort((a, b) => a.time - b.time)[0].name;
const firstNameNoExt = path.parse(firstImage).name;
const firstExt = path.extname(firstImage);
const parent = path.dirname(currentGroupDir);
const newGroupPath = path.join(parent, safeDirName(firstNameNoExt));
try {
fs.renameSync(currentGroupDir, newGroupPath);
// ✅ 清理旧目录
setTimeout(() => {
if (fs.existsSync(currentGroupDir)) {
const files = fs.readdirSync(currentGroupDir);
if (files.length === 0) {
fs.rmSync(currentGroupDir, { recursive: true, force: true });
log(`🧹 清理残留临时目录: ${path.basename(currentGroupDir)}`);
}
}
}, 1000);
currentGroupDir = newGroupPath;
// 首图改名为 1
const oldPath = path.join(currentGroupDir, firstImage);
const newPath = path.join(currentGroupDir, `1${firstExt}`);
if (fs.existsSync(oldPath)) fs.renameSync(oldPath, newPath);
log(`🏷️ 组已稳定并重命名为: ${path.basename(currentGroupDir)} (首图→1)`);
logSuccess(path.basename(currentGroupDir), "Folder renamed to first image + cleaned residual");
} catch (err) {
logError(currentGroupDir, `命名失败: ${err.message}`);
}
groupActive = false;
}, GROUP_TIMEOUT_MS + 2000);
}
// 移动文件
const dest = path.join(currentGroupDir, filename);
try {
fs.renameSync(fullPath, dest);
} catch {
fs.copyFileSync(fullPath, dest);
fs.unlinkSync(fullPath);
}
log(`📸 新图片分组 → ${filename} 到 ${path.basename(currentGroupDir)}`);
lastImageTime = now;
}, 2000);
});
}
/** 🗜️ 主处理逻辑 */
async function handleArchive(file) {
const srcPath = path.join(WATCH_DIR, file);
const lockFile = srcPath + ".lock";
if (isProcessing) return;
if (fs.existsSync(lockFile)) return;
isProcessing = true;
fs.writeFileSync(lockFile, "processing");
try {
const stable = await waitForStableFile(srcPath);
if (!stable) return;
ensureDir(LOCAL_DIR, "临时目录");
const safeName = safeDirName(file);
const localCopy = path.join(LOCAL_DIR, file);
const tempDir = path.join(LOCAL_DIR, safeName + "_temp");
const targetDir = path.join(WATCH_DIR, safeName);
fs.mkdirSync(tempDir, { recursive: true });
fs.copyFileSync(srcPath, localCopy);
if (!tryExtractAllEngines(localCopy, tempDir)) throw new Error("解压失败");
const keepArchivesOnly = hasArchiveFiles(tempDir);
cleanExtractedFiles(tempDir, keepArchivesOnly);
log(keepArchivesOnly ? "📦 检测到内层压缩包,仅保留内层压缩包" : "🖼️ 未检测到内层压缩包,保留图片/视频");
smartCopyToTarget(tempDir, targetDir);
const a = getDirStats(tempDir), b = getDirStats(targetDir);
if (a.size === b.size && a.count === b.count && b.size > 0) {
fs.unlinkSync(srcPath);
fs.rmSync(tempDir, { recursive: true, force: true });
fs.unlinkSync(localCopy);
log(`✅ 任务完成: ${file}`);
} else throw new Error(`校验失败 (${a.size} ≠ ${b.size}, ${a.count} ≠ ${b.count})`);
} catch (err) {
log(`❌ 失败 (${file}): ${err.message}`);
fs.mkdirSync(FAILED_DIR, { recursive: true });
try {
fs.renameSync(srcPath, path.join(FAILED_DIR, file));
} catch {
fs.copyFileSync(srcPath, path.join(FAILED_DIR, file));
fs.unlinkSync(srcPath);
}
} finally {
if (fs.existsSync(lockFile)) fs.unlinkSync(lockFile);
isProcessing = false;
}
}
/** 🔁 主循环 */
async function mainLoop() {
cleanOldLogs();
if (!isProcessing) {
const files = collectPendingArchives();
if (files.length > 0) await handleArchive(files[0]);
}
}
/** 🧹 优雅退出 */
process.on("SIGTERM", () => {
log("🛑 收到 systemd 停止信号,正在退出...");
process.exit(0);
});
process.on("SIGINT", () => {
log("🛑 手动中断 (Ctrl+C)");
process.exit(0);
});
log("🚀 启动 COS 智能解压迁移系统(v5.2.2 修正版)");
ensureDir(WATCH_DIR, "监控目录");
ensureDir(LOCAL_DIR, "临时目录");
ensureDir(FAILED_DIR, "失败目录");
ensureParentDir(ERROR_LOG, "错误日志目录");
ensureParentDir(SUCCESS_LOG, "成功日志目录");
log(`📂 监控目录: ${WATCH_DIR}`);
log(`📦 临时目录: ${LOCAL_DIR}`);
log("------------------------------------");
setInterval(mainLoop, INTERVAL_MS);
watchImageFolder();