| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 | 'use strict';/** * Created by Tony on 2018/7/27. */const fs = require('fs');const cron = require('node-schedule');const remove_file_types = ['.xlsx', '.pdf'];function chkIsRemoveType(file) {    let rst = false;    for (const fType of remove_file_types) {        if (file.indexOf(fType) === (file.length - fType.length)) {            rst = true;            break;        }    }    return rst;}const jobObj = {    started: false,    createJob: function(rule, rootPath) {        let localRule = rule;        if (!localRule) {            // setup schedule rule            localRule = new cron.RecurrenceRule();            // 3:15AM, everyday            localRule.dayOfWeek = [1, 2, 3, 4, 5, 6, 0];            localRule.hour = 3;            localRule.minute = 15;        }        cron.scheduleJob(localRule, function() {            const path = rootPath;            fs.exists(path, function(exists) {                if (exists) {                    fs.readdir(path, function(err, files) {                        const currentTime = (new Date()).valueOf();                        const timeGap = currentTime - (1000 * 60 * 60 * 24 * 0.5); // half day before                        files.forEach(function(file, index) {                            if (chkIsRemoveType(file)) {                                const curPath = path + '/' + file;                                fs.stat(curPath, function(err, data) {                                    if (timeGap > data.mtime) {                                        // console.log('removing... ' + curPath);                                        fs.unlink(curPath, function(err) {                                            if (err) {                                                console.log(err);                                            }                                        });                                    }                                });                            }                        });                    });                }            });        });    },};module.exports = jobObj;
 |