schangxiang@126.com
2025-09-19 0821aa23eabe557c0d9ef5dbe6989c68be35d1fe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
 
const path = require('path');
const fs = require('mz/fs');
const rimraf = require('mz-modules/rimraf');
const moment = require('moment');
 
module.exports = app => {
  return class CleanTmpdir extends (app.Subscription || app.BaseContextClass) {
    static get schedule() {
      return {
        type: 'worker',
        cron: app.config.multipart.cleanSchedule.cron,
        immediate: false,
        // disable on stream mode
        disable: app.config.multipart.mode === 'stream',
      };
    }
 
    async _remove(dir) {
      const { ctx } = this;
      if (await fs.exists(dir)) {
        ctx.coreLogger.info('[egg-multipart:CleanTmpdir] removing tmpdir: %j', dir);
        try {
          await rimraf(dir);
          ctx.coreLogger.info('[egg-multipart:CleanTmpdir:success] tmpdir: %j has been removed', dir);
        } catch (err) {
          ctx.coreLogger.error('[egg-multipart:CleanTmpdir:error] remove tmpdir: %j error: %s',
            dir, err);
          ctx.coreLogger.error(err);
        }
      }
    }
 
    async subscribe() {
      const { ctx } = this;
      const config = ctx.app.config;
      ctx.coreLogger.info('[egg-multipart:CleanTmpdir] start clean tmpdir: %j', config.multipart.tmpdir);
      // last year
      const lastYear = moment().subtract(1, 'years');
      const lastYearDir = path.join(config.multipart.tmpdir, lastYear.format('YYYY'));
      await this._remove(lastYearDir);
      // 3 months
      for (let i = 1; i <= 3; i++) {
        const date = moment().subtract(i, 'months');
        const dir = path.join(config.multipart.tmpdir, date.format('YYYY/MM'));
        await this._remove(dir);
      }
      // 7 days
      for (let i = 1; i <= 7; i++) {
        const date = moment().subtract(i, 'days');
        const dir = path.join(config.multipart.tmpdir, date.format('YYYY/MM/DD'));
        await this._remove(dir);
      }
      ctx.coreLogger.info('[egg-multipart:CleanTmpdir] end');
    }
  };
};