Send content-length header when getting file

This commit is contained in:
James Allen 2014-05-23 13:54:20 +01:00
parent de0d79e1bf
commit daf4f1ffd4
7 changed files with 38 additions and 25 deletions

View file

@ -28,12 +28,13 @@ module.exports =
getFileStream: (location, name, callback = (err, res)->)-> getFileStream: (location, name, callback = (err, res)->)->
filteredName = filterName name filteredName = filterName name
logger.log location:location, name:filteredName, "getting file" logger.log location:location, name:filteredName, "getting file"
sourceStream = fs.createReadStream "#{location}/#{filteredName}" path = "#{location}/#{filteredName}"
sourceStream.on 'error', (err) -> fs.stat path, (error, stat) ->
logger.err err:err, location:location, name:name, "Error reading from file" sourceStream = fs.createReadStream path
callback err sourceStream.on 'error', (err) ->
callback null,sourceStream logger.err err:err, location:location, name:name, "Error reading from file"
callback err
callback null, sourceStream, stat.size
copyFile: (location, fromName, toName, callback = (err)->)-> copyFile: (location, fromName, toName, callback = (err)->)->
filteredFromName=filterName fromName filteredFromName=filterName fromName

View file

@ -12,7 +12,7 @@ module.exports =
{key, bucket} = req {key, bucket} = req
{format, style} = req.query {format, style} = req.query
logger.log key:key, bucket:bucket, format:format, style:style, "reciving request to get file" logger.log key:key, bucket:bucket, format:format, style:style, "reciving request to get file"
FileHandler.getFile bucket, key, {format:format,style:style}, (err, fileStream)-> FileHandler.getFile bucket, key, {format:format,style:style}, (err, fileStream, size)->
if err? if err?
logger.err err:err, key:key, bucket:bucket, format:format, style:style, "problem getting file" logger.err err:err, key:key, bucket:bucket, format:format, style:style, "problem getting file"
if !res.finished and res?.send? if !res.finished and res?.send?
@ -22,6 +22,7 @@ module.exports =
res.send 200 res.send 200
else else
logger.log key:key, bucket:bucket, format:format, style:style, "sending file to response" logger.log key:key, bucket:bucket, format:format, style:style, "sending file to response"
res.header("Content-Length", size)
fileStream.pipe res fileStream.pipe res
insertFile: (req, res)-> insertFile: (req, res)->

View file

@ -22,20 +22,20 @@ module.exports =
(done)-> PersistorManager.deleteFile bucket, convetedKey, done (done)-> PersistorManager.deleteFile bucket, convetedKey, done
], callback ], callback
getFile: (bucket, key, opts = {}, callback)-> getFile: (bucket, key, opts = {}, callback = (err, fileStream, size) ->)->
logger.log bucket:bucket, key:key, opts:opts, "getting file" logger.log bucket:bucket, key:key, opts:opts, "getting file"
if !opts.format? and !opts.style? if !opts.format? and !opts.style?
@_getStandardFile bucket, key, opts, callback @_getStandardFile bucket, key, opts, callback
else else
@_getConvertedFile bucket, key, opts, callback @_getConvertedFile bucket, key, opts, callback
_getStandardFile: (bucket, key, opts, callback)-> _getStandardFile: (bucket, key, opts, callback = (err, fileStream, size) ->)->
PersistorManager.getFileStream bucket, key, (err, fileStream)-> PersistorManager.getFileStream bucket, key, (err, fileStream, size)->
if err? if err?
logger.err bucket:bucket, key:key, opts:opts, "error getting fileStream" logger.err bucket:bucket, key:key, opts:opts, "error getting fileStream"
callback err, fileStream callback err, fileStream, size
_getConvertedFile: (bucket, key, opts, callback)-> _getConvertedFile: (bucket, key, opts, callback = (err, fileStream, size) ->)->
convetedKey = KeyBuilder.addCachingToKey(key, opts) convetedKey = KeyBuilder.addCachingToKey(key, opts)
PersistorManager.checkIfFileExists bucket, convetedKey, (err, exists)=> PersistorManager.checkIfFileExists bucket, convetedKey, (err, exists)=>
if exists if exists

View file

@ -67,7 +67,7 @@ module.exports =
return callback(err) return callback(err)
@sendFile bucketName, key, fsPath, callback @sendFile bucketName, key, fsPath, callback
getFileStream: (bucketName, key, callback = (err, res)->)-> getFileStream: (bucketName, key, callback = (err, res, size)->)->
logger.log bucketName:bucketName, key:key, "getting file from s3" logger.log bucketName:bucketName, key:key, "getting file from s3"
s3Client = knox.createClient s3Client = knox.createClient
key: settings.filestore.s3.key key: settings.filestore.s3.key
@ -76,7 +76,7 @@ module.exports =
s3Stream = s3Client.get(key) s3Stream = s3Client.get(key)
s3Stream.end() s3Stream.end()
s3Stream.on 'response', (res) -> s3Stream.on 'response', (res) ->
callback null, res callback null, res, res.headers["content-length"]
s3Stream.on 'error', (err) -> s3Stream.on 'error', (err) ->
logger.err err:err, bucketName:bucketName, key:key, "error getting file stream from s3" logger.err err:err, bucketName:bucketName, key:key, "error getting file stream from s3"
callback err callback err

View file

@ -66,11 +66,12 @@ describe "FSPersistorManagerTests", ->
describe "getFileStream", -> describe "getFileStream", ->
it "should use correct file location", (done) -> it "should use correct file location", (done) ->
@Fs.createReadStream.returns( @Fs.createReadStream.returns(@stream = on:->)
on:-> @Fs.stat = sinon.stub().callsArgWith(1, null, { size: @size = 42 })
) @FSPersistorManager.getFileStream @location, @name1, (err, res, size)=>
@FSPersistorManager.getFileStream @location, @name1, (err,res)=>
@Fs.createReadStream.calledWith("#{@location}/#{@name1Filtered}").should.equal.true @Fs.createReadStream.calledWith("#{@location}/#{@name1Filtered}").should.equal.true
res.should.equal @stream
size.should.equal @size
done() done()
describe "copyFile", -> describe "copyFile", ->

View file

@ -43,15 +43,16 @@ describe "FileController", ->
project_id:@project_id project_id:@project_id
file_id:@file_id file_id:@file_id
@res = @res =
setHeader: -> header: sinon.stub()
@fileStream = {} @fileStream = {}
describe "getFile", -> describe "getFile", ->
it "should pipe the stream", (done)-> it "should pipe the stream", (done)->
@FileHandler.getFile.callsArgWith(3, null, @fileStream) @FileHandler.getFile.callsArgWith(3, null, @fileStream, @size = 42)
@fileStream.pipe = (res)=> @fileStream.pipe = (res)=>
res.should.equal @res res.should.equal @res
res.header.calledWith("Content-Length", @size).should.equal true
done() done()
@controller.getFile @req, @res @controller.getFile @req, @res

View file

@ -45,13 +45,22 @@ describe "S3PersistorManagerTests", ->
it "should use correct key", (done)-> it "should use correct key", (done)->
@stubbedKnoxClient.get.returns( @response =
on:-> headers:
"content-length": @size = 42
@stubbedKnoxClient.get.returns(@stream =
on: (e, callback) =>
if e == "response"
callback(@response)
end:-> end:->
) )
@S3PersistorManager.getFileStream @bucketName, @key, @fsPath, (err)=> @S3PersistorManager.getFileStream @bucketName, @key, (err, res, size) =>
@stubbedKnoxClient.get.calledWith(@key).should.equal true res.should.equal @response
done() size.should.equal @size
@stubbedKnoxClient.get.calledWith(@key).should.equal true
done()
describe "sendFile", -> describe "sendFile", ->