Skip to content

Commit

Permalink
Fix anacronw#126: Correct detecting content-type of svgs
Browse files Browse the repository at this point in the history
  • Loading branch information
JustDoItSascha committed Nov 13, 2019
1 parent 168eb90 commit 9e4673b
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,26 @@ function defaultKey (req, file, cb) {
}

function autoContentType (req, file, cb) {
file.stream.once('data', function (firstChunk) {
var type = fileType(firstChunk)
var data = ''
file.stream.on('data', function(chunk) {
data += chunk.toString();
})

file.stream.once('end', function () {
var type = fileType(data)
var mime

if (type) {
mime = type.mime
} else if (isSvg(firstChunk)) {
} else if (isSvg(data)) {
mime = 'image/svg+xml'
} else {
mime = 'application/octet-stream'
}

var outStream = new stream.PassThrough()

outStream.write(firstChunk)
outStream.write(data)
file.stream.pipe(outStream)

cb(null, mime, outStream)
Expand Down

0 comments on commit 9e4673b

Please sign in to comment.