Co-authored-by: scott@signal.org
Co-authored-by: ken@signal.org
This commit is contained in:
Ken Powers 2019-05-16 15:32:11 -07:00 committed by Scott Nonnenberg
parent 8c8856785b
commit 29de50c12a
100 changed files with 7572 additions and 693 deletions

View file

@ -8,6 +8,7 @@ const toArrayBuffer = require('to-arraybuffer');
const { map, isArrayBuffer, isString } = require('lodash');
const PATH = 'attachments.noindex';
const STICKER_PATH = 'stickers.noindex';
exports.getAllAttachments = async userDataPath => {
const dir = exports.getPath(userDataPath);
@ -17,6 +18,14 @@ exports.getAllAttachments = async userDataPath => {
return map(files, file => path.relative(dir, file));
};
exports.getAllStickers = async userDataPath => {
const dir = exports.getStickersPath(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)) {
@ -25,12 +34,12 @@ exports.getPath = userDataPath => {
return path.join(userDataPath, PATH);
};
// ensureDirectory :: AbsolutePath -> IO Unit
exports.ensureDirectory = async userDataPath => {
// getStickersPath :: AbsolutePath -> AbsolutePath
exports.getStickersPath = userDataPath => {
if (!isString(userDataPath)) {
throw new TypeError("'userDataPath' must be a string");
}
await fse.ensureDir(exports.getPath(userDataPath));
return path.join(userDataPath, STICKER_PATH);
};
// createReader :: AttachmentsPath ->
@ -56,6 +65,30 @@ exports.createReader = root => {
};
};
exports.copyIntoAttachmentsDirectory = root => {
if (!isString(root)) {
throw new TypeError("'root' must be a path");
}
return async sourcePath => {
if (!isString(sourcePath)) {
throw new TypeError('sourcePath must be a string');
}
const name = exports.createName();
const relativePath = exports.getRelativePath(name);
const absolutePath = path.join(root, relativePath);
const normalized = path.normalize(absolutePath);
if (!normalized.startsWith(root)) {
throw new Error('Invalid relative path');
}
await fse.ensureFile(normalized);
await fse.copy(sourcePath, normalized);
return relativePath;
};
};
// createWriterForNew :: AttachmentsPath ->
// ArrayBuffer ->
// IO (Promise RelativePath)
@ -142,6 +175,20 @@ exports.deleteAll = async ({ userDataPath, attachments }) => {
console.log(`deleteAll: deleted ${attachments.length} files`);
};
exports.deleteAllStickers = async ({ userDataPath, stickers }) => {
const deleteFromDisk = exports.createDeleter(
exports.getStickersPath(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(`deleteAllStickers: deleted ${stickers.length} files`);
};
// createName :: Unit -> IO String
exports.createName = () => {
const buffer = crypto.randomBytes(32);