diff --git a/app/attachments.ts b/app/attachments.ts index 53f29c93cb..ef87fde32d 100644 --- a/app/attachments.ts +++ b/app/attachments.ts @@ -88,54 +88,47 @@ export const createDeleter = ( }; }; -export const getAllAttachments = async ( - userDataPath: string -): Promise> => { - const dir = getPath(userDataPath); - const pattern = normalizePath(join(dir, '**', '*')); +export function prepareGlobPattern(dir: string): string { + const prefix = normalizePath(dir).replace(/([$^*+?()[\]])/g, '\\$1'); + // fast-glob uses `/` for all platforms + return `${prefix}/**/*`; +} + +async function getAllFiles(dir: string): Promise> { + const pattern = prepareGlobPattern(dir); const files = await fastGlob(pattern, { onlyFiles: true }); return map(files, file => relative(dir, file)); +} + +export const getAllAttachments = ( + userDataPath: string +): Promise> => { + return getAllFiles(getPath(userDataPath)); }; -export const getAllDownloads = async ( +export const getAllDownloads = ( userDataPath: string ): Promise> => { - const dir = getDownloadsPath(userDataPath); - const pattern = normalizePath(join(dir, '**', '*')); - - const files = await fastGlob(pattern, { onlyFiles: true }); - return map(files, file => relative(dir, file)); + return getAllFiles(getDownloadsPath(userDataPath)); }; -const getAllBadgeImageFiles = async ( +const getAllBadgeImageFiles = ( userDataPath: string ): Promise> => { - const dir = getBadgesPath(userDataPath); - const pattern = normalizePath(join(dir, '**', '*')); - - const files = await fastGlob(pattern, { onlyFiles: true }); - return map(files, file => relative(dir, file)); + return getAllFiles(getBadgesPath(userDataPath)); }; -export const getAllStickers = async ( +export const getAllStickers = ( userDataPath: string ): Promise> => { - const dir = getStickersPath(userDataPath); - const pattern = normalizePath(join(dir, '**', '*')); - - const files = await fastGlob(pattern, { onlyFiles: true }); - return map(files, file => relative(dir, file)); + return getAllFiles(getStickersPath(userDataPath)); }; export const getAllDraftAttachments = async ( userDataPath: string ): Promise> => { - const dir = getDraftPath(userDataPath); - const pattern = normalizePath(join(dir, '**', '*')); - - const files = await fastGlob(pattern, { onlyFiles: true }); - return map(files, file => relative(dir, file)); + return getAllFiles(getDraftPath(userDataPath)); }; export const clearTempPath = (userDataPath: string): Promise => { diff --git a/app/main.ts b/app/main.ts index 6a14d59f3a..67f3d42ed2 100644 --- a/app/main.ts +++ b/app/main.ts @@ -8,7 +8,6 @@ import { chmod, realpath, writeFile } from 'fs-extra'; import { randomBytes } from 'crypto'; import { createParser } from 'dashdash'; -import normalizePath from 'normalize-path'; import fastGlob from 'fast-glob'; import PQueue from 'p-queue'; import { get, pick, isNumber, isBoolean, some, debounce, noop } from 'lodash'; @@ -2950,8 +2949,7 @@ async function ensureFilePermissions(onlyFiles?: Array) { const start = Date.now(); const userDataPath = await realpath(app.getPath('userData')); - // fast-glob uses `/` for all platforms - const userDataGlob = normalizePath(join(userDataPath, '**', '*')); + const userDataGlob = attachments.prepareGlobPattern(userDataPath); // Determine files to touch const files = onlyFiles