mirror of https://gitgud.io/fatchan/jschan.git
parent
4b87a14dfe
commit
7718a37af0
2 changed files with 42 additions and 25 deletions
@ -1,46 +1,62 @@ |
|||||||
'use strict'; |
'use strict'; |
||||||
|
|
||||||
const util = require('util') |
process |
||||||
, fs = require('fs') |
.on('uncaughtException', console.error) |
||||||
, unlink = util.promisify(fs.unlink) |
.on('unhandledRejection', console.error); |
||||||
, stat = util.promisify(fs.stat) |
|
||||||
, readdir = util.promisify(fs.readdir) |
const { stat, remove, readdir } = require('fs-extra') |
||||||
, uploadDirectory = require(__dirname+'/helpers/files/uploadDirectory.js') |
, uploadDirectory = require(__dirname+'/helpers/files/uploadDirectory.js') |
||||||
, msTime = require(__dirname+'/helpers/mstime.js') |
, msTime = require(__dirname+'/helpers/mstime.js') |
||||||
, Mongo = require(__dirname+'/db/db.js') |
, Mongo = require(__dirname+'/db/db.js') |
||||||
|
|
||||||
async function deleteCaptchas() { |
async function deleteCaptchas() { |
||||||
|
|
||||||
try { |
|
||||||
const files = await readdir(`${uploadDirectory}captcha/`); |
const files = await readdir(`${uploadDirectory}captcha/`); |
||||||
if (files.length > 0) { |
if (files.length > 0) { |
||||||
files.forEach(async (file) => { |
files.forEach(async (file) => { |
||||||
|
try { |
||||||
const filePath = `${uploadDirectory}captcha/${file}`; |
const filePath = `${uploadDirectory}captcha/${file}`; |
||||||
const stats = await stat(filePath).catch(e => console.error); |
const stats = await stat(filePath); |
||||||
if (!stats) { |
|
||||||
return; |
|
||||||
} |
|
||||||
const now = Date.now(); |
const now = Date.now(); |
||||||
const expiry = new Date(stats.ctime).getTime() + 6*1000*60; //6 minutes ahead
|
const expiry = new Date(stats.ctime).getTime()// + msTime.minute*5;
|
||||||
if (now > expiry) { |
if (now > expiry) { |
||||||
await unlink(filePath).catch(e => console.error); |
await remove(filePath); |
||||||
|
console.log(`Deleted expired captcha ${filePath}`) |
||||||
} |
} |
||||||
}); |
} catch (e) { |
||||||
|
/* |
||||||
|
catching here to still get the error, but it wont reject the promise |
||||||
|
returned by deleteCaptchas, since this is anon async function in the |
||||||
|
foreach loop. this way we dont stop deleting captchas if only one fails |
||||||
|
*/ |
||||||
|
console.error(e); |
||||||
} |
} |
||||||
} catch (err) { |
}); |
||||||
console.error(err); |
|
||||||
} |
} |
||||||
|
|
||||||
} |
} |
||||||
|
|
||||||
(async () => { |
(async () => { |
||||||
|
|
||||||
await Mongo.connect(); |
await Mongo.connect(); |
||||||
const { buildHomepage } = require(__dirname+'/helpers/build.js'); |
const { buildHomepage } = require(__dirname+'/helpers/build.js'); |
||||||
buildHomepage(); |
|
||||||
setInterval(buildHomepage, msTime.hour); //hourly rebuild homepage for posts/day
|
|
||||||
|
|
||||||
|
console.log('Starting schedules'); |
||||||
|
|
||||||
|
buildHomepage() |
||||||
deleteCaptchas(); |
deleteCaptchas(); |
||||||
setInterval(deleteCaptchas, msTime.minute*6); //delete files for expired captchas
|
setInterval(async () => { |
||||||
|
try { |
||||||
|
await buildHomepage(); |
||||||
|
} catch (e) { |
||||||
|
console.error(e); |
||||||
|
} |
||||||
|
}, msTime.hour); //hourly rebuild homepage for posts/day
|
||||||
|
|
||||||
|
setInterval(async () => { |
||||||
|
try { |
||||||
|
await deleteCaptchas(); |
||||||
|
} catch (e) { |
||||||
|
console.error(e); |
||||||
|
} |
||||||
|
}, msTime.minute*5); //delete files for expired captchas
|
||||||
|
|
||||||
})(); |
})(); |
||||||
|
Loading…
Reference in new issue