On message delete, ensure that all external files are deleted
This commit is contained in:
parent
e80857562a
commit
34231168a7
7 changed files with 73 additions and 79 deletions
|
@ -110,12 +110,14 @@ function initializeMigrations({
|
|||
const readAttachmentData = createReader(attachmentsPath);
|
||||
const loadAttachmentData = Type.loadData(readAttachmentData);
|
||||
const getAbsoluteAttachmentPath = createAbsolutePathGetter(attachmentsPath);
|
||||
const deleteOnDisk = Attachments.createDeleter(attachmentsPath);
|
||||
|
||||
return {
|
||||
attachmentsPath,
|
||||
deleteAttachmentData: Type.deleteData(
|
||||
Attachments.createDeleter(attachmentsPath)
|
||||
),
|
||||
deleteExternalMessageFiles: MessageType.deleteAllExternalFiles({
|
||||
deleteAttachmentData: Type.deleteData(deleteOnDisk),
|
||||
deleteOnDisk,
|
||||
}),
|
||||
getAbsoluteAttachmentPath,
|
||||
getPlaceholderMigrations,
|
||||
loadAttachmentData,
|
||||
|
|
|
@ -158,26 +158,28 @@ exports.loadData = readAttachmentData => {
|
|||
// deleteData :: (RelativePath -> IO Unit)
|
||||
// Attachment ->
|
||||
// IO Unit
|
||||
exports.deleteData = deleteAttachmentData => {
|
||||
if (!is.function(deleteAttachmentData)) {
|
||||
throw new TypeError("'deleteAttachmentData' must be a function");
|
||||
exports.deleteData = deleteOnDisk => {
|
||||
if (!is.function(deleteOnDisk)) {
|
||||
throw new TypeError('deleteData: deleteOnDisk must be a function');
|
||||
}
|
||||
|
||||
return async attachment => {
|
||||
if (!exports.isValid(attachment)) {
|
||||
throw new TypeError("'attachment' is not valid");
|
||||
throw new TypeError('deleteData: attachment is not valid');
|
||||
}
|
||||
|
||||
const hasDataInMemory = exports.hasData(attachment);
|
||||
if (hasDataInMemory) {
|
||||
return;
|
||||
const { path, thumbnail, screenshot } = attachment;
|
||||
if (is.string(path)) {
|
||||
await deleteOnDisk(path);
|
||||
}
|
||||
|
||||
if (!is.string(attachment.path)) {
|
||||
throw new TypeError("'attachment.path' is required");
|
||||
if (thumbnail && is.string(thumbnail.path)) {
|
||||
await deleteOnDisk(thumbnail.path);
|
||||
}
|
||||
|
||||
await deleteAttachmentData(attachment.path);
|
||||
if (screenshot && is.string(screenshot.path)) {
|
||||
await deleteOnDisk(screenshot.path);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
@ -361,6 +361,52 @@ exports.createAttachmentLoader = loadAttachmentData => {
|
|||
});
|
||||
};
|
||||
|
||||
exports.deleteAllExternalFiles = ({ deleteAttachmentData, deleteOnDisk }) => {
|
||||
if (!isFunction(deleteAttachmentData)) {
|
||||
throw new TypeError(
|
||||
'deleteAllExternalFiles: deleteAttachmentData must be a function'
|
||||
);
|
||||
}
|
||||
|
||||
if (!isFunction(deleteOnDisk)) {
|
||||
throw new TypeError(
|
||||
'deleteAllExternalFiles: deleteOnDisk must be a function'
|
||||
);
|
||||
}
|
||||
|
||||
return async message => {
|
||||
const { attachments, quote, contact } = message;
|
||||
|
||||
if (attachments && attachments.length) {
|
||||
await Promise.all(attachments.map(deleteAttachmentData));
|
||||
}
|
||||
|
||||
if (quote && quote.attachments && quote.attachments.length) {
|
||||
await Promise.all(
|
||||
quote.attachments.map(async attachment => {
|
||||
const { thumbnail } = attachment;
|
||||
|
||||
if (thumbnail.path) {
|
||||
await deleteOnDisk(thumbnail.path);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
if (contact && contact.length) {
|
||||
await Promise.all(
|
||||
contact.map(async item => {
|
||||
const { avatar } = item;
|
||||
|
||||
if (avatar && avatar.avatar && avatar.avatar.path) {
|
||||
await deleteOnDisk(avatar.avatar.path);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// createAttachmentDataWriter :: (RelativePath -> IO Unit)
|
||||
// Message ->
|
||||
// IO (Promise Message)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue