|
@@ -0,0 +1,57 @@
|
|
|
|
+'use strict';
|
|
|
|
+const { CrudService } = require('naf-framework-mongoose-free/lib/service');
|
|
|
|
+const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
|
+const _ = require('lodash');
|
|
|
|
+const assert = require('assert');
|
|
|
|
+const os = require('os');
|
|
|
|
+const { execSync } = require('child_process');
|
|
|
|
+//
|
|
|
|
+class UtilService extends CrudService {
|
|
|
|
+ constructor(ctx) {
|
|
|
|
+ super(ctx, 'util');
|
|
|
|
+ }
|
|
|
|
+ async getDrives() {
|
|
|
|
+ let lines;
|
|
|
|
+ const osType = os.platform().toLowerCase();
|
|
|
|
+ if (osType === 'win32') {
|
|
|
|
+ const cmd = 'wmic logicaldisk get Caption,FreeSpace,Size,VolumeSerialNumber,Description /format:list';
|
|
|
|
+ const cmdRes = execSync(cmd, { encoding: 'utf8' });
|
|
|
|
+ lines = cmdRes.split('\r\r\n');
|
|
|
|
+ } else {
|
|
|
|
+ const cmd = "df -P | awk 'NR > 1'";
|
|
|
|
+ const cmdRes = execSync(cmd, { encoding: 'utf8' });
|
|
|
|
+ lines = cmdRes.split('\n');
|
|
|
|
+ }
|
|
|
|
+ const data = this.changeToArray(lines);
|
|
|
|
+ return data;
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 整理数据
|
|
|
|
+ * @param {Array} lines 除去换行后的数组
|
|
|
|
+ */
|
|
|
|
+ changeToArray(lines) {
|
|
|
|
+ const returnArry = [];
|
|
|
|
+ lines = lines.filter(f => f !== '');
|
|
|
|
+ lines = _.chunk(lines, 5);
|
|
|
|
+ for (const line of lines) {
|
|
|
|
+ const obj = {};
|
|
|
|
+ for (const str of line) {
|
|
|
|
+ const arr = str.split('=');
|
|
|
|
+ const tag = _.head(arr);
|
|
|
|
+ const value = _.last(arr);
|
|
|
|
+ if (tag === 'Caption') obj.mounted = value;
|
|
|
|
+ else if (tag === 'FreeSpace') obj.available = parseFloat(value);
|
|
|
|
+ else if (tag === 'Size') obj.blocks = parseFloat(value);
|
|
|
|
+ else continue;
|
|
|
|
+ }
|
|
|
|
+ if (obj.blocks && obj.available) {
|
|
|
|
+ obj.used = obj.blocks - obj.available;
|
|
|
|
+ obj.capacity = Math.round((parseFloat(obj.used) / parseFloat(obj.blocks)) * 100) + '%';
|
|
|
|
+ }
|
|
|
|
+ returnArry.push(obj);
|
|
|
|
+ }
|
|
|
|
+ return returnArry;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+module.exports = UtilService;
|