mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 14:21:38 +00:00
fix(media): clean expired files in subdirectories
cleanOldMedia() only scanned the top-level media directory, but saveMediaBuffer() writes to subdirs (inbound/, outbound/, browser/). Files in those subdirs were never cleaned up. Now recurses one level into subdirectories, deleting expired files while preserving the subdirectory folders themselves.
This commit is contained in:
committed by
Peter Steinberger
parent
c89eb351ea
commit
441401221d
@@ -88,6 +88,22 @@ export async function cleanOldMedia(ttlMs = DEFAULT_TTL_MS) {
|
||||
const mediaDir = await ensureMediaDir();
|
||||
const entries = await fs.readdir(mediaDir).catch(() => []);
|
||||
const now = Date.now();
|
||||
const removeExpiredFilesInDir = async (dir: string) => {
|
||||
const dirEntries = await fs.readdir(dir).catch(() => []);
|
||||
await Promise.all(
|
||||
dirEntries.map(async (entry) => {
|
||||
const full = path.join(dir, entry);
|
||||
const stat = await fs.stat(full).catch(() => null);
|
||||
if (!stat || !stat.isFile()) {
|
||||
return;
|
||||
}
|
||||
if (now - stat.mtimeMs > ttlMs) {
|
||||
await fs.rm(full).catch(() => {});
|
||||
}
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
await Promise.all(
|
||||
entries.map(async (file) => {
|
||||
const full = path.join(mediaDir, file);
|
||||
@@ -95,7 +111,11 @@ export async function cleanOldMedia(ttlMs = DEFAULT_TTL_MS) {
|
||||
if (!stat) {
|
||||
return;
|
||||
}
|
||||
if (now - stat.mtimeMs > ttlMs) {
|
||||
if (stat.isDirectory()) {
|
||||
await removeExpiredFilesInDir(full);
|
||||
return;
|
||||
}
|
||||
if (stat.isFile() && now - stat.mtimeMs > ttlMs) {
|
||||
await fs.rm(full).catch(() => {});
|
||||
}
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user