Eliminate orphaned external message files on startup

Attachments, visual attachment thumbnails, video attachment screenshots
Quote thumbnails
Contact avatars
This commit is contained in:
Scott Nonnenberg 2018-08-06 16:18:58 -07:00
parent 44dec45995
commit 6e193456f9
5 changed files with 140 additions and 3 deletions

View file

@ -1,12 +1,22 @@
const crypto = require('crypto');
const path = require('path');
const pify = require('pify');
const glob = require('glob');
const fse = require('fs-extra');
const toArrayBuffer = require('to-arraybuffer');
const { isArrayBuffer, isString } = require('lodash');
const { map, isArrayBuffer, isString } = require('lodash');
const PATH = 'attachments.noindex';
exports.getAllAttachments = async userDataPath => {
const dir = exports.getPath(userDataPath);
const pattern = path.join(dir, '**', '*');
const files = await pify(glob)(pattern, { nodir: true });
return map(files, file => path.relative(dir, file));
};
// getPath :: AbsolutePath -> AbsolutePath
exports.getPath = userDataPath => {
if (!isString(userDataPath)) {
@ -120,6 +130,18 @@ exports.createDeleter = root => {
};
};
exports.deleteAll = async ({ userDataPath, attachments }) => {
const deleteFromDisk = exports.createDeleter(exports.getPath(userDataPath));
for (let index = 0, max = attachments.length; index < max; index += 1) {
const file = attachments[index];
// eslint-disable-next-line no-await-in-loop
await deleteFromDisk(file);
}
console.log(`deleteAll: deleted ${attachments.length} files`);
};
// createName :: Unit -> IO String
exports.createName = () => {
const buffer = crypto.randomBytes(32);