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)->)->
filteredName = filterName name
logger.log location:location, name:filteredName, "getting file"
sourceStream = fs.createReadStream "#{location}/#{filteredName}"
sourceStream.on 'error', (err) ->
logger.err err:err, location:location, name:name, "Error reading from file"
callback err
callback null,sourceStream
path = "#{location}/#{filteredName}"
fs.stat path, (error, stat) ->
sourceStream = fs.createReadStream path
sourceStream.on 'error', (err) ->
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)->)->
filteredFromName=filterName fromName

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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