Skip to content

Commit

Permalink
fix(gpx-upload): allow arbitrary upload field names
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Kristian Flaatten committed Feb 26, 2016
1 parent 385f33e commit 64fa83b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
37 changes: 21 additions & 16 deletions src/routes/api_v1.litcoffee
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
file.extension = file.originalname.split('.').reverse()[0]
cb null, true
apiv1.use '/gpx/parse', upload.fields [name: 'files[]']
apiv1.use '/gpx/parse', upload.any()
readFileSync = require('fs').readFileSync
DOMParser = require('xmldom').DOMParser
Expand All @@ -85,21 +85,26 @@
apiv1.post '/gpx/parse', (req, res, next) ->
res.end() if not req.files
for field, files of req.files
for file, i in files
req.files[field][i] =
fieldname: file.fieldname
filename: file.originalname
extension: file.extension
mimetype: file.mimetype
geojson: null
if file.extension is 'gpx'
dom = domparser.parseFromString readFileSync file.path, 'utf8'
req.files[field][i].geojson = geojsonFromGpx dom
else
req.files[field][i].error = "Invalid extension '#{file.extension}'"
files = {}
for f in req.files
files[f.fieldname] = [] if not files[f.fieldname]
file =
fieldname: f.fieldname
filename: f.originalname
extension: f.extension
mimetype: f.mimetype
geojson: null
if file.extension is 'gpx'
dom = domparser.parseFromString readFileSync f.path, 'utf8'
file.geojson = geojsonFromGpx dom
else
file.error = "Invalid extension '#{file.extension}'"
files[f.fieldname].push file
res.json req.files
res.json files
module.exports = apiv1
15 changes: 14 additions & 1 deletion test/routes/api_v1-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ describe '/gpx/parse', ->
assert.equal typeof res.body['files[]'][0].geojson, 'object'
.end done

it 'should parse uploaded GPX file with arbitrary field name', (done) ->
@timeout 5000
req.post url
.attach 'gpx', resolve __dirname, '../data/totland.gpx'
.expect 200
.expect (res) ->
assert.equal res.body['gpx'].length, 1
assert.equal res.body['gpx'][0].fieldname, 'gpx'
assert.equal res.body['gpx'][0].filename, 'totland.gpx'
assert.equal res.body['gpx'][0].extension, 'gpx'
assert.equal res.body['gpx'][0].mimetype, 'application/gpx+xml'
assert.equal typeof res.body['gpx'][0].geojson, 'object'
.end done

it 'should return error for non GPX file', (done) ->
req.post url
.attach 'files[]', resolve __dirname, '../data/file.json'
Expand All @@ -136,4 +150,3 @@ describe '/gpx/parse', ->
assert.equal res.body['files[]'][0].geojson, null
assert.equal res.body['files[]'][0].error, 'Invalid extension \'json\''
.end done

0 comments on commit 64fa83b

Please sign in to comment.