Skip to content

Commit 7e50eb0

Browse files
Fix mirror resume guarantee in Keep import route
Use finally instead of duplicate resume() calls in success/catch paths, so the mirror cannot get stuck paused if the catch block itself throws. Matches the pattern already used in the Obsidian import route. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent d5e363e commit 7e50eb0

1 file changed

Lines changed: 13 additions & 35 deletions

File tree

server/src/routes/import.js

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -210,63 +210,42 @@ router.post('/', blockInDemo, upload.single('archive'), async (req, res) => {
210210
mirrorWorker.pause('keep-import');
211211

212212
// Process the import with progress updates
213+
let importResult;
213214
try {
214-
// Patch the processGoogleKeepImport function to send progress
215-
const originalProcessNote = processGoogleKeepImport;
216-
217215
// Create a wrapped version that sends progress updates
218216
const processWithProgress = async (dirPath) => {
219-
// Get file count first
220-
const files = fs.readdirSync(dirPath)
221-
.filter(file => file.endsWith('.json'));
222-
217+
const files = fs.readdirSync(dirPath).filter(file => file.endsWith('.json'));
223218
sendProgress('status', { message: `Found ${files.length} notes to import` });
224-
225-
// Track progress
219+
226220
let lastProgressPercent = 0;
227-
228-
// Override the console.log function temporarily to capture progress
229221
const originalConsoleLog = console.log;
230222
console.log = (message) => {
231223
originalConsoleLog(message);
232-
233-
// Check if the message contains batch progress
234224
if (message.includes('Progress:')) {
235225
const progressMatch = message.match(/Progress: (\d+)\/(\d+)/);
236226
if (progressMatch) {
237227
const [, current, total] = progressMatch;
238228
const percent = Math.floor((parseInt(current) / parseInt(total)) * 100);
239-
240-
// Only send updates when the percentage changes significantly
241229
if (percent >= lastProgressPercent + 5 || percent === 100) {
242230
lastProgressPercent = percent;
243-
sendProgress('progress', {
244-
current: parseInt(current),
245-
total: parseInt(total),
246-
percent
247-
});
231+
sendProgress('progress', { current: parseInt(current), total: parseInt(total), percent });
248232
}
249233
}
250234
}
251235
};
252-
236+
253237
try {
254-
const result = await originalProcessNote(dirPath);
255-
256-
// Restore original console.log
238+
return await processGoogleKeepImport(dirPath);
239+
} finally {
257240
console.log = originalConsoleLog;
258-
259-
return result;
260-
} catch (error) {
261-
// Restore original console.log
262-
console.log = originalConsoleLog;
263-
throw error;
264241
}
265242
};
266-
267-
const importResult = await processWithProgress(keepDir);
268-
269-
mirrorWorker.resume();
243+
244+
try {
245+
importResult = await processWithProgress(keepDir);
246+
} finally {
247+
mirrorWorker.resume();
248+
}
270249

271250
// Clean up temporary files
272251
cleanup(zipFilePath, extractionDir);
@@ -280,7 +259,6 @@ router.post('/', blockInDemo, upload.single('archive'), async (req, res) => {
280259

281260
res.end();
282261
} catch (importError) {
283-
mirrorWorker.resume();
284262
console.error('Import process error:', importError);
285263
sendProgress('error', {
286264
message: 'An error occurred during the import process',

0 commit comments

Comments
 (0)