2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2018-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
import { ipcMain } from 'electron';
|
|
|
|
import * as rimraf from 'rimraf';
|
|
|
|
import {
|
|
|
|
getPath,
|
|
|
|
getStickersPath,
|
|
|
|
getTempPath,
|
|
|
|
getDraftPath,
|
2021-10-27 17:54:16 +00:00
|
|
|
} from '../ts/util/attachments';
|
2018-07-27 01:13:56 +00:00
|
|
|
|
|
|
|
let initialized = false;
|
|
|
|
|
|
|
|
const ERASE_ATTACHMENTS_KEY = 'erase-attachments';
|
2019-05-16 22:32:11 +00:00
|
|
|
const ERASE_STICKERS_KEY = 'erase-stickers';
|
2019-05-24 01:27:42 +00:00
|
|
|
const ERASE_TEMP_KEY = 'erase-temp';
|
2019-08-07 00:40:25 +00:00
|
|
|
const ERASE_DRAFTS_KEY = 'erase-drafts';
|
2018-08-08 17:00:33 +00:00
|
|
|
const CLEANUP_ORPHANED_ATTACHMENTS_KEY = 'cleanup-orphaned-attachments';
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export function initialize({
|
|
|
|
configDir,
|
|
|
|
cleanupOrphanedAttachments,
|
|
|
|
}: {
|
|
|
|
configDir: string;
|
|
|
|
cleanupOrphanedAttachments: () => Promise<void>;
|
|
|
|
}): void {
|
2018-07-27 01:13:56 +00:00
|
|
|
if (initialized) {
|
|
|
|
throw new Error('initialze: Already initialized!');
|
|
|
|
}
|
|
|
|
initialized = true;
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
const attachmentsDir = getPath(configDir);
|
|
|
|
const stickersDir = getStickersPath(configDir);
|
|
|
|
const tempDir = getTempPath(configDir);
|
|
|
|
const draftDir = getDraftPath(configDir);
|
2019-05-24 01:27:42 +00:00
|
|
|
|
|
|
|
ipcMain.on(ERASE_TEMP_KEY, event => {
|
|
|
|
try {
|
|
|
|
rimraf.sync(tempDir);
|
|
|
|
event.sender.send(`${ERASE_TEMP_KEY}-done`);
|
|
|
|
} catch (error) {
|
|
|
|
const errorForDisplay = error && error.stack ? error.stack : error;
|
|
|
|
console.log(`erase temp error: ${errorForDisplay}`);
|
|
|
|
event.sender.send(`${ERASE_TEMP_KEY}-done`, error);
|
|
|
|
}
|
|
|
|
});
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
ipcMain.on(ERASE_ATTACHMENTS_KEY, event => {
|
2018-07-27 01:13:56 +00:00
|
|
|
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
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
ipcMain.on(ERASE_STICKERS_KEY, event => {
|
|
|
|
try {
|
|
|
|
rimraf.sync(stickersDir);
|
|
|
|
event.sender.send(`${ERASE_STICKERS_KEY}-done`);
|
|
|
|
} catch (error) {
|
|
|
|
const errorForDisplay = error && error.stack ? error.stack : error;
|
|
|
|
console.log(`erase stickers error: ${errorForDisplay}`);
|
|
|
|
event.sender.send(`${ERASE_STICKERS_KEY}-done`, error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-07 00:40:25 +00:00
|
|
|
ipcMain.on(ERASE_DRAFTS_KEY, event => {
|
|
|
|
try {
|
|
|
|
rimraf.sync(draftDir);
|
|
|
|
event.sender.send(`${ERASE_DRAFTS_KEY}-done`);
|
|
|
|
} catch (error) {
|
|
|
|
const errorForDisplay = error && error.stack ? error.stack : error;
|
|
|
|
console.log(`erase drafts error: ${errorForDisplay}`);
|
|
|
|
event.sender.send(`${ERASE_DRAFTS_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
|
|
|
}
|