diff --git a/app/attachment_channel.js b/app/attachment_channel.js index 46a4b9ec88f..c5e22975150 100644 --- a/app/attachment_channel.js +++ b/app/attachment_channel.js @@ -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!'); } @@ -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); + } + }); } diff --git a/js/background.js b/js/background.js index b9f6277d99c..d342e6f2a3b 100644 --- a/js/background.js +++ b/js/background.js @@ -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 diff --git a/js/modules/data.js b/js/modules/data.js index 0092cb99ef3..31b41b4add5 100644 --- a/js/modules/data.js +++ b/js/modules/data.js @@ -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; @@ -64,6 +65,7 @@ module.exports = { removeAll, removeOtherData, + cleanupOrphanedAttachments, // Returning plain JSON getMessagesNeedingUpgrade, @@ -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([ diff --git a/main.js b/main.js index 8bfda80183d..763798dbeb5 100644 --- a/main.js +++ b/main.js @@ -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'); @@ -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 @@ -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;