purge-uploads.js 856 B

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