Browse Source

Рефакторинг

Book Pauk 6 years ago
parent
commit
d62b475fdd
3 changed files with 11 additions and 3 deletions
  1. 2 1
      server/core/FileDecompressor.js
  2. 2 1
      server/core/ReaderWorker.js
  3. 7 1
      server/core/utils.js

+ 2 - 1
server/core/FileDecompressor.js

@@ -1,6 +1,7 @@
 const fs = require('fs-extra');
 const zlib = require('zlib');
 const crypto = require('crypto');
+const utils = require('./utils');
 const decompress = require('decompress');
 const FileDetector = require('./FileDetector');
 
@@ -51,7 +52,7 @@ class FileDecompressor {
         if (!await fs.pathExists(outFilename)) {
             await fs.writeFile(outFilename, await this.gzipBuffer(buf))
         } else {
-            await fs.utimes(outFilename, Date.now()/1000, Date.now()/1000);
+            await utils.touchFile(outFilename);
         }
 
         return outFilename;

+ 2 - 1
server/core/ReaderWorker.js

@@ -57,6 +57,7 @@ class ReaderWorker {
                 downloadedFilename = `${this.config.uploadDir}/${url.substr(7)}`;
                 if (!await fs.pathExists(downloadedFilename)) 
                     throw new Error('Файл не найден на сервере (возможно был удален как устаревший). Пожалуйста, загрузите файл с диска на сервер заново.');
+                await utils.touchFile(downloadedFilename);
                 isUploaded = true;
             }
             wState.set({progress: 100});
@@ -117,7 +118,7 @@ class ReaderWorker {
         if (!await fs.pathExists(outFilename)) {
             await fs.move(file.path, outFilename);
         } else {
-            await fs.utimes(outFilename, Date.now()/1000, Date.now()/1000);
+            await utils.touchFile(outFilename);
             await fs.remove(file.path);
         }
 

+ 7 - 1
server/core/utils.js

@@ -1,3 +1,4 @@
+const fs = require('fs-extra');
 const crypto = require('crypto');
 
 function sleep(ms) {
@@ -8,7 +9,12 @@ function randomHexString(len) {
     return crypto.randomBytes(len).toString('hex')
 }
 
+async function touchFile(filename) {
+    await fs.utimes(filename, Date.now()/1000, Date.now()/1000);
+}
+
 module.exports = {
     sleep,
-    randomHexString
+    randomHexString,
+    touchFile
 };