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

use fs.copyFile for performance
This commit is contained in:
Jakob Ackermann 2021-05-18 11:43:43 +02:00 committed by GitHub
commit 7cf019ab78
2 changed files with 7 additions and 23 deletions

View file

@ -35,8 +35,12 @@ module.exports = UrlCache = {
if (error != null) {
return callback(error)
}
return UrlCache._copyFile(pathToCachedUrl, destPath, function (error) {
return fs.copyFile(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 +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) {
if (callback == null) {
callback = function (error) {}

View file

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