return url from FileStoreHandler.uploadFileFromDisk

This commit is contained in:
Hayden Faulds 2017-11-10 15:47:47 +00:00
parent 0178f78249
commit 6e84c635cf
2 changed files with 16 additions and 14 deletions

View file

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

View file

@ -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", ->