purge-uploads.mjs 844 B

123456789101112131415161718192021222324252627
  1. import path from 'node:path'
  2. import fse from 'fs-extra'
  3. import { DateTime } from 'luxon'
  4. export async function task ({ payload }) {
  5. WIKI.logger.info('Purging orphaned upload files...')
  6. try {
  7. const uplTempPath = path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, 'uploads')
  8. await fse.ensureDir(uplTempPath)
  9. const ls = await fse.readdir(uplTempPath)
  10. const fifteenAgo = DateTime.now().minus({ minutes: 15 })
  11. for (const f of ls) {
  12. const stat = fse.stat(path.join(uplTempPath, f))
  13. if ((await stat).isFile && stat.ctime < fifteenAgo) {
  14. await fse.unlink(path.join(uplTempPath, f))
  15. }
  16. }
  17. WIKI.logger.info('Purging orphaned upload files: [ COMPLETED ]')
  18. } catch (err) {
  19. WIKI.logger.error('Purging orphaned upload files: [ FAILED ]')
  20. WIKI.logger.error(err.message)
  21. throw err
  22. }
  23. }