From 6e84c635cf332e37dfbd17d54e1aea5c7bcfbb7e Mon Sep 17 00:00:00 2001 From: Hayden Faulds Date: Fri, 10 Nov 2017 15:47:47 +0000 Subject: [PATCH] return url from FileStoreHandler.uploadFileFromDisk --- .../FileStore/FileStoreHandler.coffee | 9 ++++---- .../FileStore/FileStoreHandlerTests.coffee | 21 ++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/services/web/app/coffee/Features/FileStore/FileStoreHandler.coffee b/services/web/app/coffee/Features/FileStore/FileStoreHandler.coffee index 92efe7bfcb..88f55bc698 100644 --- a/services/web/app/coffee/Features/FileStore/FileStoreHandler.coffee +++ b/services/web/app/coffee/Features/FileStore/FileStoreHandler.coffee @@ -21,9 +21,9 @@ module.exports = FileStoreHandler = return callback(new Error("can not upload symlink")) _cb = callback - callback = (err) -> + callback = (err, url) -> callback = -> # avoid double callbacks - _cb(err) + _cb(err, url) logger.log project_id:project_id, file_id:file_id, fsPath:fsPath, "uploading file from disk" readStream = fs.createReadStream(fsPath) @@ -31,9 +31,10 @@ module.exports = FileStoreHandler = logger.err err:err, project_id:project_id, file_id:file_id, fsPath:fsPath, "something went wrong on the read stream of uploadFileFromDisk" callback err readStream.on "open", () -> + url = FileStoreHandler._buildUrl(project_id, file_id) opts = method: "post" - uri: FileStoreHandler._buildUrl(project_id, file_id) + uri: url timeout:fiveMinsInMs writeStream = request(opts) writeStream.on "error", (err)-> @@ -45,7 +46,7 @@ module.exports = FileStoreHandler = logger.err {err, statusCode: response.statusCode}, "error uploading to filestore" callback(err) else - callback(null) + callback(null, url) readStream.pipe writeStream getFileStream: (project_id, file_id, query, callback)-> diff --git a/services/web/test/unit/coffee/FileStore/FileStoreHandlerTests.coffee b/services/web/test/unit/coffee/FileStore/FileStoreHandlerTests.coffee index 73f32f3a60..ea7c7a4cbb 100644 --- a/services/web/test/unit/coffee/FileStore/FileStoreHandlerTests.coffee +++ b/services/web/test/unit/coffee/FileStore/FileStoreHandlerTests.coffee @@ -16,7 +16,7 @@ describe "FileStoreHandler", -> }) @writeStream = my:"writeStream" - on: (type, cb)-> + on: (type, cb)-> if type == "response" cb({statusCode: 200}) @readStream = {my:"readStream", on: sinon.stub()} @@ -38,7 +38,7 @@ describe "FileStoreHandler", -> @isSafeOnFileSystem = true it "should create read stream", (done)-> - @fs.createReadStream.returns + @fs.createReadStream.returns pipe:-> on: (type, cb)-> if type == "open" @@ -49,8 +49,8 @@ describe "FileStoreHandler", -> it "should pipe the read stream to request", (done)-> @request.returns(@writeStream) - @fs.createReadStream.returns - on: (type, cb)-> + @fs.createReadStream.returns + on: (type, cb)-> if type == "open" cb() pipe:(o)=> @@ -59,9 +59,9 @@ describe "FileStoreHandler", -> @handler.uploadFileFromDisk @project_id, @file_id, @fsPath, => it "should pass the correct options to request", (done)-> - @fs.createReadStream.returns + @fs.createReadStream.returns pipe:-> - on: (type, cb)-> + on: (type, cb)-> if type == "open" cb() @handler.uploadFileFromDisk @project_id, @file_id, @fsPath, => @@ -70,23 +70,24 @@ describe "FileStoreHandler", -> done() it "builds the correct url", (done)-> - @fs.createReadStream.returns + @fs.createReadStream.returns pipe:-> - on: (type, cb)-> + on: (type, cb)-> if type == "open" cb() @handler.uploadFileFromDisk @project_id, @file_id, @fsPath, => @handler._buildUrl.calledWith(@project_id, @file_id).should.equal true done() - it 'should callback with null', (done) -> + it 'should callback with the url', (done) -> @fs.createReadStream.returns pipe:-> on: (type, cb)-> if type == "open" cb() - @handler.uploadFileFromDisk @project_id, @file_id, @fsPath, (err) => + @handler.uploadFileFromDisk @project_id, @file_id, @fsPath, (err, url) => expect(err).to.not.exist + expect(url).to.equal(@handler._buildUrl()) done() describe "symlink", ->