浏览代码

Рефакторинг, поправки

Book Pauk 5 年之前
父节点
当前提交
eac5fdcec0
共有 1 个文件被更改,包括 17 次插入4 次删除
  1. 17 4
      server/core/LibSharedStorage/MegaStorage.js

+ 17 - 4
server/core/LibSharedStorage/MegaStorage.js

@@ -6,15 +6,27 @@ const utils = require('../utils');
 
 
 class MegaStorage {
 class MegaStorage {
     constructor() {
     constructor() {
-        this.readingFiles = false;
-        this.stats = null;
+        this.inited = false;
     }
     }
 
 
     async init(config) {
     async init(config) {
         this.config = config;
         this.config = config;
         this.megaStorageDir = config.megaStorageDir;
         this.megaStorageDir = config.megaStorageDir;
+        this.statsPath = `${this.megaStorageDir}/stats.json`;
         this.compressLevel = (config.compressLevel ? config.compressLevel : 4);
         this.compressLevel = (config.compressLevel ? config.compressLevel : 4);
         await fs.ensureDir(this.megaStorageDir);
         await fs.ensureDir(this.megaStorageDir);
+
+        this.readingFiles = false;
+        this.stats = {};
+
+        if (await fs.pathExists(this.statsPath)) {
+            this.stats = Object.assign({},
+                JSON.parse(await fs.readFile(this.statsPath, 'utf8')),
+                this.stats
+            );
+        }
+
+        this.inited = true;
     }
     }
 
 
     async nameHash(filename) {
     async nameHash(filename) {
@@ -63,17 +75,18 @@ class MegaStorage {
         if (!dir)
         if (!dir)
             dir = this.megaStorageDir;
             dir = this.megaStorageDir;
 
 
+        let result;
         const files = await fs.readdir(dir, { withFileTypes: true });
         const files = await fs.readdir(dir, { withFileTypes: true });
         for (const file of files) {
         for (const file of files) {
             if (!this.readingFiles)
             if (!this.readingFiles)
                 return;
                 return;
             const found = path.resolve(dir, file.name);
             const found = path.resolve(dir, file.name);
             if (file.isDirectory())
             if (file.isDirectory())
-                await this._findFiles(callback, found);
+                result = await this._findFiles(callback, found);
             else
             else
                 callback(found);
                 callback(found);
         }
         }
-        return true;
+        return result;
     }
     }
 
 
     async startFindFiles(callback, dir) {
     async startFindFiles(callback, dir) {