2018-04-29 21:55:36 +00:00
|
|
|
/* global WIKI */
|
|
|
|
|
|
|
|
const Promise = require('bluebird')
|
2019-02-13 22:20:46 +00:00
|
|
|
const fs = require('fs-extra')
|
2018-04-29 21:55:36 +00:00
|
|
|
const moment = require('moment')
|
|
|
|
const path = require('path')
|
|
|
|
|
2019-02-13 22:20:46 +00:00
|
|
|
module.exports = async () => {
|
2018-04-29 21:55:36 +00:00
|
|
|
WIKI.logger.info('Purging orphaned upload files...')
|
|
|
|
|
|
|
|
try {
|
2019-10-25 22:20:02 +00:00
|
|
|
const uplTempPath = path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, 'uploads')
|
2019-04-22 17:12:03 +00:00
|
|
|
await fs.ensureDir(uplTempPath)
|
2019-02-13 22:20:46 +00:00
|
|
|
const ls = await fs.readdir(uplTempPath)
|
2018-04-29 21:55:36 +00:00
|
|
|
const fifteenAgo = moment().subtract(15, 'minutes')
|
|
|
|
|
|
|
|
await Promise.map(ls, (f) => {
|
2019-02-13 22:20:46 +00:00
|
|
|
return fs.stat(path.join(uplTempPath, f)).then((s) => { return { filename: f, stat: s } })
|
2018-04-29 21:55:36 +00:00
|
|
|
}).filter((s) => { return s.stat.isFile() }).then((arrFiles) => {
|
|
|
|
return Promise.map(arrFiles, (f) => {
|
|
|
|
if (moment(f.stat.ctime).isBefore(fifteenAgo, 'minute')) {
|
2019-02-13 22:20:46 +00:00
|
|
|
return fs.unlink(path.join(uplTempPath, f.filename))
|
2018-04-29 21:55:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
WIKI.logger.info('Purging orphaned upload files: [ COMPLETED ]')
|
|
|
|
} catch (err) {
|
|
|
|
WIKI.logger.error('Purging orphaned upload files: [ FAILED ]')
|
|
|
|
WIKI.logger.error(err.message)
|
|
|
|
}
|
|
|
|
}
|