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.

33 lines
1.1 KiB

  1. require('../core/worker')
  2. /* global WIKI */
  3. const Promise = require('bluebird')
  4. const fs = Promise.promisifyAll(require('fs-extra'))
  5. const moment = require('moment')
  6. const path = require('path')
  7. module.exports = async (job) => {
  8. WIKI.logger.info('Purging orphaned upload files...')
  9. try {
  10. const uplTempPath = path.resolve(process.cwd(), WIKI.config.paths.data, 'uploads')
  11. const ls = await fs.readdirAsync(uplTempPath)
  12. const fifteenAgo = moment().subtract(15, 'minutes')
  13. await Promise.map(ls, (f) => {
  14. return fs.statAsync(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.unlinkAsync(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. }