Deduplicate incoming attachments on disk

This commit is contained in:
trevor-signal
2026-02-05 14:48:31 -05:00
committed by GitHub
parent 85cc412b40
commit 834f0c7775
51 changed files with 1727 additions and 560 deletions
+11 -3
View File
@@ -48,7 +48,7 @@ import { sleep } from '../ts/util/sleep.std.js';
import { toWebStream } from '../ts/util/toWebStream.node.js';
import { createLogger } from '../ts/logging/log.std.js';
import {
deleteAll as deleteAllAttachments,
deleteAllAttachments,
deleteAllBadges,
deleteAllDownloads,
deleteAllDraftAttachments,
@@ -62,7 +62,7 @@ import {
getAvatarsPath,
getDownloadsPath,
getDraftPath,
getPath,
getAttachmentsPath,
getStickersPath,
getTempPath,
} from './attachments.node.js';
@@ -450,6 +450,14 @@ function deleteOrphanedAttachments({
await sql.sqlRead('finishGetKnownMessageAttachments', cursor);
}
}
const protectedAttachments = await sql.sqlRead(
'getAllProtectedAttachmentPaths'
);
for (const protectedAttachment of protectedAttachments) {
orphanedAttachments.delete(protectedAttachment);
}
log.info(
`cleanupOrphanedAttachments: ${totalAttachmentsFound} attachments and \
${totalDownloadsFound} downloads found on disk`
@@ -514,7 +522,7 @@ export function initialize({
}
initialized = true;
attachmentsDir = getPath(configDir);
attachmentsDir = getAttachmentsPath(configDir);
stickersDir = getStickersPath(configDir);
tempDir = getTempPath(configDir);
draftDir = getDraftPath(configDir);
+5 -5
View File
@@ -26,7 +26,7 @@ const { map, isString } = lodash;
const log = createLogger('attachments');
const PATH = 'attachments.noindex';
const ATTACHMENTS_PATH = 'attachments.noindex';
const AVATAR_PATH = 'avatars.noindex';
const BADGES_PATH = 'badges.noindex';
const STICKER_PATH = 'stickers.noindex';
@@ -69,7 +69,7 @@ export const getBadgesPath = createPathGetter(BADGES_PATH);
export const getDraftPath = createPathGetter(DRAFT_PATH);
export const getDownloadsPath = createPathGetter(DOWNLOADS_PATH);
export const getMegaphonesPath = createPathGetter(MEGAPHONES_PATH);
export const getPath = createPathGetter(PATH);
export const getAttachmentsPath = createPathGetter(ATTACHMENTS_PATH);
export const getStickersPath = createPathGetter(STICKER_PATH);
export const getTempPath = createPathGetter(TEMP_PATH);
export const getUpdateCachePath = createPathGetter(UPDATE_CACHE_PATH);
@@ -111,7 +111,7 @@ async function getAllFiles(dir: string): Promise<ReadonlyArray<string>> {
export const getAllAttachments = (
userDataPath: string
): Promise<ReadonlyArray<string>> => {
return getAllFiles(getPath(userDataPath));
return getAllFiles(getAttachmentsPath(userDataPath));
};
export const getAllDownloads = (
@@ -186,14 +186,14 @@ export const deleteStaleDownloads = async (
await deleteAllDownloads({ userDataPath, downloads: stale });
};
export const deleteAll = async ({
export const deleteAllAttachments = async ({
userDataPath,
attachments,
}: {
userDataPath: string;
attachments: ReadonlyArray<string>;
}): Promise<void> => {
const deleteFromDisk = createDeleter(getPath(userDataPath));
const deleteFromDisk = createDeleter(getAttachmentsPath(userDataPath));
await pMap(attachments, deleteFromDisk, { concurrency: FS_CONCURRENCY });
+2 -2
View File
@@ -10,7 +10,7 @@ import {
getBadgesPath,
getDraftPath,
getDownloadsPath,
getPath,
getAttachmentsPath,
getStickersPath,
getTempPath,
getUpdateCachePath,
@@ -66,7 +66,7 @@ function _createFileHandler({
getBadgesPath(userDataPath),
getDraftPath(userDataPath),
getDownloadsPath(userDataPath),
getPath(userDataPath),
getAttachmentsPath(userDataPath),
getStickersPath(userDataPath),
getTempPath(userDataPath),
getUpdateCachePath(userDataPath),