From 44e0742aa33b37bddafb8cd10f25f1ba999de1ac Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 14 May 2021 15:49:20 +0100 Subject: [PATCH] use fse.copy for performance it uses the native fs.copyFile method --- services/clsi/app/js/UrlCache.js | 26 +++++---------------- services/clsi/test/unit/js/UrlCacheTests.js | 5 ++-- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/services/clsi/app/js/UrlCache.js b/services/clsi/app/js/UrlCache.js index e8ee10dc67..90c6486f98 100644 --- a/services/clsi/app/js/UrlCache.js +++ b/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) {} diff --git a/services/clsi/test/unit/js/UrlCacheTests.js b/services/clsi/test/unit/js/UrlCacheTests.js index 2b991245a9..7276b1325d 100644 --- a/services/clsi/test/unit/js/UrlCacheTests.js +++ b/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) })