2018-03-16 21:32:17 +00:00
|
|
|
const crypto = require('crypto');
|
|
|
|
const path = require('path');
|
2018-04-02 19:08:53 +00:00
|
|
|
|
2018-08-06 23:18:58 +00:00
|
|
|
const pify = require('pify');
|
|
|
|
const glob = require('glob');
|
2018-04-02 19:08:53 +00:00
|
|
|
const fse = require('fs-extra');
|
2018-03-16 21:32:17 +00:00
|
|
|
const toArrayBuffer = require('to-arraybuffer');
|
2018-08-06 23:18:58 +00:00
|
|
|
const { map, isArrayBuffer, isString } = require('lodash');
|
2018-03-16 21:32:17 +00:00
|
|
|
|
2018-03-20 22:03:22 +00:00
|
|
|
const PATH = 'attachments.noindex';
|
2018-03-16 21:32:17 +00:00
|
|
|
|
2018-08-06 23:18:58 +00:00
|
|
|
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));
|
|
|
|
};
|
|
|
|
|
2018-03-16 21:32:17 +00:00
|
|
|
// getPath :: AbsolutePath -> AbsolutePath
|
2018-05-02 01:54:43 +00:00
|
|
|
exports.getPath = userDataPath => {
|
2018-03-16 21:32:17 +00:00
|
|
|
if (!isString(userDataPath)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'userDataPath' must be a string");
|
2018-03-16 21:32:17 +00:00
|
|
|
}
|
|
|
|
return path.join(userDataPath, PATH);
|
|
|
|
};
|
|
|
|
|
|
|
|
// ensureDirectory :: AbsolutePath -> IO Unit
|
2018-05-02 01:54:43 +00:00
|
|
|
exports.ensureDirectory = async userDataPath => {
|
2018-03-16 21:32:17 +00:00
|
|
|
if (!isString(userDataPath)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'userDataPath' must be a string");
|
2018-03-16 21:32:17 +00:00
|
|
|
}
|
|
|
|
await fse.ensureDir(exports.getPath(userDataPath));
|
|
|
|
};
|
|
|
|
|
2018-04-03 19:25:24 +00:00
|
|
|
// createReader :: AttachmentsPath ->
|
|
|
|
// RelativePath ->
|
|
|
|
// IO (Promise ArrayBuffer)
|
2018-05-02 01:54:43 +00:00
|
|
|
exports.createReader = root => {
|
2018-03-16 21:32:17 +00:00
|
|
|
if (!isString(root)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'root' must be a path");
|
2018-03-16 21:32:17 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
return async relativePath => {
|
2018-03-16 21:32:17 +00:00
|
|
|
if (!isString(relativePath)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'relativePath' must be a string");
|
2018-03-16 21:32:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const absolutePath = path.join(root, relativePath);
|
2018-05-19 01:08:22 +00:00
|
|
|
const normalized = path.normalize(absolutePath);
|
|
|
|
if (!normalized.startsWith(root)) {
|
|
|
|
throw new Error('Invalid relative path');
|
|
|
|
}
|
|
|
|
const buffer = await fse.readFile(normalized);
|
2018-03-16 21:32:17 +00:00
|
|
|
return toArrayBuffer(buffer);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-04-04 01:06:29 +00:00
|
|
|
// createWriterForNew :: AttachmentsPath ->
|
|
|
|
// ArrayBuffer ->
|
|
|
|
// IO (Promise RelativePath)
|
2018-05-02 01:54:43 +00:00
|
|
|
exports.createWriterForNew = root => {
|
2018-03-16 21:32:17 +00:00
|
|
|
if (!isString(root)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'root' must be a path");
|
2018-03-16 21:32:17 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
return async arrayBuffer => {
|
2018-03-16 21:32:17 +00:00
|
|
|
if (!isArrayBuffer(arrayBuffer)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'arrayBuffer' must be an array buffer");
|
2018-03-16 21:32:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const name = exports.createName();
|
|
|
|
const relativePath = exports.getRelativePath(name);
|
2018-04-04 01:08:43 +00:00
|
|
|
return exports.createWriterForExisting(root)({
|
|
|
|
data: arrayBuffer,
|
|
|
|
path: relativePath,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// createWriter :: AttachmentsPath ->
|
|
|
|
// { data: ArrayBuffer, path: RelativePath } ->
|
|
|
|
// IO (Promise RelativePath)
|
2018-05-02 01:54:43 +00:00
|
|
|
exports.createWriterForExisting = root => {
|
2018-04-04 01:08:43 +00:00
|
|
|
if (!isString(root)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'root' must be a path");
|
2018-04-04 01:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return async ({ data: arrayBuffer, path: relativePath } = {}) => {
|
|
|
|
if (!isString(relativePath)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'relativePath' must be a path");
|
2018-04-04 01:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isArrayBuffer(arrayBuffer)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'arrayBuffer' must be an array buffer");
|
2018-04-04 01:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const buffer = Buffer.from(arrayBuffer);
|
2018-03-16 21:32:17 +00:00
|
|
|
const absolutePath = path.join(root, relativePath);
|
2018-05-19 01:08:22 +00:00
|
|
|
const normalized = path.normalize(absolutePath);
|
|
|
|
if (!normalized.startsWith(root)) {
|
|
|
|
throw new Error('Invalid relative path');
|
|
|
|
}
|
|
|
|
|
|
|
|
await fse.ensureFile(normalized);
|
|
|
|
await fse.writeFile(normalized, buffer);
|
2018-03-16 21:32:17 +00:00
|
|
|
return relativePath;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-04-03 19:25:24 +00:00
|
|
|
// createDeleter :: AttachmentsPath ->
|
|
|
|
// RelativePath ->
|
|
|
|
// IO Unit
|
2018-05-02 01:54:43 +00:00
|
|
|
exports.createDeleter = root => {
|
2018-03-19 23:50:14 +00:00
|
|
|
if (!isString(root)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'root' must be a path");
|
2018-03-19 23:50:14 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
return async relativePath => {
|
2018-03-19 23:50:14 +00:00
|
|
|
if (!isString(relativePath)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'relativePath' must be a string");
|
2018-03-19 23:50:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const absolutePath = path.join(root, relativePath);
|
2018-05-19 01:08:22 +00:00
|
|
|
const normalized = path.normalize(absolutePath);
|
|
|
|
if (!normalized.startsWith(root)) {
|
|
|
|
throw new Error('Invalid relative path');
|
|
|
|
}
|
2018-03-19 23:50:14 +00:00
|
|
|
await fse.remove(absolutePath);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-08-06 23:18:58 +00:00
|
|
|
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`);
|
|
|
|
};
|
|
|
|
|
2018-03-16 21:32:17 +00:00
|
|
|
// createName :: Unit -> IO String
|
|
|
|
exports.createName = () => {
|
|
|
|
const buffer = crypto.randomBytes(32);
|
|
|
|
return buffer.toString('hex');
|
|
|
|
};
|
|
|
|
|
2018-04-26 20:19:35 +00:00
|
|
|
// getRelativePath :: String -> Path
|
2018-05-02 01:54:43 +00:00
|
|
|
exports.getRelativePath = name => {
|
2018-03-16 21:32:17 +00:00
|
|
|
if (!isString(name)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'name' must be a string");
|
2018-03-16 21:32:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const prefix = name.slice(0, 2);
|
|
|
|
return path.join(prefix, name);
|
|
|
|
};
|
2018-04-26 20:50:19 +00:00
|
|
|
|
2018-07-09 21:29:13 +00:00
|
|
|
// createAbsolutePathGetter :: RootPath -> RelativePath -> AbsolutePath
|
2018-05-19 01:08:22 +00:00
|
|
|
exports.createAbsolutePathGetter = rootPath => relativePath => {
|
|
|
|
const absolutePath = path.join(rootPath, relativePath);
|
|
|
|
const normalized = path.normalize(absolutePath);
|
|
|
|
if (!normalized.startsWith(rootPath)) {
|
|
|
|
throw new Error('Invalid relative path');
|
|
|
|
}
|
|
|
|
return normalized;
|
|
|
|
};
|