2018-03-16 21:32:17 +00:00
|
|
|
const crypto = require('crypto');
|
|
|
|
const path = require('path');
|
2020-01-15 22:23:02 +00:00
|
|
|
const { app, dialog, shell, remote } = require('electron');
|
2018-04-02 19:08:53 +00:00
|
|
|
|
2020-02-21 23:40:04 +00:00
|
|
|
const fastGlob = require('fast-glob');
|
2020-02-27 01:53:39 +00:00
|
|
|
const glob = require('glob');
|
|
|
|
const pify = require('pify');
|
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');
|
2020-02-27 01:53:39 +00:00
|
|
|
const normalizePath = require('normalize-path');
|
2020-01-09 19:57:43 +00:00
|
|
|
const sanitizeFilename = require('sanitize-filename');
|
|
|
|
const getGuid = require('uuid/v4');
|
2020-09-09 00:46:29 +00:00
|
|
|
const { isPathInside } = require('../ts/util/isPathInside');
|
2020-09-11 21:17:07 +00:00
|
|
|
const { isWindows } = require('../ts/OS');
|
|
|
|
const {
|
|
|
|
writeWindowsZoneIdentifier,
|
|
|
|
} = require('../ts/util/windowsZoneIdentifier');
|
2020-01-09 19:57:43 +00:00
|
|
|
|
|
|
|
let xattr;
|
|
|
|
try {
|
2020-01-14 20:32:49 +00:00
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
// eslint-disable-next-line global-require, import/no-extraneous-dependencies, import/no-unresolved
|
2020-01-09 19:57:43 +00:00
|
|
|
xattr = require('fs-xattr');
|
|
|
|
} catch (e) {
|
2020-11-03 00:46:49 +00:00
|
|
|
console.log('x-attr dependency did not load successfully');
|
2020-01-09 19:57:43 +00:00
|
|
|
}
|
2018-03-16 21:32:17 +00:00
|
|
|
|
2018-03-20 22:03:22 +00:00
|
|
|
const PATH = 'attachments.noindex';
|
2019-05-16 22:32:11 +00:00
|
|
|
const STICKER_PATH = 'stickers.noindex';
|
2019-05-24 01:27:42 +00:00
|
|
|
const TEMP_PATH = 'temp';
|
2019-08-07 00:40:25 +00:00
|
|
|
const DRAFT_PATH = 'drafts.noindex';
|
2018-03-16 21:32:17 +00:00
|
|
|
|
2020-08-27 18:08:37 +00:00
|
|
|
const getApp = () => app || remote.app;
|
|
|
|
|
2018-08-06 23:18:58 +00:00
|
|
|
exports.getAllAttachments = async userDataPath => {
|
|
|
|
const dir = exports.getPath(userDataPath);
|
2020-02-27 01:53:39 +00:00
|
|
|
const pattern = normalizePath(path.join(dir, '**', '*'));
|
2018-08-06 23:18:58 +00:00
|
|
|
|
2020-02-21 23:40:04 +00:00
|
|
|
const files = await fastGlob(pattern, { onlyFiles: true });
|
2018-08-06 23:18:58 +00:00
|
|
|
return map(files, file => path.relative(dir, file));
|
|
|
|
};
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
exports.getAllStickers = async userDataPath => {
|
|
|
|
const dir = exports.getStickersPath(userDataPath);
|
2020-02-27 01:53:39 +00:00
|
|
|
const pattern = normalizePath(path.join(dir, '**', '*'));
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2020-02-21 23:40:04 +00:00
|
|
|
const files = await fastGlob(pattern, { onlyFiles: true });
|
2019-05-16 22:32:11 +00:00
|
|
|
return map(files, file => path.relative(dir, file));
|
|
|
|
};
|
|
|
|
|
2019-08-07 00:40:25 +00:00
|
|
|
exports.getAllDraftAttachments = async userDataPath => {
|
|
|
|
const dir = exports.getDraftPath(userDataPath);
|
2020-02-27 01:53:39 +00:00
|
|
|
const pattern = normalizePath(path.join(dir, '**', '*'));
|
2019-08-07 00:40:25 +00:00
|
|
|
|
2020-02-21 23:40:04 +00:00
|
|
|
const files = await fastGlob(pattern, { onlyFiles: true });
|
2019-08-07 00:40:25 +00:00
|
|
|
return map(files, file => path.relative(dir, file));
|
|
|
|
};
|
|
|
|
|
2019-10-04 18:06:17 +00:00
|
|
|
exports.getBuiltInImages = async () => {
|
|
|
|
const dir = path.join(__dirname, '../images');
|
|
|
|
const pattern = path.join(dir, '**', '*.svg');
|
|
|
|
|
2020-02-27 01:53:39 +00:00
|
|
|
// Note: we cannot use fast-glob here because, inside of .asar files, readdir will not
|
|
|
|
// honor the withFileTypes flag: https://github.com/electron/electron/issues/19074
|
|
|
|
const files = await pify(glob)(pattern, { nodir: true });
|
2019-10-04 18:06:17 +00:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
// getStickersPath :: AbsolutePath -> AbsolutePath
|
|
|
|
exports.getStickersPath = 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
|
|
|
}
|
2019-05-16 22:32:11 +00:00
|
|
|
return path.join(userDataPath, STICKER_PATH);
|
2018-03-16 21:32:17 +00:00
|
|
|
};
|
|
|
|
|
2019-05-24 01:27:42 +00:00
|
|
|
// getTempPath :: AbsolutePath -> AbsolutePath
|
|
|
|
exports.getTempPath = userDataPath => {
|
|
|
|
if (!isString(userDataPath)) {
|
|
|
|
throw new TypeError("'userDataPath' must be a string");
|
|
|
|
}
|
|
|
|
return path.join(userDataPath, TEMP_PATH);
|
|
|
|
};
|
|
|
|
|
2019-08-07 00:40:25 +00:00
|
|
|
// getDraftPath :: AbsolutePath -> AbsolutePath
|
|
|
|
exports.getDraftPath = userDataPath => {
|
|
|
|
if (!isString(userDataPath)) {
|
|
|
|
throw new TypeError("'userDataPath' must be a string");
|
|
|
|
}
|
|
|
|
return path.join(userDataPath, DRAFT_PATH);
|
|
|
|
};
|
|
|
|
|
2019-05-24 01:27:42 +00:00
|
|
|
// clearTempPath :: AbsolutePath -> AbsolutePath
|
|
|
|
exports.clearTempPath = userDataPath => {
|
|
|
|
const tempPath = exports.getTempPath(userDataPath);
|
|
|
|
return fse.emptyDir(tempPath);
|
|
|
|
};
|
|
|
|
|
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);
|
2020-08-27 18:08:37 +00:00
|
|
|
if (!isPathInside(normalized, root)) {
|
2018-05-19 01:08:22 +00:00
|
|
|
throw new Error('Invalid relative path');
|
|
|
|
}
|
|
|
|
const buffer = await fse.readFile(normalized);
|
2018-03-16 21:32:17 +00:00
|
|
|
return toArrayBuffer(buffer);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-12-03 20:02:50 +00:00
|
|
|
exports.createDoesExist = root => {
|
|
|
|
if (!isString(root)) {
|
|
|
|
throw new TypeError("'root' must be a path");
|
|
|
|
}
|
|
|
|
|
|
|
|
return async relativePath => {
|
|
|
|
if (!isString(relativePath)) {
|
|
|
|
throw new TypeError("'relativePath' must be a string");
|
|
|
|
}
|
|
|
|
|
|
|
|
const absolutePath = path.join(root, relativePath);
|
|
|
|
const normalized = path.normalize(absolutePath);
|
2020-08-27 18:08:37 +00:00
|
|
|
if (!isPathInside(normalized, root)) {
|
2019-12-03 20:02:50 +00:00
|
|
|
throw new Error('Invalid relative path');
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await fse.access(normalized, fse.constants.F_OK);
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
exports.copyIntoAttachmentsDirectory = root => {
|
|
|
|
if (!isString(root)) {
|
|
|
|
throw new TypeError("'root' must be a path");
|
|
|
|
}
|
|
|
|
|
2020-08-27 18:08:37 +00:00
|
|
|
const userDataPath = getApp().getPath('userData');
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
return async sourcePath => {
|
|
|
|
if (!isString(sourcePath)) {
|
|
|
|
throw new TypeError('sourcePath must be a string');
|
|
|
|
}
|
|
|
|
|
2020-08-27 18:08:37 +00:00
|
|
|
if (!isPathInside(sourcePath, userDataPath)) {
|
|
|
|
throw new Error(
|
|
|
|
"'sourcePath' must be relative to the user config directory"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
const name = exports.createName();
|
|
|
|
const relativePath = exports.getRelativePath(name);
|
|
|
|
const absolutePath = path.join(root, relativePath);
|
|
|
|
const normalized = path.normalize(absolutePath);
|
2020-08-27 18:08:37 +00:00
|
|
|
if (!isPathInside(normalized, root)) {
|
2019-05-16 22:32:11 +00:00
|
|
|
throw new Error('Invalid relative path');
|
|
|
|
}
|
|
|
|
|
|
|
|
await fse.ensureFile(normalized);
|
|
|
|
await fse.copy(sourcePath, normalized);
|
|
|
|
return relativePath;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-01-09 19:57:43 +00:00
|
|
|
exports.writeToDownloads = async ({ data, name }) => {
|
2020-08-27 18:08:37 +00:00
|
|
|
const appToUse = getApp();
|
2020-01-09 19:57:43 +00:00
|
|
|
const downloadsPath =
|
|
|
|
appToUse.getPath('downloads') || appToUse.getPath('home');
|
|
|
|
const sanitized = sanitizeFilename(name);
|
|
|
|
|
|
|
|
const extension = path.extname(sanitized);
|
|
|
|
const basename = path.basename(sanitized, extension);
|
|
|
|
const getCandidateName = count => `${basename} (${count})${extension}`;
|
|
|
|
|
|
|
|
const existingFiles = await fse.readdir(downloadsPath);
|
|
|
|
let candidateName = sanitized;
|
|
|
|
let count = 0;
|
|
|
|
while (existingFiles.includes(candidateName)) {
|
|
|
|
count += 1;
|
|
|
|
candidateName = getCandidateName(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
const target = path.join(downloadsPath, candidateName);
|
|
|
|
const normalized = path.normalize(target);
|
2020-08-27 18:08:37 +00:00
|
|
|
if (!isPathInside(normalized, downloadsPath)) {
|
2020-01-09 19:57:43 +00:00
|
|
|
throw new Error('Invalid filename!');
|
|
|
|
}
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
await writeWithAttributes(normalized, Buffer.from(data));
|
2020-01-15 22:23:02 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
fullPath: normalized,
|
|
|
|
name: candidateName,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
async function writeWithAttributes(target, data) {
|
|
|
|
await fse.writeFile(target, Buffer.from(data));
|
2020-01-09 19:57:43 +00:00
|
|
|
|
|
|
|
if (process.platform === 'darwin' && xattr) {
|
|
|
|
// kLSQuarantineTypeInstantMessageAttachment
|
|
|
|
const type = '0003';
|
|
|
|
|
|
|
|
// Hexadecimal seconds since epoch
|
|
|
|
const timestamp = Math.trunc(Date.now() / 1000).toString(16);
|
|
|
|
|
|
|
|
const appName = 'Signal';
|
|
|
|
const guid = getGuid();
|
|
|
|
|
|
|
|
// https://ilostmynotes.blogspot.com/2012/06/gatekeeper-xprotect-and-quarantine.html
|
|
|
|
const attrValue = `${type};${timestamp};${appName};${guid}`;
|
|
|
|
|
2020-01-15 22:23:02 +00:00
|
|
|
await xattr.set(target, 'com.apple.quarantine', attrValue);
|
2020-09-11 21:17:07 +00:00
|
|
|
} else if (isWindows()) {
|
|
|
|
// This operation may fail (see the function's comments), which is not a show-stopper.
|
|
|
|
try {
|
|
|
|
await writeWindowsZoneIdentifier(target);
|
|
|
|
} catch (err) {
|
|
|
|
console.warn('Failed to write Windows Zone.Identifier file; continuing');
|
|
|
|
}
|
2020-01-09 19:57:43 +00:00
|
|
|
}
|
2020-01-15 22:23:02 +00:00
|
|
|
}
|
2020-01-09 19:57:43 +00:00
|
|
|
|
|
|
|
exports.openFileInDownloads = async name => {
|
|
|
|
const shellToUse = shell || remote.shell;
|
2020-08-27 18:08:37 +00:00
|
|
|
const appToUse = getApp();
|
2020-01-09 19:57:43 +00:00
|
|
|
|
|
|
|
const downloadsPath =
|
|
|
|
appToUse.getPath('downloads') || appToUse.getPath('home');
|
|
|
|
const target = path.join(downloadsPath, name);
|
|
|
|
|
|
|
|
const normalized = path.normalize(target);
|
2020-08-27 18:08:37 +00:00
|
|
|
if (!isPathInside(normalized, downloadsPath)) {
|
2020-01-09 19:57:43 +00:00
|
|
|
throw new Error('Invalid filename!');
|
|
|
|
}
|
|
|
|
|
|
|
|
shellToUse.showItemInFolder(normalized);
|
|
|
|
};
|
|
|
|
|
2020-01-15 22:23:02 +00:00
|
|
|
exports.saveAttachmentToDisk = async ({ data, name }) => {
|
|
|
|
const dialogToUse = dialog || remote.dialog;
|
|
|
|
const browserWindow = remote.getCurrentWindow();
|
|
|
|
|
|
|
|
const { canceled, filePath } = await dialogToUse.showSaveDialog(
|
|
|
|
browserWindow,
|
|
|
|
{
|
|
|
|
defaultPath: name,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (canceled) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
await writeWithAttributes(filePath, Buffer.from(data));
|
|
|
|
|
|
|
|
const basename = path.basename(filePath);
|
|
|
|
|
|
|
|
return {
|
|
|
|
fullPath: filePath,
|
|
|
|
name: basename,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.openFileInFolder = async target => {
|
|
|
|
const shellToUse = shell || remote.shell;
|
|
|
|
|
|
|
|
shellToUse.showItemInFolder(target);
|
|
|
|
};
|
|
|
|
|
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);
|
2020-08-27 18:08:37 +00:00
|
|
|
if (!isPathInside(normalized, root)) {
|
2018-05-19 01:08:22 +00:00
|
|
|
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);
|
2020-08-27 18:08:37 +00:00
|
|
|
if (!isPathInside(normalized, root)) {
|
2018-05-19 01:08:22 +00:00
|
|
|
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`);
|
|
|
|
};
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
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`);
|
|
|
|
};
|
|
|
|
|
2019-08-07 00:40:25 +00:00
|
|
|
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`);
|
|
|
|
};
|
|
|
|
|
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);
|
2020-08-27 18:08:37 +00:00
|
|
|
if (!isPathInside(normalized, rootPath)) {
|
2018-05-19 01:08:22 +00:00
|
|
|
throw new Error('Invalid relative path');
|
|
|
|
}
|
|
|
|
return normalized;
|
|
|
|
};
|