Skip to content

Commit

Permalink
feat: clean cached images that are no longer used
Browse files Browse the repository at this point in the history
  • Loading branch information
ElMassimo committed Jan 12, 2022
1 parent 649d4f4 commit d0879b3
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export function createImageApi (config: Config) {
},
async waitForImages () {
debug.total('%i image(s)', generatedImages.length)
return await Promise.all(generatedImages)
const assets = await Promise.all(generatedImages)
cleanCacheDir(assets.map(asset => asset.name!))
return assets
},
async resolveImage (filename: string, params: Record<string, any>): Promise<ImageResult> {
const presetName = params[config.urlParam]
Expand Down Expand Up @@ -113,4 +115,13 @@ export function createImageApi (config: Config) {

return VIRTUAL_ID + id
}

async function cleanCacheDir (newFiles: string[]) {
const usedFiles = new Set(newFiles)
const cachedFiles = await fs.readdir(config.cacheDir)
cachedFiles.forEach(file => {
if (!usedFiles.has(file))
fs.rm(resolve(config.cacheDir, file), { force: true })
})
}
}

0 comments on commit d0879b3

Please sign in to comment.