use fse.copy for performance

it uses the native fs.copyFile method
This commit is contained in:
Brian Gough 2021-05-14 15:49:20 +01:00
parent b456ea726d
commit 44e0742aa3
2 changed files with 9 additions and 22 deletions

View file

@ -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) {}

View file

@ -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)
})