diff --git a/ts/types/MessageAttachment.ts b/ts/types/MessageAttachment.ts index 199d33908..683efd24f 100644 --- a/ts/types/MessageAttachment.ts +++ b/ts/types/MessageAttachment.ts @@ -10,6 +10,7 @@ import { autoOrientJPEGAttachment, captureDimensionsAndScreenshot, deleteData, + deleteDataSuccessful, loadData, replaceUnicodeV2, } from './attachments/migrations'; @@ -30,14 +31,14 @@ export const deleteExternalMessageFiles = async (message: { await Promise.all(attachments.map(deleteData)); // test that the files were deleted successfully try { - await Promise.all( - attachments.map(async (attachment: { path: string; thumbnail: any; screenshot: any }) => { - await readAttachmentData(attachment.path); - }) - ); - window.log.info('[deleteExternalMessageFiles]: Failed to delete attachments for', message); + let results = await Promise.allSettled(attachments.map(deleteDataSuccessful)); + results = results.filter(result => result.status === 'rejected'); + + if (results.length) { + throw Error; + } } catch (err) { - // If we fail to read the path then we know we deleted successfully + window.log.warn('[deleteExternalMessageFiles]: Failed to delete attachments for', message); } } diff --git a/ts/types/attachments/migrations.ts b/ts/types/attachments/migrations.ts index 44221d420..0624b10e9 100644 --- a/ts/types/attachments/migrations.ts +++ b/ts/types/attachments/migrations.ts @@ -2,6 +2,7 @@ import * as GoogleChrome from '../../../ts/util/GoogleChrome'; import * as MIME from '../../../ts/types/MIME'; import { toLogFormat } from './Errors'; import { arrayBufferToBlob, blobToArrayBuffer } from 'blob-util'; +import fse from 'fs-extra'; import { isString } from 'lodash'; import { @@ -170,6 +171,27 @@ export const deleteData = async (attachment: { path: string; thumbnail: any; scr return attachment; }; +export const deleteDataSuccessful = async (attachment: { + path: string; + thumbnail: any; + screenshot: any; +}) => { + const errorMessage = `deleteDataSuccessful: Deletion failed for attachment ${attachment.path}`; + return fse.pathExists(attachment.path, (err, exists) => { + if (err) { + return Promise.reject(`${errorMessage} ${err}`); + } + + // Note we want to confirm the path no longer exists + if (exists) { + return Promise.reject(errorMessage); + } + + window.log.debug(`deleteDataSuccessful: Deletion succeeded for attachment ${attachment.path}`); + return true; + }); +}; + type CaptureDimensionType = { contentType: string; path: string }; export const captureDimensionsAndScreenshot = async (