Skip to content

Commit b528a21

Browse files
committed
feat: file temp auto clean
1 parent cb744a0 commit b528a21

File tree

3 files changed

+76
-25
lines changed

3 files changed

+76
-25
lines changed

electron/mapi/file/index.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ const hubRootDefault = async () => {
506506
return path.join(root(), "hub");
507507
};
508508

509-
const hubRoot = async () => {
509+
const hubRoot = async (): Promise<string> => {
510510
const hubDirDefault = await hubRootDefault();
511511
let hubDir = await ConfigIndex.get("hubRoot", "");
512512
if (!hubDir) {
@@ -778,7 +778,7 @@ const tempRoot = async () => {
778778
return tempDir;
779779
};
780780

781-
const autoCleanTemp = async (keepDays: number = 1) => {
781+
const autoCleanTemp = async (keepDays: number = 7) => {
782782
const root = await tempRoot();
783783
if (!fs.existsSync(root)) {
784784
return;
@@ -793,8 +793,11 @@ const autoCleanTemp = async (keepDays: number = 1) => {
793793
}
794794
const lastModified = new Date(stat.mtimeMs);
795795
const diffDays = Math.floor((now.getTime() - lastModified.getTime()) / (1000 * 60 * 60 * 24));
796-
if (diffDays > keepDays) {
796+
if (diffDays >= keepDays) {
797797
fs.unlinkSync(filePath);
798+
Log.info('AutoCleanTemp.Clean', filePath);
799+
} else {
800+
// console.log('AutoCleanTemp.Skip', filePath, diffDays);
798801
}
799802
}
800803
};
@@ -810,10 +813,6 @@ const temp = async (ext: string = "tmp", prefix: string = "file", suffix: string
810813
};
811814

812815
const tempDir = async (prefix: string = "dir") => {
813-
if (Math.random() < 0.1) {
814-
// 10% chance to clean temp directory
815-
autoCleanTemp(1).then();
816-
}
817816
const root = await tempRoot();
818817
const p = [prefix, TimeUtil.timestampInMs(), StrUtil.randomString(32)].join("_");
819818
const dir = path.join(root, p);
@@ -1042,6 +1041,28 @@ const ext = (path: string) => {
10421041
return nodePath.extname(path).replace(/^\./, "");
10431042
};
10441043

1044+
const stat = async (path: string, option?: { isDataPath?: boolean }): Promise<{
1045+
size: number;
1046+
isDirectory: boolean;
1047+
lastModified: number;
1048+
}> => {
1049+
option = Object.assign({
1050+
isDataPath: false,
1051+
},
1052+
option
1053+
);
1054+
let fp = path;
1055+
if (option.isDataPath) {
1056+
fp = await fullPath(path);
1057+
}
1058+
const stat = fs.statSync(fp);
1059+
return {
1060+
size: stat.size,
1061+
isDirectory: stat.isDirectory(),
1062+
lastModified: stat.mtimeMs,
1063+
};
1064+
}
1065+
10451066
const textToName = (text: string, ext: string = "", maxLimit: number = 100) => {
10461067
if (text) {
10471068
// 转换为合法的文件名
@@ -1178,9 +1199,11 @@ export const FileIndex = {
11781199
appendText,
11791200
download,
11801201
ext,
1202+
stat,
11811203
textToName,
11821204
pathToName,
11831205
hubRootDefault,
1206+
hubRoot,
11841207
hubSave,
11851208
hubSaveContent,
11861209
hubDelete,
@@ -1191,6 +1214,7 @@ export const FileIndex = {
11911214
cacheSet,
11921215
cacheGetPath,
11931216
cacheGet,
1217+
autoCleanTemp,
11941218
};
11951219

11961220
export default FileIndex;

electron/mapi/file/main.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ ipcMain.handle("file:openSave", async (_, options): Promise<string | null> => {
5959
return res.filePath || null;
6060
});
6161

62+
const autoCleanTemp = async () => {
63+
fileIndex.autoCleanTemp(1).finally(() => {
64+
setTimeout(() => {
65+
autoCleanTemp();
66+
}, 10 * 60 * 1000);
67+
});
68+
}
69+
70+
setTimeout(() => {
71+
autoCleanTemp().then();
72+
}, 5000);
73+
74+
6275
export default {
6376
...fileIndex,
6477
};

src/declarations/type.d.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ type DefsMapi = {
142142
root: () => string;
143143
info: (msg: string, data: any = null) => Promise<void>;
144144
error: (msg: string, data: any = null) => Promise<void>;
145-
collect: (option?: {startTime?: string; endTime?: string; limit?: number}) => Promise<string>;
145+
collect: (option?: { startTime?: string; endTime?: string; limit?: number }) => Promise<string>;
146146
};
147147
storage: {
148148
all: () => Promise<any>;
@@ -161,19 +161,25 @@ type DefsMapi = {
161161
};
162162
file: {
163163
fullPath: (path: string) => Promise<string>;
164-
exists: (path: string, option?: {isDataPath?: boolean}) => Promise<boolean>;
165-
isDirectory: (path: string, option?: {isDataPath?: boolean}) => Promise<boolean>;
166-
mkdir: (path: string, option?: {isDataPath?: boolean}) => Promise<void>;
167-
list: (path: string, option?: {isDataPath?: boolean}) => Promise<any[]>;
168-
listAll: (path: string, option?: {isDataPath?: boolean}) => Promise<any[]>;
169-
write: (path: string, data: any, option?: {isDataPath?: boolean}) => Promise<void>;
170-
writeBuffer: (path: string, data: any, option?: {isDataPath?: boolean}) => Promise<void>;
171-
writeStream: (path: string, stream: any, option?: {isDataPath?: boolean}) => Promise<void>;
172-
read: (path: string, option?: {isDataPath?: boolean}) => Promise<any>;
173-
readBuffer: (path: string, option?: {isDataPath?: boolean}) => Promise<any>;
174-
readStream: (path: string, option?: {isDataPath?: boolean}) => Promise<ReadableStream | null>;
175-
deletes: (path: string, option?: {isDataPath?: boolean}) => Promise<void>;
176-
clean: (paths: string[], option?: {isDataPath?: boolean}) => Promise<void>;
164+
exists: (path: string, option?: { isDataPath?: boolean }) => Promise<boolean>;
165+
isDirectory: (path: string, option?: { isDataPath?: boolean }) => Promise<boolean>;
166+
mkdir: (path: string, option?: { isDataPath?: boolean }) => Promise<void>;
167+
list: (path: string, option?: { isDataPath?: boolean }) => Promise<{
168+
name: string,
169+
pathname: string,
170+
isDirectory: boolean,
171+
size: number,
172+
lastModified: number,
173+
}[]>;
174+
listAll: (path: string, option?: { isDataPath?: boolean }) => Promise<any[]>;
175+
write: (path: string, data: any, option?: { isDataPath?: boolean }) => Promise<void>;
176+
writeBuffer: (path: string, data: any, option?: { isDataPath?: boolean }) => Promise<void>;
177+
writeStream: (path: string, stream: any, option?: { isDataPath?: boolean }) => Promise<void>;
178+
read: (path: string, option?: { isDataPath?: boolean }) => Promise<any>;
179+
readBuffer: (path: string, option?: { isDataPath?: boolean }) => Promise<any>;
180+
readStream: (path: string, option?: { isDataPath?: boolean }) => Promise<ReadableStream | null>;
181+
deletes: (path: string, option?: { isDataPath?: boolean }) => Promise<void>;
182+
clean: (paths: string[], option?: { isDataPath?: boolean }) => Promise<void>;
177183
rename: (
178184
pathOld: string,
179185
pathNew: string,
@@ -182,7 +188,7 @@ type DefsMapi = {
182188
overwrite?: boolean;
183189
}
184190
) => Promise<void>;
185-
copy: (pathOld: string, pathNew: string, option?: {isDataPath?: boolean}) => Promise<void>;
191+
copy: (pathOld: string, pathNew: string, option?: { isDataPath?: boolean }) => Promise<void>;
186192
temp: (ext: string = "tmp", prefix: string = "file", suffix: string = "") => Promise<string>;
187193
tempDir: (prefix: string = "dir") => Promise<string>;
188194
watchText: (
@@ -195,7 +201,7 @@ type DefsMapi = {
195201
) => Promise<{
196202
stop: Function;
197203
}>;
198-
appendText: (path: string, data: any, option?: {isDataPath?: boolean}) => Promise<void>;
204+
appendText: (path: string, data: any, option?: { isDataPath?: boolean }) => Promise<void>;
199205
download: (
200206
url: string,
201207
path?: string | null,
@@ -215,9 +221,15 @@ type DefsMapi = {
215221
openDirectory: (options: {} = {}) => Promise<string | null>;
216222
openSave: (options: {} = {}) => Promise<string | null>;
217223
ext: (path: string) => Promise<string>;
224+
stat: (path: string, option?: { isDataPath?: boolean }) => Promise<{
225+
size: number;
226+
isDirectory: boolean;
227+
lastModified: number;
228+
}>;
218229
textToName: (text: string, ext: string = "", maxLimit: number = 100) => string;
219230
pathToName: (path: string, includeExt: boolean = true, maxLimit: number = 100) => string;
220231
hubRootDefault: () => Promise<string>;
232+
hubRoot: () => Promise<string>;
221233
hubSave: (
222234
file: string,
223235
option?: {
@@ -327,8 +339,8 @@ type DefsMapi = {
327339
};
328340
misc: {
329341
getZipFileContent: (path: string, pathInZip: string) => Promise<string>;
330-
unzip: (zipPath: string, dest: string, option?: {process: Function}) => Promise<void>;
331-
zip: (zipPath: string, sourceDir: string, option?: {end?: (archive: any) => void}) => Promise<void>;
342+
unzip: (zipPath: string, dest: string, option?: { process: Function }) => Promise<void>;
343+
zip: (zipPath: string, sourceDir: string, option?: { end?: (archive: any) => void; }) => Promise<void>;
332344
request: (option: {
333345
url: string;
334346
method?: "GET" | "POST";
@@ -378,3 +390,5 @@ declare global {
378390
}
379391

380392
export {};
393+
394+

0 commit comments

Comments
 (0)