2021-10-27 17:54:16 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { isString } from 'lodash';
|
|
|
|
import { join, normalize } from 'path';
|
|
|
|
import fse from 'fs-extra';
|
|
|
|
|
|
|
|
import { isPathInside } from './isPathInside';
|
|
|
|
|
|
|
|
const PATH = 'attachments.noindex';
|
|
|
|
const AVATAR_PATH = 'avatars.noindex';
|
2021-11-02 23:01:13 +00:00
|
|
|
const BADGES_PATH = 'badges.noindex';
|
2021-10-27 17:54:16 +00:00
|
|
|
const STICKER_PATH = 'stickers.noindex';
|
|
|
|
const TEMP_PATH = 'temp';
|
|
|
|
const DRAFT_PATH = 'drafts.noindex';
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const createPathGetter =
|
|
|
|
(subpath: string) =>
|
|
|
|
(userDataPath: string): string => {
|
|
|
|
if (!isString(userDataPath)) {
|
|
|
|
throw new TypeError("'userDataPath' must be a string");
|
|
|
|
}
|
|
|
|
return join(userDataPath, subpath);
|
|
|
|
};
|
2021-10-27 17:54:16 +00:00
|
|
|
|
|
|
|
export const getAvatarsPath = createPathGetter(AVATAR_PATH);
|
2021-11-02 23:01:13 +00:00
|
|
|
export const getBadgesPath = createPathGetter(BADGES_PATH);
|
2021-10-27 17:54:16 +00:00
|
|
|
export const getDraftPath = createPathGetter(DRAFT_PATH);
|
|
|
|
export const getPath = createPathGetter(PATH);
|
|
|
|
export const getStickersPath = createPathGetter(STICKER_PATH);
|
|
|
|
export const getTempPath = createPathGetter(TEMP_PATH);
|
|
|
|
|
|
|
|
export const createDeleter = (
|
|
|
|
root: string
|
|
|
|
): ((relativePath: string) => Promise<void>) => {
|
|
|
|
if (!isString(root)) {
|
|
|
|
throw new TypeError("'root' must be a path");
|
|
|
|
}
|
|
|
|
|
|
|
|
return async (relativePath: string): Promise<void> => {
|
|
|
|
if (!isString(relativePath)) {
|
|
|
|
throw new TypeError("'relativePath' must be a string");
|
|
|
|
}
|
|
|
|
|
|
|
|
const absolutePath = join(root, relativePath);
|
|
|
|
const normalized = normalize(absolutePath);
|
|
|
|
if (!isPathInside(normalized, root)) {
|
|
|
|
throw new Error('Invalid relative path');
|
|
|
|
}
|
|
|
|
await fse.remove(absolutePath);
|
|
|
|
};
|
|
|
|
};
|