Update server.js via upload script - 2026-11-16 22:56:50
This commit is contained in:
parent
cc6d6d44ff
commit
685da99380
42
server.js
42
server.js
|
|
@ -1402,7 +1402,7 @@ async function extractConvoFile(f) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const convoUpload = multer({ storage: multer.memoryStorage(), limits: { files: 10, fileSize: 30 * 1024 * 1024 } });
|
const convoUpload = multer({ storage: multer.memoryStorage(), limits: { files: 10, fileSize: 30 * 1024 * 1024 } });
|
||||||
const importUpload = multer({ storage: multer.memoryStorage(), limits: { files: 5, fileSize: 200 * 1024 * 1024 } });
|
const importUpload = multer({ storage: multer.memoryStorage(), limits: { files: 30, fileSize: 200 * 1024 * 1024 } });
|
||||||
const fixUploadName = (n) => { n = String(n || ''); if (/[^\u0000-ÿ]/.test(n)) return n; const u = Buffer.from(n, 'latin1').toString('utf8'); return u.includes('<27>') ? n : u; };
|
const fixUploadName = (n) => { n = String(n || ''); if (/[^\u0000-ÿ]/.test(n)) return n; const u = Buffer.from(n, 'latin1').toString('utf8'); return u.includes('<27>') ? n : u; };
|
||||||
const clipTxt = (v, n) => String(v == null ? '' : v).replace(/\r\n/g, '\n').slice(0, n);
|
const clipTxt = (v, n) => String(v == null ? '' : v).replace(/\r\n/g, '\n').slice(0, n);
|
||||||
|
|
||||||
|
|
@ -1512,7 +1512,7 @@ app.post('/api/convos/:cid', requireAuth, (req, res) => {
|
||||||
// me:聊天记录里"我"的名字(默认 Mike),其他人都算客户
|
// me:聊天记录里"我"的名字(默认 Mike),其他人都算客户
|
||||||
app.post('/api/convos/:cid/import', requireAuth, (req, res) => {
|
app.post('/api/convos/:cid/import', requireAuth, (req, res) => {
|
||||||
const cid = safeId(req.params.cid); if (!cid) return res.status(400).json({ error: 'bad id' });
|
const cid = safeId(req.params.cid); if (!cid) return res.status(400).json({ error: 'bad id' });
|
||||||
importUpload.array('files', 5)(req, res, async (err) => {
|
importUpload.array('files', 30)(req, res, async (err) => {
|
||||||
if (err) return res.status(400).json({ error: err.code === 'LIMIT_FILE_SIZE' ? '文件太大(最多 200 MB)' : err.message });
|
if (err) return res.status(400).json({ error: err.code === 'LIMIT_FILE_SIZE' ? '文件太大(最多 200 MB)' : err.message });
|
||||||
const b = req.body || {};
|
const b = req.body || {};
|
||||||
const meNames = String(b.me || 'Mike').split(/[,,]/).map((s) => s.trim()).filter(Boolean);
|
const meNames = String(b.me || 'Mike').split(/[,,]/).map((s) => s.trim()).filter(Boolean);
|
||||||
|
|
@ -1542,13 +1542,38 @@ app.post('/api/convos/:cid/import', requireAuth, (req, res) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
const pasted = clipTxt(b.text, CONVO_TEXT_MAX).trim();
|
// 复制粘贴导入:前端已经把粘贴的文字拆成一条条消息(可在预览里改发送人),粘贴的图片 / 文件各自是一条消息
|
||||||
|
if (b.messages) {
|
||||||
|
let list; try { list = JSON.parse(b.messages); } catch (e) { return res.status(400).json({ error: '消息格式不对' }); }
|
||||||
|
if (!Array.isArray(list) || !list.length) return res.status(400).json({ error: '没有要导入的消息' });
|
||||||
|
const baseAt = AT_RE.test(b.at || '') ? b.at : nowAt();
|
||||||
|
const t0 = Date.now();
|
||||||
|
for (let i = 0; i < list.length && i < 3000; i++) {
|
||||||
|
const x = list[i] || {};
|
||||||
|
const text = clipTxt(x.text, CONVO_TEXT_MAX).trim();
|
||||||
|
const fl = [], ft = [];
|
||||||
|
for (const k of Array.isArray(x.files) ? x.files : []) {
|
||||||
|
const f = files[k]; if (!f) continue;
|
||||||
|
fl.push(storeFile(cid, f)); const tx = await fileTextFor(f); if (tx) ft.push(`【${f.originalname}】\n${tx}`);
|
||||||
|
}
|
||||||
|
if (!text && !fl.length) continue;
|
||||||
|
const msg = {
|
||||||
|
id: newMsgId(), from: x.from === 'me' ? 'me' : 'customer', channel: MSG_CHANNELS.includes(x.channel) ? x.channel : (channel || 'whatsapp'),
|
||||||
|
at: AT_RE.test(x.at || '') ? x.at : baseAt, text, files: fl, createdAt: t0 + i,
|
||||||
|
};
|
||||||
|
if (x.who) msg.who = clipTxt(x.who, 60);
|
||||||
|
if (ft.length) msg.fileText = clipTxt(ft.join('\n\n'), 40000);
|
||||||
|
if (!text || isMostlyChinese(text)) msg.zh = '';
|
||||||
|
out.push(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const pasted = b.messages ? '' : clipTxt(b.text, CONVO_TEXT_MAX).trim();
|
||||||
if (pasted) {
|
if (pasted) {
|
||||||
const rows = parseWaMessages(pasted);
|
const rows = parseWaMessages(pasted);
|
||||||
if (rows.length < 1) return res.status(400).json({ error: '没认出聊天格式。每行要像 "12/11/2026, 14:05 - Anton: 内容" 这样;单条消息请直接在下面的输入框里发。' });
|
if (rows.length < 1) return res.status(400).json({ error: '没认出聊天格式。每行要像 "12/11/2026, 14:05 - Anton: 内容" 这样;单条消息请直接在下面的输入框里发。' });
|
||||||
addWa(rows, null, channel);
|
addWa(rows, null, channel);
|
||||||
}
|
}
|
||||||
for (const f of files) {
|
for (const f of b.messages ? [] : files) {
|
||||||
const ext = path.extname(f.originalname).toLowerCase();
|
const ext = path.extname(f.originalname).toLowerCase();
|
||||||
if (ext === '.zip') {
|
if (ext === '.zip') {
|
||||||
const entries = unzipEntries(f.buffer);
|
const entries = unzipEntries(f.buffer);
|
||||||
|
|
@ -1577,9 +1602,14 @@ app.post('/api/convos/:cid/import', requireAuth, (req, res) => {
|
||||||
if (!out.length) return res.status(400).json({ error: '没有读到消息' });
|
if (!out.length) return res.status(400).json({ error: '没有读到消息' });
|
||||||
// 和已有消息重复的(同时间、同发送方、同内容)跳过,方便重复导入同一个聊天的新导出
|
// 和已有消息重复的(同时间、同发送方、同内容)跳过,方便重复导入同一个聊天的新导出
|
||||||
const all = readConvos(); const c = convoOf(all, cid);
|
const all = readConvos(); const c = convoOf(all, cid);
|
||||||
const key = (m) => `${m.at}|${m.from}|${String(m.text || '').slice(0, 200)}`;
|
const key = (m) => `${m.at}|${m.from}|${String(m.text || '').slice(0, 200)}|${(m.files || []).map((f) => f.name).join(',')}`;
|
||||||
const have = new Set(c.items.map(key));
|
const have = new Set(c.items.map(key));
|
||||||
const fresh = out.filter((m) => !have.has(key(m)));
|
const fresh = [];
|
||||||
|
for (const m of out) {
|
||||||
|
const k = key(m);
|
||||||
|
if (have.has(k)) { for (const f of m.files || []) fs.unlink(path.join(UPLOADS_DIR, cid, f.stored), () => {}); continue; }
|
||||||
|
fresh.push(m);
|
||||||
|
}
|
||||||
c.items.push(...fresh); writeConvos(all);
|
c.items.push(...fresh); writeConvos(all);
|
||||||
const people = [...new Set(out.map((m) => m.who).filter(Boolean))];
|
const people = [...new Set(out.map((m) => m.who).filter(Boolean))];
|
||||||
res.json({ ok: true, items: fresh, skipped: out.length - fresh.length, people, meFound: out.some((m) => m.from === 'me') });
|
res.json({ ok: true, items: fresh, skipped: out.length - fresh.length, people, meFound: out.some((m) => m.from === 'me') });
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue