Persist drafts

This commit is contained in:
Scott Nonnenberg 2019-08-06 17:40:25 -07:00
parent 5ebd8bc690
commit 9d4f2afa5a
23 changed files with 1048 additions and 720 deletions

View file

@ -10,6 +10,7 @@ const { map, isArrayBuffer, isString } = require('lodash');
const PATH = 'attachments.noindex';
const STICKER_PATH = 'stickers.noindex';
const TEMP_PATH = 'temp';
const DRAFT_PATH = 'drafts.noindex';
exports.getAllAttachments = async userDataPath => {
const dir = exports.getPath(userDataPath);
@ -27,6 +28,14 @@ exports.getAllStickers = async userDataPath => {
return map(files, file => path.relative(dir, file));
};
exports.getAllDraftAttachments = async userDataPath => {
const dir = exports.getDraftPath(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)) {
@ -51,6 +60,14 @@ exports.getTempPath = userDataPath => {
return path.join(userDataPath, TEMP_PATH);
};
// getDraftPath :: AbsolutePath -> AbsolutePath
exports.getDraftPath = userDataPath => {
if (!isString(userDataPath)) {
throw new TypeError("'userDataPath' must be a string");
}
return path.join(userDataPath, DRAFT_PATH);
};
// clearTempPath :: AbsolutePath -> AbsolutePath
exports.clearTempPath = userDataPath => {
const tempPath = exports.getTempPath(userDataPath);
@ -204,6 +221,20 @@ exports.deleteAllStickers = async ({ userDataPath, stickers }) => {
console.log(`deleteAllStickers: deleted ${stickers.length} files`);
};
exports.deleteAllDraftAttachments = async ({ userDataPath, stickers }) => {
const deleteFromDisk = exports.createDeleter(
exports.getDraftPath(userDataPath)
);
for (let index = 0, max = stickers.length; index < max; index += 1) {
const file = stickers[index];
// eslint-disable-next-line no-await-in-loop
await deleteFromDisk(file);
}
console.log(`deleteAllDraftAttachments: deleted ${stickers.length} files`);
};
// createName :: Unit -> IO String
exports.createName = () => {
const buffer = crypto.randomBytes(32);