From 9d25aa4e43a9951cd3da888b12bb80f414007ef7 Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Tue, 20 Mar 2018 15:03:26 -0400 Subject: [PATCH] Delete attachment files on disk upon message delete --- js/models/messages.js | 18 ++++++++++++++++-- preload.js | 2 ++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/js/models/messages.js b/js/models/messages.js index 8cf9fb9b546..fdbcc98e7d1 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -1,8 +1,11 @@ +/* eslint-disable */ + (function () { 'use strict'; window.Whisper = window.Whisper || {}; - const { Message: TypedMessage } = window.Signal.Types; + const { Attachment, Message: TypedMessage } = window.Signal.Types; + const { context: migrationContext } = window.Signal.Migrations; var Message = window.Whisper.Message = Backbone.Model.extend({ database : Whisper.Database, @@ -13,7 +16,7 @@ } this.on('change:attachments', this.updateImageUrl); - this.on('destroy', this.revokeImageUrl); + this.on('destroy', this.onDestroy); this.on('change:expirationStartTimestamp', this.setToExpire); this.on('change:expireTimer', this.setToExpire); this.on('unload', this.revokeImageUrl); @@ -139,6 +142,17 @@ return ''; }, + /* eslint-enable */ + /* jshint ignore:start */ + async onDestroy() { + this.revokeImageUrl(); + const attachments = this.get('attachments'); + const deleteData = + Attachment.deleteData(migrationContext.deleteAttachmentData); + await Promise.all(attachments.map(deleteData)); + }, + /* jshint ignore:end */ + /* eslint-disable */ updateImageUrl: function() { this.revokeImageUrl(); var attachment = this.get('attachments')[0]; diff --git a/preload.js b/preload.js index c2d3d1a33c2..cc30a4b4e14 100644 --- a/preload.js +++ b/preload.js @@ -109,6 +109,7 @@ // ES2015+ modules const attachmentsPath = Attachments.getPath(app.getPath('userData')); + const deleteAttachmentData = Attachments.deleteData(attachmentsPath); const readAttachmentData = Attachments.readData(attachmentsPath); const writeAttachmentData = Attachments.writeData(attachmentsPath); @@ -121,6 +122,7 @@ window.Signal.Migrations = window.Signal.Migrations || {}; // Injected context functions to keep `Message` agnostic from Electron: window.Signal.Migrations.context = { + deleteAttachmentData, readAttachmentData, writeAttachmentData, };