Repository navigation
Expand file tree
/
Copy pathnative.ts
More file actions
60 lines (51 loc) · 1.85 KB
/
Copy pathnative.ts
File metadata and controls
60 lines (51 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* ActivityTracker — native file storage
* Stores the logs per user as JSON files in Vencord's data folder instead of
* cramming them into settings.json. Nothing leaves your PC.
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { DATA_DIR } from "@main/utils/constants";
import { IpcMainInvokeEvent } from "electron";
import { mkdir, readdir, readFile, unlink, writeFile } from "fs/promises";
import path from "path";
let logsDir: string;
async function getLogsDir() {
if (!logsDir) {
logsDir = path.join(DATA_DIR, "ActivityTrackerData");
await mkdir(logsDir, { recursive: true });
}
return logsDir;
}
function fileFor(dir: string, userId: string) {
// only allow digits as file name so nobody can navigate out of the folder via the ID
const safe = String(userId).replace(/[^0-9]/g, "");
return path.join(dir, `${safe}.json`);
}
export async function writeUserLogs(_: IpcMainInvokeEvent, userId: string, json: string) {
const dir = await getLogsDir();
await writeFile(fileFor(dir, userId), json, "utf-8");
}
export async function readUserLogs(_: IpcMainInvokeEvent, userId: string): Promise<string> {
const dir = await getLogsDir();
try {
return await readFile(fileFor(dir, userId), "utf-8");
} catch {
return "";
}
}
export async function deleteUserLogs(_: IpcMainInvokeEvent, userId: string) {
const dir = await getLogsDir();
try {
await unlink(fileFor(dir, userId));
} catch { }
}
export async function deleteAllLogs(_: IpcMainInvokeEvent) {
const dir = await getLogsDir();
try {
const files = await readdir(dir);
await Promise.all(files.filter(f => f.endsWith(".json")).map(f => unlink(path.join(dir, f)).catch(() => { })));
} catch { }
}
export async function getLogsPath(_: IpcMainInvokeEvent): Promise<string> {
return getLogsDir();
}