|
@@ -0,0 +1,120 @@
|
|
|
+'use strict';
|
|
|
+const { CrudService } = require('naf-framework-mongoose-free/lib/service');
|
|
|
+const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
+const _ = require('lodash');
|
|
|
+const d = require('diskinfo');
|
|
|
+const fs = require('fs');
|
|
|
+const path = require('path');
|
|
|
+const moment = require('moment');
|
|
|
+const nodemailer = require('nodemailer');
|
|
|
+//
|
|
|
+class DiskService extends CrudService {
|
|
|
+ constructor(ctx) {
|
|
|
+ super(ctx, 'disk');
|
|
|
+ }
|
|
|
+
|
|
|
+ async diskInfo() {
|
|
|
+ return new Promise(resolve => {
|
|
|
+ d.getDrives(function(err, aDrives) {
|
|
|
+ aDrives = aDrives.map(i => _.omit(i, [ 'filesystem' ]));
|
|
|
+ resolve(aDrives);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+ async toDir({ route = [ '' ] } = {}) {
|
|
|
+ const root = this.app.config.dirRoot;
|
|
|
+ const p = path.resolve(root, ...route);
|
|
|
+ const dir = fs.readdirSync(p);
|
|
|
+ const arr = [];
|
|
|
+ for (const file of dir) {
|
|
|
+ const fp = path.resolve(p, file);
|
|
|
+ const fi = fs.statSync(fp);
|
|
|
+ const obj = { name: file };
|
|
|
+ if (fi.isDirectory()) obj.type = 'dir';
|
|
|
+ else if (fi.isFile()) obj.type = 'file';
|
|
|
+ else obj.type = undefined;
|
|
|
+ // 文件大小, kb
|
|
|
+ obj.size = fi.size / 100;
|
|
|
+ // 创建时间
|
|
|
+ obj.create_time = moment(fi.ctime).format('YYYY-MM-DD HH:mm:ss');
|
|
|
+ arr.push(obj);
|
|
|
+ }
|
|
|
+ return arr;
|
|
|
+ }
|
|
|
+
|
|
|
+ async delFile({ route, target, times }) {
|
|
|
+ if (!_.isArray(route)) throw new BusinessError(ErrorCode.BADPARAM, '路径错误');
|
|
|
+ const root = this.app.config.dirRoot;
|
|
|
+ const p = path.resolve(root, ...route, target || '');
|
|
|
+ if (fs.statSync(p).isFile()) {
|
|
|
+ if (_.get(times, 'start_time') && _.get(times, 'end_time')) {
|
|
|
+ const r = this.inRange(moment(fs.statSync(p).ctime).format('YYYY-MM-DD HH:mm:ss'), times);
|
|
|
+ if (!r) return;
|
|
|
+ }
|
|
|
+ fs.unlinkSync(p);
|
|
|
+ } else await this.dirDelete(p, times);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 删除路径下的所有内容
|
|
|
+ * @param {String} filePath 路径
|
|
|
+ * @param {Object} times 里面有开始时间和结束时间: start_time;end_time
|
|
|
+ */
|
|
|
+ async dirDelete(filePath, times) {
|
|
|
+ if (fs.existsSync(filePath)) {
|
|
|
+ const files = fs.readdirSync(filePath);
|
|
|
+ for (const file of files) {
|
|
|
+ const curPath = path.resolve(filePath, file);
|
|
|
+ const fi = fs.statSync(curPath);
|
|
|
+ if (fi.isDirectory()) {
|
|
|
+ // recurse
|
|
|
+ this.dirDelete(curPath);
|
|
|
+ } else {
|
|
|
+ // delete file
|
|
|
+ if (_.get(times, 'start_time') && _.get(times, 'end_time')) {
|
|
|
+ const r = this.inRange(moment(fi.ctime).format('YYYY-MM-DD HH:mm:ss'), times);
|
|
|
+ if (!r) continue;
|
|
|
+ }
|
|
|
+ fs.unlinkSync(curPath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!(_.get(times, 'start_time') && _.get(times, 'end_time'))) fs.rmdirSync(filePath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断时间是否在范围内
|
|
|
+ * @param {String} create_time 文件的时间范围
|
|
|
+ * @param {Object} times 里面有开始时间和结束时间: start_time;end_time
|
|
|
+ */
|
|
|
+ inRange(create_time, times) {
|
|
|
+ console.log(moment(create_time).isBetween(times.start_time, times.end_time, null, '[]'));
|
|
|
+ return moment(create_time).isBetween(times.start_time, times.end_time, null, '[]');
|
|
|
+ }
|
|
|
+
|
|
|
+ async sendWarningEmail() {
|
|
|
+ const { receiver, sender } = this.app.config;
|
|
|
+ if (!receiver) return;
|
|
|
+ if (!sender) return;
|
|
|
+ const config = {
|
|
|
+ host: 'smtp.163.com',
|
|
|
+ port: 465,
|
|
|
+ secure: true,
|
|
|
+ auth: sender,
|
|
|
+ };
|
|
|
+ const mailOptions = {
|
|
|
+ from: `"free服务器" <${sender.user}>`, // 邮件来源
|
|
|
+ to: receiver, // 邮件发送到哪里,多个邮箱使用逗号隔开
|
|
|
+ subject: 'free服务器提示', // 邮件主题
|
|
|
+ text: '项目空间不足', // 存文本类型的邮件正文
|
|
|
+ html: '<b>清理科企项目空间啦!!</b>', // html类型的邮件正文
|
|
|
+ };
|
|
|
+ const transporter = nodemailer.createTransport(config);
|
|
|
+ transporter.sendMail(mailOptions, (error, info) => {
|
|
|
+ if (error) console.log(error);
|
|
|
+ else console.log(info);
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = DiskService;
|