Просмотр исходного кода

Merge pull request #231 from overleaf/bg-use-fse-copy

use fs.copyFile for performance
Jakob Ackermann 5 лет назад
Родитель
Сommit
7cf019ab78
2 измененных файлов с 7 добавлено и 23 удалено
  1. 5 20
      services/clsi/app/js/UrlCache.js
  2. 2 3
      services/clsi/test/unit/js/UrlCacheTests.js

+ 5 - 20
services/clsi/app/js/UrlCache.js

@@ -35,8 +35,12 @@ module.exports = UrlCache = {
         if (error != null) {
         if (error != null) {
           return callback(error)
           return callback(error)
         }
         }
-        return UrlCache._copyFile(pathToCachedUrl, destPath, function (error) {
+        return fs.copyFile(pathToCachedUrl, destPath, function (error) {
           if (error != null) {
           if (error != null) {
+            logger.error(
+              { err: error, from: pathToCachedUrl, to: destPath },
+              'error copying file from cache'
+            )
             return UrlCache._clearUrlDetails(project_id, url, () =>
             return UrlCache._clearUrlDetails(project_id, url, () =>
               callback(error)
               callback(error)
             )
             )
@@ -163,25 +167,6 @@ module.exports = UrlCache = {
     )}`
     )}`
   },
   },
 
 
-  _copyFile(from, to, _callback) {
-    if (_callback == null) {
-      _callback = function (error) {}
-    }
-    const callbackOnce = function (error) {
-      if (error != null) {
-        logger.error({ err: error, from, to }, 'error copying file from cache')
-      }
-      _callback(error)
-      return (_callback = function () {})
-    }
-    const writeStream = fs.createWriteStream(to)
-    const readStream = fs.createReadStream(from)
-    writeStream.on('error', callbackOnce)
-    readStream.on('error', callbackOnce)
-    writeStream.on('close', callbackOnce)
-    return writeStream.on('open', () => readStream.pipe(writeStream))
-  },
-
   _clearUrlFromCache(project_id, url, callback) {
   _clearUrlFromCache(project_id, url, callback) {
     if (callback == null) {
     if (callback == null) {
       callback = function (error) {}
       callback = function (error) {}

+ 2 - 3
services/clsi/test/unit/js/UrlCacheTests.js

@@ -27,7 +27,7 @@ describe('UrlCache', function () {
         'settings-sharelatex': (this.Settings = {
         'settings-sharelatex': (this.Settings = {
           path: { clsiCacheDir: '/cache/dir' }
           path: { clsiCacheDir: '/cache/dir' }
         }),
         }),
-        fs: (this.fs = {})
+        fs: (this.fs = { copyFile: sinon.stub().yields() })
       }
       }
     }))
     }))
   })
   })
@@ -248,7 +248,6 @@ describe('UrlCache', function () {
     beforeEach(function () {
     beforeEach(function () {
       this.cachePath = 'path/to/cached/url'
       this.cachePath = 'path/to/cached/url'
       this.destPath = 'path/to/destination'
       this.destPath = 'path/to/destination'
-      this.UrlCache._copyFile = sinon.stub().callsArg(2)
       this.UrlCache._ensureUrlIsInCache = sinon
       this.UrlCache._ensureUrlIsInCache = sinon
         .stub()
         .stub()
         .callsArgWith(3, null, this.cachePath)
         .callsArgWith(3, null, this.cachePath)
@@ -268,7 +267,7 @@ describe('UrlCache', function () {
     })
     })
 
 
     it('should copy the file to the new location', function () {
     it('should copy the file to the new location', function () {
-      return this.UrlCache._copyFile
+      return this.fs.copyFile
         .calledWith(this.cachePath, this.destPath)
         .calledWith(this.cachePath, this.destPath)
         .should.equal(true)
         .should.equal(true)
     })
     })