Refactor fast-glob pattern

This commit is contained in:
Fedor Indutny 2025-02-12 16:03:31 -08:00 committed by GitHub
parent 5e73e1ae97
commit 9382c2fbd7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 31 deletions

View file

@ -88,54 +88,47 @@ export const createDeleter = (
};
};
export const getAllAttachments = async (
userDataPath: string
): Promise<ReadonlyArray<string>> => {
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<ReadonlyArray<string>> {
const pattern = prepareGlobPattern(dir);
const files = await fastGlob(pattern, { onlyFiles: true });
return map(files, file => relative(dir, file));
}
export const getAllAttachments = (
userDataPath: string
): Promise<ReadonlyArray<string>> => {
return getAllFiles(getPath(userDataPath));
};
export const getAllDownloads = async (
export const getAllDownloads = (
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));
return getAllFiles(getDownloadsPath(userDataPath));
};
const getAllBadgeImageFiles = async (
const getAllBadgeImageFiles = (
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));
return getAllFiles(getBadgesPath(userDataPath));
};
export const getAllStickers = async (
export const getAllStickers = (
userDataPath: string
): Promise<ReadonlyArray<string>> => {
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<ReadonlyArray<string>> => {
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<void> => {

View file

@ -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<string>) {
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