fix: check if path exists for deleted attachment instead of trying to read that data

this is more performant since it should be deleted
pull/2660/head
William Grant 2 years ago
parent 778f575bb6
commit c759eed0d8

@ -10,6 +10,7 @@ import {
autoOrientJPEGAttachment, autoOrientJPEGAttachment,
captureDimensionsAndScreenshot, captureDimensionsAndScreenshot,
deleteData, deleteData,
deleteDataSuccessful,
loadData, loadData,
replaceUnicodeV2, replaceUnicodeV2,
} from './attachments/migrations'; } from './attachments/migrations';
@ -30,14 +31,14 @@ export const deleteExternalMessageFiles = async (message: {
await Promise.all(attachments.map(deleteData)); await Promise.all(attachments.map(deleteData));
// test that the files were deleted successfully // test that the files were deleted successfully
try { try {
await Promise.all( let results = await Promise.allSettled(attachments.map(deleteDataSuccessful));
attachments.map(async (attachment: { path: string; thumbnail: any; screenshot: any }) => { results = results.filter(result => result.status === 'rejected');
await readAttachmentData(attachment.path);
}) if (results.length) {
); throw Error;
window.log.info('[deleteExternalMessageFiles]: Failed to delete attachments for', message); }
} catch (err) { } 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);
} }
} }

@ -2,6 +2,7 @@ import * as GoogleChrome from '../../../ts/util/GoogleChrome';
import * as MIME from '../../../ts/types/MIME'; import * as MIME from '../../../ts/types/MIME';
import { toLogFormat } from './Errors'; import { toLogFormat } from './Errors';
import { arrayBufferToBlob, blobToArrayBuffer } from 'blob-util'; import { arrayBufferToBlob, blobToArrayBuffer } from 'blob-util';
import fse from 'fs-extra';
import { isString } from 'lodash'; import { isString } from 'lodash';
import { import {
@ -170,6 +171,27 @@ export const deleteData = async (attachment: { path: string; thumbnail: any; scr
return attachment; 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 }; type CaptureDimensionType = { contentType: string; path: string };
export const captureDimensionsAndScreenshot = async ( export const captureDimensionsAndScreenshot = async (

Loading…
Cancel
Save