forked from r.muellner/velum-raycast
fix: skip unreadable Office files during project review
This commit is contained in:
@@ -12,6 +12,7 @@ export type { ProjectWhitelistEntry } from "./project-whitelist";
|
||||
|
||||
export type ProjectRule = { type: "BEGRIFF" | "PERSON" | "ORG" | "ORT" | "FIRMA" | "KUNDE"; value: string };
|
||||
export type ProjectFile = { relPath: string; absPath: string; kind: "text" | "document"; size: number };
|
||||
export type SkippedProjectFile = { path: string; reason: string };
|
||||
export type RunFile = { source: string; output: string; kind: ProjectFile["kind"] };
|
||||
export type ProjectRun = {
|
||||
version: 2;
|
||||
@@ -61,6 +62,15 @@ async function isBinary(path: string): Promise<boolean> {
|
||||
} finally { await file.close(); }
|
||||
}
|
||||
|
||||
async function isOfficeArchive(path: string): Promise<boolean> {
|
||||
const file = await fs.open(path, "r");
|
||||
try {
|
||||
const header = Buffer.alloc(4);
|
||||
const { bytesRead } = await file.read(header, 0, header.length, 0);
|
||||
return bytesRead === 4 && header.equals(Buffer.from([0x50, 0x4b, 0x03, 0x04]));
|
||||
} finally { await file.close(); }
|
||||
}
|
||||
|
||||
export function projectDataRoot(projectPath: string, support = environment.supportPath): string {
|
||||
return join(support, "velum-projects", projectIdFor(projectPath));
|
||||
}
|
||||
@@ -176,32 +186,41 @@ export function forceMasks(rules: ProjectRule[]): Record<string, string[]> {
|
||||
}
|
||||
|
||||
export async function scanProjectFiles(root: string, maxSize = 20 * 1024 * 1024): Promise<{
|
||||
files: ProjectFile[]; skipped: string[];
|
||||
files: ProjectFile[]; skipped: SkippedProjectFile[];
|
||||
}> {
|
||||
const files: ProjectFile[] = [];
|
||||
const skipped: string[] = [];
|
||||
const skipped: SkippedProjectFile[] = [];
|
||||
const skip = (path: string, reason: string) => skipped.push({ path, reason });
|
||||
async function walk(directory: string): Promise<void> {
|
||||
for (const entry of await fs.readdir(directory, { withFileTypes: true })) {
|
||||
const path = join(directory, entry.name);
|
||||
const relPath = relative(root, path).split(sep).join("/");
|
||||
if (entry.isSymbolicLink()) { skipped.push(relPath); continue; }
|
||||
if (entry.isSymbolicLink()) { skip(relPath, "Symbolischer Link"); continue; }
|
||||
if (entry.isDirectory()) {
|
||||
if (EXCLUDES.has(entry.name)) { skipped.push(relPath); continue; }
|
||||
if (EXCLUDES.has(entry.name)) { skip(relPath, "Ausgeschlossener Ordner"); continue; }
|
||||
await walk(path);
|
||||
continue;
|
||||
}
|
||||
if (!entry.isFile()) continue;
|
||||
if (entry.name.startsWith("~$") || entry.name.startsWith("._")) {
|
||||
skip(relPath, "Temporäre Office-/macOS-Datei");
|
||||
continue;
|
||||
}
|
||||
if (entry.name === ".env" || entry.name.startsWith(".env.") || SECRET_EXTENSIONS.has(extname(entry.name).toLowerCase())) {
|
||||
skipped.push(relPath);
|
||||
skip(relPath, "Vertraulicher Dateityp");
|
||||
continue;
|
||||
}
|
||||
const stat = await fs.stat(path);
|
||||
if (stat.size > maxSize) { skipped.push(relPath); continue; }
|
||||
if (stat.size > maxSize) { skip(relPath, "Datei zu groß"); continue; }
|
||||
const kind = DOCUMENTS.has(extname(path).toLowerCase()) ? "document" : "text";
|
||||
if (kind === "document" && extname(path).toLowerCase() !== ".pdf" && !(await isOfficeArchive(path))) {
|
||||
skip(relPath, "Kein lesbares Office-ZIP-Archiv (beschädigt, verschlüsselt oder falsche Endung)");
|
||||
continue;
|
||||
}
|
||||
if (kind === "text") {
|
||||
if (await isBinary(path)) { skipped.push(relPath); continue; }
|
||||
if (await isBinary(path)) { skip(relPath, "Binärdatei"); continue; }
|
||||
try { new TextDecoder("utf-8", { fatal: true }).decode(await fs.readFile(path)); }
|
||||
catch { skipped.push(relPath); continue; }
|
||||
catch { skip(relPath, "Keine gültige UTF-8-Textdatei"); continue; }
|
||||
}
|
||||
files.push({ relPath, absPath: path, kind, size: stat.size });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user