fix: convert QR to base64 DataURL and migrate old session to Slot 1 dynamically
This commit is contained in:
@@ -278,11 +278,19 @@ function bootstrapSlot(slotId) {
|
||||
// ─── Listeners ───────────────────────────────────────────────────────────
|
||||
client.on('qr', (qr) => {
|
||||
console.log(`[WA] QR generated for Slot ${slotId}`);
|
||||
slotState.qrCodeCache = qr;
|
||||
|
||||
const QRCode = require('qrcode');
|
||||
QRCode.toDataURL(qr, (err, url) => {
|
||||
if (err) {
|
||||
console.error(`[WA] Failed to generate QR DataURL for Slot ${slotId}:`, err.message);
|
||||
return;
|
||||
}
|
||||
slotState.qrCodeCache = url;
|
||||
slotState.clientReady = false;
|
||||
|
||||
updateSlotStatus(slotId, 'qrcode');
|
||||
broadcast(slotId, { type: 'qr', qr });
|
||||
broadcast(slotId, { type: 'qr', qr: url });
|
||||
});
|
||||
});
|
||||
|
||||
client.on('authenticated', () => {
|
||||
@@ -719,6 +727,24 @@ server.listen(PORT, async () => {
|
||||
// Initialize MySQL Database
|
||||
await initDatabase();
|
||||
|
||||
// Session Migration Logic: Move old session to slot-1 if slot-1 is empty
|
||||
const oldSessionPath = path.join(__dirname, '.wwebjs_auth', 'session');
|
||||
const slot1SessionPath = path.join(__dirname, '.wwebjs_auth', 'session-slot-1');
|
||||
if (fs.existsSync(oldSessionPath) && !fs.existsSync(slot1SessionPath)) {
|
||||
console.log('[MIGRATION] Migrating active WhatsApp session from old version to Slot 1...');
|
||||
try {
|
||||
if (fs.cpSync) {
|
||||
fs.cpSync(oldSessionPath, slot1SessionPath, { recursive: true });
|
||||
} else {
|
||||
const { execSync } = require('child_process');
|
||||
execSync(`cp -R "${oldSessionPath}" "${slot1SessionPath}"`);
|
||||
}
|
||||
console.log('[MIGRATION] Session migrated successfully! Slot 1 will load connected.');
|
||||
} catch (migrationErr) {
|
||||
console.error('[MIGRATION ERROR] Failed to migrate session:', migrationErr.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Auto-bootstrap Slot 1 at startup
|
||||
bootstrapSlot(1);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user