2018-07-27 01:13:56 +00:00
|
|
|
const electron = require('electron');
|
|
|
|
const Attachments = require('./attachments');
|
|
|
|
const rimraf = require('rimraf');
|
|
|
|
|
|
|
|
const { ipcMain } = electron;
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
initialize,
|
|
|
|
};
|
|
|
|
|
|
|
|
let initialized = false;
|
|
|
|
|
|
|
|
const ERASE_ATTACHMENTS_KEY = 'erase-attachments';
|
2018-08-08 17:00:33 +00:00
|
|
|
const CLEANUP_ORPHANED_ATTACHMENTS_KEY = 'cleanup-orphaned-attachments';
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2018-08-08 17:00:33 +00:00
|
|
|
async function initialize({ configDir, cleanupOrphanedAttachments }) {
|
2018-07-27 01:13:56 +00:00
|
|
|
if (initialized) {
|
|
|
|
throw new Error('initialze: Already initialized!');
|
|
|
|
}
|
|
|
|
initialized = true;
|
|
|
|
|
|
|
|
console.log('Ensure attachments directory exists');
|
|
|
|
await Attachments.ensureDirectory(configDir);
|
|
|
|
|
|
|
|
const attachmentsDir = Attachments.getPath(configDir);
|
|
|
|
|
|
|
|
ipcMain.on(ERASE_ATTACHMENTS_KEY, async event => {
|
|
|
|
try {
|
|
|
|
rimraf.sync(attachmentsDir);
|
|
|
|
event.sender.send(`${ERASE_ATTACHMENTS_KEY}-done`);
|
|
|
|
} catch (error) {
|
|
|
|
const errorForDisplay = error && error.stack ? error.stack : error;
|
2018-08-08 17:00:33 +00:00
|
|
|
console.log(`erase attachments error: ${errorForDisplay}`);
|
2018-07-27 01:13:56 +00:00
|
|
|
event.sender.send(`${ERASE_ATTACHMENTS_KEY}-done`, error);
|
|
|
|
}
|
|
|
|
});
|
2018-08-08 17:00:33 +00:00
|
|
|
|
|
|
|
ipcMain.on(CLEANUP_ORPHANED_ATTACHMENTS_KEY, async event => {
|
|
|
|
try {
|
|
|
|
await cleanupOrphanedAttachments();
|
|
|
|
event.sender.send(`${CLEANUP_ORPHANED_ATTACHMENTS_KEY}-done`);
|
|
|
|
} catch (error) {
|
|
|
|
const errorForDisplay = error && error.stack ? error.stack : error;
|
|
|
|
console.log(`cleanup orphaned attachments error: ${errorForDisplay}`);
|
|
|
|
event.sender.send(`${CLEANUP_ORPHANED_ATTACHMENTS_KEY}-done`, error);
|
|
|
|
}
|
|
|
|
});
|
2018-07-27 01:13:56 +00:00
|
|
|
}
|