Эх сурвалжийг харах

use fse.copy for performance

it uses the native fs.copyFile method
Brian Gough 5 жил өмнө
parent
commit
44e0742aa3

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

@@ -19,6 +19,7 @@ const UrlFetcher = require('./UrlFetcher')
 const Settings = require('settings-sharelatex')
 const crypto = require('crypto')
 const fs = require('fs')
+const fse = require('fs-extra')
 const logger = require('logger-sharelatex')
 const async = require('async')
 
@@ -35,8 +36,12 @@ module.exports = UrlCache = {
         if (error != null) {
           return callback(error)
         }
-        return UrlCache._copyFile(pathToCachedUrl, destPath, function (error) {
+        return fse.copy(pathToCachedUrl, destPath, function (error) {
           if (error != null) {
+            logger.error(
+              { err: error, from: pathToCachedUrl, to: destPath },
+              'error copying file from cache'
+            )
             return UrlCache._clearUrlDetails(project_id, url, () =>
               callback(error)
             )
@@ -163,25 +168,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) {
     if (callback == null) {
       callback = function (error) {}

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

@@ -27,7 +27,8 @@ describe('UrlCache', function () {
         'settings-sharelatex': (this.Settings = {
           path: { clsiCacheDir: '/cache/dir' }
         }),
-        fs: (this.fs = {})
+        fs: (this.fs = {}),
+        'fs-extra': (this.fse = { copy: sinon.stub().yields() })
       }
     }))
   })
@@ -268,7 +269,7 @@ describe('UrlCache', function () {
     })
 
     it('should copy the file to the new location', function () {
-      return this.UrlCache._copyFile
+      return this.fse.copy
         .calledWith(this.cachePath, this.destPath)
         .should.equal(true)
     })