2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2018-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
import { randomBytes } from 'crypto';
|
2021-09-24 00:49:05 +00:00
|
|
|
import { basename, join, normalize, relative } from 'path';
|
2021-06-18 17:04:27 +00:00
|
|
|
import { app, dialog, shell, remote } from 'electron';
|
|
|
|
|
|
|
|
import fastGlob from 'fast-glob';
|
|
|
|
import glob from 'glob';
|
|
|
|
import pify from 'pify';
|
|
|
|
import fse from 'fs-extra';
|
2021-09-24 00:49:05 +00:00
|
|
|
import { map, isTypedArray, isString } from 'lodash';
|
2021-06-18 17:04:27 +00:00
|
|
|
import normalizePath from 'normalize-path';
|
|
|
|
import getGuid from 'uuid/v4';
|
|
|
|
|
|
|
|
import { isPathInside } from '../ts/util/isPathInside';
|
|
|
|
import { isWindows } from '../ts/OS';
|
|
|
|
import { writeWindowsZoneIdentifier } from '../ts/util/windowsZoneIdentifier';
|
|
|
|
|
|
|
|
type FSAttrType = {
|
|
|
|
set: (path: string, attribute: string, value: string) => Promise<void>;
|
|
|
|
};
|
|
|
|
|
|
|
|
let xattr: FSAttrType | undefined;
|
|
|
|
|
2020-01-09 19:57:43 +00:00
|
|
|
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';
|
2021-08-06 00:17:05 +00:00
|
|
|
const AVATAR_PATH = 'avatars.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;
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const getAllAttachments = async (
|
|
|
|
userDataPath: string
|
|
|
|
): Promise<ReadonlyArray<string>> => {
|
|
|
|
const dir = getPath(userDataPath);
|
|
|
|
const pattern = normalizePath(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 });
|
2021-06-18 17:04:27 +00:00
|
|
|
return map(files, file => relative(dir, file));
|
2018-08-06 23:18:58 +00:00
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const getAllStickers = async (
|
|
|
|
userDataPath: string
|
|
|
|
): Promise<ReadonlyArray<string>> => {
|
|
|
|
const dir = getStickersPath(userDataPath);
|
|
|
|
const pattern = normalizePath(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 });
|
2021-06-18 17:04:27 +00:00
|
|
|
return map(files, file => relative(dir, file));
|
2019-05-16 22:32:11 +00:00
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const getAllDraftAttachments = async (
|
|
|
|
userDataPath: string
|
|
|
|
): Promise<ReadonlyArray<string>> => {
|
|
|
|
const dir = getDraftPath(userDataPath);
|
|
|
|
const pattern = normalizePath(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 });
|
2021-06-18 17:04:27 +00:00
|
|
|
return map(files, file => relative(dir, file));
|
2019-08-07 00:40:25 +00:00
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const getBuiltInImages = async (): Promise<ReadonlyArray<string>> => {
|
|
|
|
const dir = join(__dirname, '../images');
|
|
|
|
const pattern = join(dir, '**', '*.svg');
|
2019-10-04 18:06:17 +00:00
|
|
|
|
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 });
|
2021-06-18 17:04:27 +00:00
|
|
|
return map(files, file => relative(dir, file));
|
2019-10-04 18:06:17 +00:00
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const getPath = (userDataPath: string): string => {
|
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
|
|
|
}
|
2021-06-18 17:04:27 +00:00
|
|
|
return join(userDataPath, PATH);
|
2018-03-16 21:32:17 +00:00
|
|
|
};
|
|
|
|
|
2021-08-06 00:17:05 +00:00
|
|
|
export const getAvatarsPath = (userDataPath: string): string => {
|
|
|
|
if (!isString(userDataPath)) {
|
|
|
|
throw new TypeError("'userDataPath' must be a string");
|
|
|
|
}
|
|
|
|
return join(userDataPath, AVATAR_PATH);
|
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const getStickersPath = (userDataPath: string): string => {
|
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
|
|
|
}
|
2021-06-18 17:04:27 +00:00
|
|
|
return join(userDataPath, STICKER_PATH);
|
2018-03-16 21:32:17 +00:00
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const getTempPath = (userDataPath: string): string => {
|
2019-05-24 01:27:42 +00:00
|
|
|
if (!isString(userDataPath)) {
|
|
|
|
throw new TypeError("'userDataPath' must be a string");
|
|
|
|
}
|
2021-06-18 17:04:27 +00:00
|
|
|
return join(userDataPath, TEMP_PATH);
|
2019-05-24 01:27:42 +00:00
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const getDraftPath = (userDataPath: string): string => {
|
2019-08-07 00:40:25 +00:00
|
|
|
if (!isString(userDataPath)) {
|
|
|
|
throw new TypeError("'userDataPath' must be a string");
|
|
|
|
}
|
2021-06-18 17:04:27 +00:00
|
|
|
return join(userDataPath, DRAFT_PATH);
|
2019-08-07 00:40:25 +00:00
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const clearTempPath = (userDataPath: string): Promise<void> => {
|
|
|
|
const tempPath = getTempPath(userDataPath);
|
2019-05-24 01:27:42 +00:00
|
|
|
return fse.emptyDir(tempPath);
|
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const createReader = (
|
|
|
|
root: string
|
2021-09-24 00:49:05 +00:00
|
|
|
): ((relativePath: string) => Promise<Uint8Array>) => {
|
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
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
return async (relativePath: string): Promise<Uint8Array> => {
|
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
|
|
|
}
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
const absolutePath = join(root, relativePath);
|
|
|
|
const normalized = 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');
|
|
|
|
}
|
2021-09-24 00:49:05 +00:00
|
|
|
return fse.readFile(normalized);
|
2018-03-16 21:32:17 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const createDoesExist = (
|
|
|
|
root: string
|
|
|
|
): ((relativePath: string) => Promise<boolean>) => {
|
2019-12-03 20:02:50 +00:00
|
|
|
if (!isString(root)) {
|
|
|
|
throw new TypeError("'root' must be a path");
|
|
|
|
}
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
return async (relativePath: string): Promise<boolean> => {
|
2019-12-03 20:02:50 +00:00
|
|
|
if (!isString(relativePath)) {
|
|
|
|
throw new TypeError("'relativePath' must be a string");
|
|
|
|
}
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
const absolutePath = join(root, relativePath);
|
|
|
|
const normalized = 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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const copyIntoAttachmentsDirectory = (
|
|
|
|
root: string
|
2021-10-05 22:10:08 +00:00
|
|
|
): ((sourcePath: string) => Promise<{ path: string; size: number }>) => {
|
2019-05-16 22:32:11 +00:00
|
|
|
if (!isString(root)) {
|
|
|
|
throw new TypeError("'root' must be a path");
|
|
|
|
}
|
|
|
|
|
2020-08-27 18:08:37 +00:00
|
|
|
const userDataPath = getApp().getPath('userData');
|
|
|
|
|
2021-10-05 22:10:08 +00:00
|
|
|
return async (
|
|
|
|
sourcePath: string
|
|
|
|
): Promise<{ path: string; size: number }> => {
|
2019-05-16 22:32:11 +00:00
|
|
|
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"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
const name = createName();
|
|
|
|
const relativePath = getRelativePath(name);
|
|
|
|
const absolutePath = join(root, relativePath);
|
|
|
|
const normalized = 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);
|
2021-10-05 22:10:08 +00:00
|
|
|
const { size } = await fse.stat(normalized);
|
|
|
|
|
|
|
|
return {
|
|
|
|
path: relativePath,
|
|
|
|
size,
|
|
|
|
};
|
2019-05-16 22:32:11 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
async function writeWithAttributes(
|
|
|
|
target: string,
|
2021-09-24 00:49:05 +00:00
|
|
|
data: Uint8Array
|
2021-06-18 17:04:27 +00:00
|
|
|
): Promise<void> {
|
2020-01-15 22:23:02 +00:00
|
|
|
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
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const openFileInDownloads = async (name: string): Promise<void> => {
|
2020-01-09 19:57:43 +00:00
|
|
|
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');
|
2021-06-18 17:04:27 +00:00
|
|
|
const target = join(downloadsPath, name);
|
2020-01-09 19:57:43 +00:00
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
const normalized = 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);
|
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const saveAttachmentToDisk = async ({
|
|
|
|
data,
|
|
|
|
name,
|
|
|
|
}: {
|
2021-09-24 00:49:05 +00:00
|
|
|
data: Uint8Array;
|
2021-06-18 17:04:27 +00:00
|
|
|
name: string;
|
|
|
|
}): Promise<null | { fullPath: string; name: string }> => {
|
2020-01-15 22:23:02 +00:00
|
|
|
const dialogToUse = dialog || remote.dialog;
|
|
|
|
const browserWindow = remote.getCurrentWindow();
|
|
|
|
|
|
|
|
const { canceled, filePath } = await dialogToUse.showSaveDialog(
|
|
|
|
browserWindow,
|
|
|
|
{
|
|
|
|
defaultPath: name,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
if (canceled || !filePath) {
|
2020-01-15 22:23:02 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
await writeWithAttributes(filePath, data);
|
2020-01-15 22:23:02 +00:00
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
const fileBasename = basename(filePath);
|
2020-01-15 22:23:02 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
fullPath: filePath,
|
2021-06-18 17:04:27 +00:00
|
|
|
name: fileBasename,
|
2020-01-15 22:23:02 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const openFileInFolder = async (target: string): Promise<void> => {
|
2020-01-15 22:23:02 +00:00
|
|
|
const shellToUse = shell || remote.shell;
|
|
|
|
|
|
|
|
shellToUse.showItemInFolder(target);
|
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const createWriterForNew = (
|
|
|
|
root: string
|
2021-09-24 00:49:05 +00:00
|
|
|
): ((bytes: Uint8Array) => Promise<string>) => {
|
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
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
return async (bytes: Uint8Array) => {
|
|
|
|
if (!isTypedArray(bytes)) {
|
|
|
|
throw new TypeError("'bytes' must be a typed array");
|
2018-03-16 21:32:17 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
const name = createName();
|
|
|
|
const relativePath = getRelativePath(name);
|
|
|
|
return createWriterForExisting(root)({
|
2021-09-24 00:49:05 +00:00
|
|
|
data: bytes,
|
2018-04-04 01:08:43 +00:00
|
|
|
path: relativePath,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const createWriterForExisting = (
|
|
|
|
root: string
|
2021-09-24 00:49:05 +00:00
|
|
|
): ((options: { data: Uint8Array; path: string }) => Promise<string>) => {
|
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
|
|
|
}
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
return async ({
|
2021-09-24 00:49:05 +00:00
|
|
|
data: bytes,
|
2021-06-18 17:04:27 +00:00
|
|
|
path: relativePath,
|
|
|
|
}: {
|
2021-09-24 00:49:05 +00:00
|
|
|
data: Uint8Array;
|
2021-06-18 17:04:27 +00:00
|
|
|
path: string;
|
|
|
|
}): Promise<string> => {
|
2018-04-04 01:08:43 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
if (!isTypedArray(bytes)) {
|
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
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const buffer = Buffer.from(bytes);
|
2021-06-18 17:04:27 +00:00
|
|
|
const absolutePath = join(root, relativePath);
|
|
|
|
const normalized = 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;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const createDeleter = (
|
|
|
|
root: string
|
|
|
|
): ((relativePath: string) => Promise<void>) => {
|
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
|
|
|
}
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
return async (relativePath: string): Promise<void> => {
|
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
|
|
|
}
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
const absolutePath = join(root, relativePath);
|
|
|
|
const normalized = 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);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const deleteAll = async ({
|
|
|
|
userDataPath,
|
|
|
|
attachments,
|
|
|
|
}: {
|
|
|
|
userDataPath: string;
|
|
|
|
attachments: ReadonlyArray<string>;
|
|
|
|
}): Promise<void> => {
|
|
|
|
const deleteFromDisk = createDeleter(getPath(userDataPath));
|
2018-08-06 23:18:58 +00:00
|
|
|
|
|
|
|
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`);
|
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const deleteAllStickers = async ({
|
|
|
|
userDataPath,
|
|
|
|
stickers,
|
|
|
|
}: {
|
|
|
|
userDataPath: string;
|
|
|
|
stickers: ReadonlyArray<string>;
|
|
|
|
}): Promise<void> => {
|
|
|
|
const deleteFromDisk = createDeleter(getStickersPath(userDataPath));
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
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`);
|
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const deleteAllDraftAttachments = async ({
|
|
|
|
userDataPath,
|
|
|
|
attachments,
|
|
|
|
}: {
|
|
|
|
userDataPath: string;
|
|
|
|
attachments: ReadonlyArray<string>;
|
|
|
|
}): Promise<void> => {
|
|
|
|
const deleteFromDisk = createDeleter(getDraftPath(userDataPath));
|
2019-08-07 00:40:25 +00:00
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
for (let index = 0, max = attachments.length; index < max; index += 1) {
|
|
|
|
const file = attachments[index];
|
2019-08-07 00:40:25 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await deleteFromDisk(file);
|
|
|
|
}
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
console.log(`deleteAllDraftAttachments: deleted ${attachments.length} files`);
|
2019-08-07 00:40:25 +00:00
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const createName = (): string => {
|
|
|
|
const buffer = randomBytes(32);
|
2018-03-16 21:32:17 +00:00
|
|
|
return buffer.toString('hex');
|
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const getRelativePath = (name: string): string => {
|
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);
|
2021-06-18 17:04:27 +00:00
|
|
|
return join(prefix, name);
|
2018-03-16 21:32:17 +00:00
|
|
|
};
|
2018-04-26 20:50:19 +00:00
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const createAbsolutePathGetter = (rootPath: string) => (
|
|
|
|
relativePath: string
|
|
|
|
): string => {
|
|
|
|
const absolutePath = join(rootPath, relativePath);
|
|
|
|
const normalized = 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;
|
|
|
|
};
|