Skip to content

Instantly share code, notes, and snippets.

@odiak
Last active Mar 20, 2022
Embed
What would you like to do?
const fsp = require('fs').promises
async function purge(dir, pictureId) {
const entries = await fsp.readdir(dir, { withFileTypes: true })
let n = 0
await Promise.all(entries.map(async (e) => {
const path = `${dir}/${e.name}`
if (e.isDirectory()) {
n += await purge(path, pictureId)
} else if (e.isFile()) {
const buf = await fsp.readFile(path)
const idOnCache = buf.slice(364, 396).toString()
if (idOnCache === pictureId) {
const copy = Buffer.from(buf)
copy.writeIntLE(Math.floor(new Date() / 1000) - 1, 8, 6)
copy.writeIntLE(0, 32, 6)
await fsp.writeFile(path, copy)
n++
}
}
}))
return n
}
async function handler(r) {
const pictureId = r.uri.slice(r.uri.lastIndexOf('/') + 1)
const n = await purge('/var/cache/nginx/kakeru', pictureId)
r.return(200, `${n}`)
}
export default {handler}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment