Skip to content

Commit

Permalink
Re-enable orphaned attachment cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg-signal committed Aug 16, 2018
1 parent 15751f3 commit 9f920aa
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
16 changes: 14 additions & 2 deletions app/attachment_channel.js
Expand Up @@ -11,8 +11,9 @@ module.exports = {
let initialized = false;

const ERASE_ATTACHMENTS_KEY = 'erase-attachments';
const CLEANUP_ORPHANED_ATTACHMENTS_KEY = 'cleanup-orphaned-attachments';

async function initialize({ configDir }) {
async function initialize({ configDir, cleanupOrphanedAttachments }) {
if (initialized) {
throw new Error('initialze: Already initialized!');
}
Expand All @@ -29,8 +30,19 @@ async function initialize({ configDir }) {
event.sender.send(`${ERASE_ATTACHMENTS_KEY}-done`);
} catch (error) {
const errorForDisplay = error && error.stack ? error.stack : error;
console.log(`sql-erase error: ${errorForDisplay}`);
console.log(`erase attachments error: ${errorForDisplay}`);
event.sender.send(`${ERASE_ATTACHMENTS_KEY}-done`, error);
}
});

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);
}
});
}
4 changes: 4 additions & 0 deletions js/background.js
Expand Up @@ -408,6 +408,10 @@
);
window.log.info('Cleanup: complete');

if (newVersion) {
await window.Signal.Data.cleanupOrphanedAttachments();
}

Views.Initialization.setMessage(window.i18n('loading'));

// Note: We are not invoking the second set of IndexedDB migrations because it is
Expand Down
6 changes: 6 additions & 0 deletions js/modules/data.js
Expand Up @@ -22,6 +22,7 @@ const DATABASE_UPDATE_TIMEOUT = 2 * 60 * 1000; // two minutes
const SQL_CHANNEL_KEY = 'sql-channel';
const ERASE_SQL_KEY = 'erase-sql-key';
const ERASE_ATTACHMENTS_KEY = 'erase-attachments';
const CLEANUP_ORPHANED_ATTACHMENTS_KEY = 'cleanup-orphaned-attachments';

const _jobs = Object.create(null);
const _DEBUG = false;
Expand Down Expand Up @@ -64,6 +65,7 @@ module.exports = {

removeAll,
removeOtherData,
cleanupOrphanedAttachments,

// Returning plain JSON
getMessagesNeedingUpgrade,
Expand Down Expand Up @@ -388,6 +390,10 @@ async function removeAll() {
await channels.removeAll();
}

async function cleanupOrphanedAttachments() {
await callChannel(CLEANUP_ORPHANED_ATTACHMENTS_KEY);
}

// Note: will need to restart the app after calling this, to set up afresh
async function removeOtherData() {
await Promise.all([
Expand Down
25 changes: 16 additions & 9 deletions main.js
Expand Up @@ -26,7 +26,7 @@ const packageJson = require('./package.json');

const sql = require('./app/sql');
const sqlChannels = require('./app/sql_channel');
// const attachments = require('./app/attachments');
const attachments = require('./app/attachments');
const attachmentChannel = require('./app/attachment_channel');
const autoUpdate = require('./app/auto_update');
const createTrayIcon = require('./app/tray_icon');
Expand Down Expand Up @@ -618,8 +618,6 @@ app.on('ready', async () => {
locale = loadLocale({ appLocale, logger });
}

await attachmentChannel.initialize({ configDir: userDataPath });

let key = userConfig.get('key');
if (!key) {
// https://www.zetetic.net/sqlcipher/sqlcipher-api/#key
Expand All @@ -630,12 +628,21 @@ app.on('ready', async () => {
await sql.initialize({ configDir: userDataPath, key });
await sqlChannels.initialize({ userConfig });

// const allAttachments = await attachments.getAllAttachments(userDataPath);
// const orphanedAttachments = await sql.removeKnownAttachments(allAttachments);
// await attachments.deleteAll({
// userDataPath,
// attachments: orphanedAttachments,
// });
async function cleanupOrphanedAttachments() {
const allAttachments = await attachments.getAllAttachments(userDataPath);
const orphanedAttachments = await sql.removeKnownAttachments(
allAttachments
);
await attachments.deleteAll({
userDataPath,
attachments: orphanedAttachments,
});
}

await attachmentChannel.initialize({
configDir: userDataPath,
cleanupOrphanedAttachments,
});

ready = true;

Expand Down

0 comments on commit 9f920aa

Please sign in to comment.