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.

31 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(process.cwd(), WIKI.config.paths.data, 'uploads')
  10. const ls = await fs.readdir(uplTempPath)
  11. const fifteenAgo = moment().subtract(15, 'minutes')
  12. await Promise.map(ls, (f) => {
  13. return fs.stat(path.join(uplTempPath, f)).then((s) => { return { filename: f, stat: s } })
  14. }).filter((s) => { return s.stat.isFile() }).then((arrFiles) => {
  15. return Promise.map(arrFiles, (f) => {
  16. if (moment(f.stat.ctime).isBefore(fifteenAgo, 'minute')) {
  17. return fs.unlink(path.join(uplTempPath, f.filename))
  18. }
  19. })
  20. })
  21. WIKI.logger.info('Purging orphaned upload files: [ COMPLETED ]')
  22. } catch (err) {
  23. WIKI.logger.error('Purging orphaned upload files: [ FAILED ]')
  24. WIKI.logger.error(err.message)
  25. }
  26. }