From d4d5b6c86b6b43919c35b3297ffdbc9a36ca79d6 Mon Sep 17 00:00:00 2001 From: William Grant Date: Thu, 27 Apr 2023 13:44:14 +1000 Subject: [PATCH] fix: improved disk deletion handling and pass yarn ready need to use await with the pathExists function and cleaned up code into a handler function --- ts/types/MessageAttachment.ts | 12 ------- ts/types/attachments/migrations.ts | 50 +++++++++++++----------------- 2 files changed, 22 insertions(+), 40 deletions(-) diff --git a/ts/types/MessageAttachment.ts b/ts/types/MessageAttachment.ts index 683efd24f..2f7a8b76d 100644 --- a/ts/types/MessageAttachment.ts +++ b/ts/types/MessageAttachment.ts @@ -10,7 +10,6 @@ import { autoOrientJPEGAttachment, captureDimensionsAndScreenshot, deleteData, - deleteDataSuccessful, loadData, replaceUnicodeV2, } from './attachments/migrations'; @@ -29,17 +28,6 @@ export const deleteExternalMessageFiles = async (message: { if (attachments && attachments.length) { await Promise.all(attachments.map(deleteData)); - // test that the files were deleted successfully - try { - let results = await Promise.allSettled(attachments.map(deleteDataSuccessful)); - results = results.filter(result => result.status === 'rejected'); - - if (results.length) { - throw Error; - } - } catch (err) { - window.log.warn('[deleteExternalMessageFiles]: Failed to delete attachments for', message); - } } if (quote && quote.attachments && quote.attachments.length) { diff --git a/ts/types/attachments/migrations.ts b/ts/types/attachments/migrations.ts index 0624b10e9..d15f2faca 100644 --- a/ts/types/attachments/migrations.ts +++ b/ts/types/attachments/migrations.ts @@ -2,7 +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 { pathExists } from 'fs-extra'; import { isString } from 'lodash'; import { @@ -143,6 +143,24 @@ export const loadData = async (attachment: any) => { return { ...attachment, data }; }; +const handleDiskDeletion = async (path: string) => { + await deleteOnDisk(path); + try { + const exists = await pathExists(path); + + // Note we want to confirm the path no longer exists + if (exists) { + throw Error('Error: File path still exists.'); + } + + window.log.debug(`deleteDataSuccessful: Deletion succeeded for attachment ${path}`); + return undefined; + } catch (err) { + window.log.warn(`deleteDataSuccessful: Deletion failed for attachment ${path} ${err}`); + return path; + } +}; + // deleteData :: (RelativePath -> IO Unit) // Attachment -> // IO Unit @@ -154,44 +172,20 @@ export const deleteData = async (attachment: { path: string; thumbnail: any; scr const { path, thumbnail, screenshot } = attachment; if (isString(path)) { - await deleteOnDisk(path); - attachment.path = ''; + attachment.path = String(await handleDiskDeletion(path)); } if (thumbnail && isString(thumbnail.path)) { - await deleteOnDisk(thumbnail.path); - attachment.thumbnail = undefined; + attachment.thumbnail = await handleDiskDeletion(thumbnail.path); } if (screenshot && isString(screenshot.path)) { - await deleteOnDisk(screenshot.path); - attachment.screenshot = undefined; + attachment.screenshot = await handleDiskDeletion(screenshot.path); } 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 (