You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1.0 KiB

  1. /* global WIKI */
  2. const Promise = require('bluebird')
  3. const fs = require('fs-extra')
  4. const moment = require('moment')
  5. const path = require('path')
  6. module.exports = async () => {
  7. WIKI.logger.info('Purging orphaned upload files...')
  8. try {
  9. const uplTempPath = path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, 'uploads')
  10. await fs.ensureDir(uplTempPath)
  11. const ls = await fs.readdir(uplTempPath)
  12. const fifteenAgo = moment().subtract(15, 'minutes')
  13. await Promise.map(ls, (f) => {
  14. return fs.stat(path.join(uplTempPath, f)).then((s) => { return { filename: f, stat: s } })
  15. }).filter((s) => { return s.stat.isFile() }).then((arrFiles) => {
  16. return Promise.map(arrFiles, (f) => {
  17. if (moment(f.stat.ctime).isBefore(fifteenAgo, 'minute')) {
  18. return fs.unlink(path.join(uplTempPath, f.filename))
  19. }
  20. })
  21. })
  22. WIKI.logger.info('Purging orphaned upload files: [ COMPLETED ]')
  23. } catch (err) {
  24. WIKI.logger.error('Purging orphaned upload files: [ FAILED ]')
  25. WIKI.logger.error(err.message)
  26. }
  27. }