2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2018 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2024-07-11 19:44:09 +00:00
|
|
|
import { PassThrough } from 'node:stream';
|
2024-08-19 20:05:35 +00:00
|
|
|
import { stat } from 'node:fs/promises';
|
2022-10-24 20:46:36 +00:00
|
|
|
import { join, relative, normalize } from 'path';
|
2024-08-19 20:05:35 +00:00
|
|
|
import pMap from 'p-map';
|
2021-06-18 17:04:27 +00:00
|
|
|
import fastGlob from 'fast-glob';
|
|
|
|
import fse from 'fs-extra';
|
2022-10-24 20:46:36 +00:00
|
|
|
import { map, isString } from 'lodash';
|
2021-06-18 17:04:27 +00:00
|
|
|
import normalizePath from 'normalize-path';
|
2022-10-24 20:46:36 +00:00
|
|
|
import { isPathInside } from '../ts/util/isPathInside';
|
2024-08-19 20:05:35 +00:00
|
|
|
import { DAY } from '../ts/util/durations';
|
|
|
|
import { isOlderThan } from '../ts/util/timestamp';
|
|
|
|
import { isNotNil } from '../ts/util/isNotNil';
|
2024-07-11 19:44:09 +00:00
|
|
|
import {
|
|
|
|
generateKeys,
|
|
|
|
decryptAttachmentV2ToSink,
|
|
|
|
encryptAttachmentV2ToDisk,
|
|
|
|
} from '../ts/AttachmentCrypto';
|
|
|
|
import type { LocalAttachmentV2Type } from '../ts/types/Attachment';
|
2024-08-19 20:05:35 +00:00
|
|
|
import * as Errors from '../ts/types/errors';
|
2022-10-24 20:46:36 +00:00
|
|
|
|
|
|
|
const PATH = 'attachments.noindex';
|
|
|
|
const AVATAR_PATH = 'avatars.noindex';
|
|
|
|
const BADGES_PATH = 'badges.noindex';
|
|
|
|
const STICKER_PATH = 'stickers.noindex';
|
|
|
|
const TEMP_PATH = 'temp';
|
|
|
|
const UPDATE_CACHE_PATH = 'update-cache';
|
|
|
|
const DRAFT_PATH = 'drafts.noindex';
|
2024-08-19 20:05:35 +00:00
|
|
|
const DOWNLOADS_PATH = 'downloads.noindex';
|
2022-10-24 20:46:36 +00:00
|
|
|
|
|
|
|
const CACHED_PATHS = new Map<string, string>();
|
|
|
|
|
2024-08-19 20:05:35 +00:00
|
|
|
const FS_CONCURRENCY = 100;
|
|
|
|
|
2022-10-24 20:46:36 +00:00
|
|
|
const createPathGetter =
|
|
|
|
(subpath: string) =>
|
|
|
|
(userDataPath: string): string => {
|
|
|
|
if (!isString(userDataPath)) {
|
|
|
|
throw new TypeError("'userDataPath' must be a string");
|
|
|
|
}
|
|
|
|
|
|
|
|
const naivePath = join(userDataPath, subpath);
|
|
|
|
|
|
|
|
const cached = CACHED_PATHS.get(naivePath);
|
|
|
|
if (cached) {
|
|
|
|
return cached;
|
|
|
|
}
|
2021-06-18 17:04:27 +00:00
|
|
|
|
2022-10-24 20:46:36 +00:00
|
|
|
let result = naivePath;
|
|
|
|
if (fse.pathExistsSync(naivePath)) {
|
|
|
|
result = fse.realpathSync(naivePath);
|
|
|
|
}
|
2021-06-18 17:04:27 +00:00
|
|
|
|
2022-10-24 20:46:36 +00:00
|
|
|
CACHED_PATHS.set(naivePath, result);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getAvatarsPath = createPathGetter(AVATAR_PATH);
|
|
|
|
export const getBadgesPath = createPathGetter(BADGES_PATH);
|
|
|
|
export const getDraftPath = createPathGetter(DRAFT_PATH);
|
2024-08-19 20:05:35 +00:00
|
|
|
export const getDownloadsPath = createPathGetter(DOWNLOADS_PATH);
|
2022-10-24 20:46:36 +00:00
|
|
|
export const getPath = createPathGetter(PATH);
|
|
|
|
export const getStickersPath = createPathGetter(STICKER_PATH);
|
|
|
|
export const getTempPath = createPathGetter(TEMP_PATH);
|
|
|
|
export const getUpdateCachePath = createPathGetter(UPDATE_CACHE_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);
|
|
|
|
};
|
|
|
|
};
|
2020-08-27 18:08:37 +00:00
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2024-08-19 20:05:35 +00:00
|
|
|
export const getAllDownloads = async (
|
|
|
|
userDataPath: string
|
|
|
|
): Promise<ReadonlyArray<string>> => {
|
|
|
|
const dir = getDownloadsPath(userDataPath);
|
|
|
|
const pattern = normalizePath(join(dir, '**', '*'));
|
|
|
|
|
|
|
|
const files = await fastGlob(pattern, { onlyFiles: true });
|
|
|
|
return map(files, file => relative(dir, file));
|
|
|
|
};
|
|
|
|
|
2021-11-02 23:01:13 +00:00
|
|
|
const getAllBadgeImageFiles = async (
|
|
|
|
userDataPath: string
|
|
|
|
): Promise<ReadonlyArray<string>> => {
|
|
|
|
const dir = getBadgesPath(userDataPath);
|
|
|
|
const pattern = normalizePath(join(dir, '**', '*'));
|
|
|
|
|
|
|
|
const files = await fastGlob(pattern, { onlyFiles: true });
|
|
|
|
return map(files, file => relative(dir, file));
|
|
|
|
};
|
|
|
|
|
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 clearTempPath = (userDataPath: string): Promise<void> => {
|
|
|
|
const tempPath = getTempPath(userDataPath);
|
2019-05-24 01:27:42 +00:00
|
|
|
return fse.emptyDir(tempPath);
|
|
|
|
};
|
|
|
|
|
2024-08-19 20:05:35 +00:00
|
|
|
export const deleteStaleDownloads = async (
|
|
|
|
userDataPath: string
|
|
|
|
): Promise<void> => {
|
|
|
|
const dir = getDownloadsPath(userDataPath);
|
|
|
|
const files = await getAllDownloads(userDataPath);
|
|
|
|
|
|
|
|
const result = await pMap(
|
|
|
|
files,
|
|
|
|
async file => {
|
|
|
|
try {
|
|
|
|
const { birthtimeMs } = await stat(join(dir, file));
|
|
|
|
if (isOlderThan(birthtimeMs, DAY)) {
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// No longer exists
|
|
|
|
if (error.code === 'ENOENT') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.error(
|
|
|
|
'deleteStaleDownloads: failed to get file stats',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
},
|
|
|
|
{ concurrency: FS_CONCURRENCY }
|
|
|
|
);
|
|
|
|
|
|
|
|
const stale = result.filter(isNotNil);
|
|
|
|
if (stale.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log(`deleteStaleDownloads: found ${stale.length}`);
|
|
|
|
await deleteAllDownloads({ userDataPath, downloads: stale });
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2024-08-19 20:05:35 +00:00
|
|
|
await pMap(attachments, deleteFromDisk, { concurrency: FS_CONCURRENCY });
|
2018-08-06 23:18:58 +00:00
|
|
|
|
|
|
|
console.log(`deleteAll: deleted ${attachments.length} files`);
|
|
|
|
};
|
|
|
|
|
2024-08-19 20:05:35 +00:00
|
|
|
export const deleteAllDownloads = async ({
|
|
|
|
userDataPath,
|
|
|
|
downloads,
|
|
|
|
}: {
|
|
|
|
userDataPath: string;
|
|
|
|
downloads: ReadonlyArray<string>;
|
|
|
|
}): Promise<void> => {
|
|
|
|
const deleteFromDisk = createDeleter(getDownloadsPath(userDataPath));
|
|
|
|
|
|
|
|
await pMap(downloads, deleteFromDisk, { concurrency: FS_CONCURRENCY });
|
|
|
|
|
|
|
|
console.log(`deleteAllDownloads: deleted ${downloads.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
|
|
|
|
2024-08-19 20:05:35 +00:00
|
|
|
await pMap(stickers, deleteFromDisk, { concurrency: FS_CONCURRENCY });
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
console.log(`deleteAllStickers: deleted ${stickers.length} files`);
|
|
|
|
};
|
|
|
|
|
2021-11-02 23:01:13 +00:00
|
|
|
export const deleteAllBadges = async ({
|
|
|
|
userDataPath,
|
|
|
|
pathsToKeep,
|
|
|
|
}: {
|
|
|
|
userDataPath: string;
|
|
|
|
pathsToKeep: Set<string>;
|
|
|
|
}): Promise<void> => {
|
|
|
|
const deleteFromDisk = createDeleter(getBadgesPath(userDataPath));
|
|
|
|
|
|
|
|
let filesDeleted = 0;
|
|
|
|
for (const file of await getAllBadgeImageFiles(userDataPath)) {
|
|
|
|
if (!pathsToKeep.has(file)) {
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await deleteFromDisk(file);
|
|
|
|
filesDeleted += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`deleteAllBadges: deleted ${filesDeleted} 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
|
|
|
|
2024-08-19 20:05:35 +00:00
|
|
|
await pMap(attachments, deleteFromDisk, { concurrency: FS_CONCURRENCY });
|
2019-08-07 00:40:25 +00:00
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
console.log(`deleteAllDraftAttachments: deleted ${attachments.length} files`);
|
2019-08-07 00:40:25 +00:00
|
|
|
};
|
2024-07-11 19:44:09 +00:00
|
|
|
|
|
|
|
export const readAndDecryptDataFromDisk = async ({
|
|
|
|
absolutePath,
|
|
|
|
keysBase64,
|
|
|
|
size,
|
|
|
|
}: {
|
|
|
|
absolutePath: string;
|
|
|
|
keysBase64: string;
|
|
|
|
size: number;
|
|
|
|
}): Promise<Uint8Array> => {
|
|
|
|
const sink = new PassThrough();
|
|
|
|
|
|
|
|
const chunks = new Array<Buffer>();
|
|
|
|
|
|
|
|
sink.on('data', chunk => chunks.push(chunk));
|
|
|
|
sink.resume();
|
|
|
|
|
|
|
|
await decryptAttachmentV2ToSink(
|
|
|
|
{
|
|
|
|
ciphertextPath: absolutePath,
|
|
|
|
idForLogging: 'attachments/readAndDecryptDataFromDisk',
|
|
|
|
keysBase64,
|
|
|
|
size,
|
2024-07-16 20:39:56 +00:00
|
|
|
type: 'local',
|
2024-07-11 19:44:09 +00:00
|
|
|
},
|
|
|
|
sink
|
|
|
|
);
|
|
|
|
|
|
|
|
return Buffer.concat(chunks);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const writeNewAttachmentData = async ({
|
|
|
|
data,
|
|
|
|
getAbsoluteAttachmentPath,
|
|
|
|
}: {
|
|
|
|
data: Uint8Array;
|
|
|
|
getAbsoluteAttachmentPath: (relativePath: string) => string;
|
|
|
|
}): Promise<LocalAttachmentV2Type> => {
|
|
|
|
const keys = generateKeys();
|
|
|
|
|
|
|
|
const { plaintextHash, path } = await encryptAttachmentV2ToDisk({
|
|
|
|
plaintext: { data },
|
|
|
|
getAbsoluteAttachmentPath,
|
|
|
|
keys,
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
version: 2,
|
|
|
|
plaintextHash,
|
|
|
|
size: data.byteLength,
|
|
|
|
path,
|
|
|
|
localKey: Buffer.from(keys).toString('base64'),
|
|
|
|
};
|
|
|
|
};
|