Skip to content

Instantly share code, notes, and snippets.

@odiak
Last active March 20, 2022 21:23
Show Gist options
  • Save odiak/4694f85783a057d812f80ea537e4dfb7 to your computer and use it in GitHub Desktop.
Save odiak/4694f85783a057d812f80ea537e4dfb7 to your computer and use it in GitHub Desktop.
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