Skip to content

Commit

Permalink
Add support for GeoJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-vasile committed Apr 14, 2019
1 parent b0e9bd8 commit db305cf
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 18 deletions.
46 changes: 46 additions & 0 deletions internal/matchers/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,52 @@ func Json(in []byte) bool {
return parsed == len(in)
}

// GeoJson matches a RFC 7946 GeoJSON file.
//
// BUG(gabriel-vasile): The "type" key should be searched for in the root object.
func GeoJson(in []byte) bool {
in = trimLWS(in)
// geojson is always an object
if in[0] != '{' {
return false
}

s := []byte(`"type"`)
si := bytes.Index(in, s)
sl := len(s)

if si == -1 {
return false
}

// skip the "type" part
in = in[si+sl:]
// skip any whitespace before the colon
in = trimLWS(in)
// skip any whitesapce after the colon
// not checking if char is colon because json matcher already did check
in = trimLWS(in[1:])

geoJsonTypes := [][]byte{
[]byte(`"Feature"`),
[]byte(`"FeatureCollection"`),
[]byte(`"Point"`),
[]byte(`"LineString"`),
[]byte(`"Polygon"`),
[]byte(`"MultiPoint"`),
[]byte(`"MultiLineString"`),
[]byte(`"MultiPolygon"`),
[]byte(`"GeometryCollection"`),
}
for _, t := range geoJsonTypes {
if bytes.HasPrefix(in, t) {
return true
}
}

return false
}

// Js matches a Javascript file.
func Js(in []byte) bool {
return detect(in, jsSigs)
Expand Down
28 changes: 15 additions & 13 deletions mime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,21 @@ var files = map[string]*node{
"a.m4b": aMp4,

// source code
"a.html": html,
"a.svg": svg,
"b.svg": svg,
"a.txt": txt,
"a.php": php,
"a.ps": ps,
"a.json": json,
"a.rtf": rtf,
"a.js": js,
"a.lua": lua,
"a.pl": perl,
"a.py": python,
"a.tcl": tcl,
"a.html": html,
"a.svg": svg,
"b.svg": svg,
"a.txt": txt,
"a.php": php,
"a.ps": ps,
"a.json": json,
"a.geojson": geoJson,
"b.geojson": geoJson,
"a.rtf": rtf,
"a.js": js,
"a.lua": lua,
"a.pl": perl,
"a.py": python,
"a.tcl": tcl,

// binary
"a.class": class,
Expand Down
3 changes: 2 additions & 1 deletion supported_mimes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 69 Supported MIME types
## 70 Supported MIME types
This file is automatically generated when running tests. Do not edit manually.

Extension | MIME type
Expand Down Expand Up @@ -63,6 +63,7 @@ Extension | MIME type
**pl** | text/x-perl
**py** | application/x-python
**json** | application/json
**geojson** | application/geo+json
**rtf** | text/rtf
**tcl** | text/x-tcl
**gz** | application/gzip
Expand Down
10 changes: 10 additions & 0 deletions testdata/a.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [125.6, 10.1]
},
"properties": {
"name": "Dinagat Islands"
}
}
1 change: 1 addition & 0 deletions testdata/b.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[28.143464326858517,45.24112435573255],[28.142788410186768,45.24123767220149],[28.14276695251465,45.241037479619976],[28.14348578453064,45.24093549444874],[28.143464326858517,45.24112435573255]]]}},{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[28.137568831443787,45.24693153374828],[28.13750177621841,45.246669043227094],[28.137721717357635,45.24662372099991],[28.1377512216568,45.24690887274392],[28.137568831443787,45.24693153374828]]]}}]}
9 changes: 5 additions & 4 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ var (
html, svg, xml, php, js, lua, perl, python, json, rtf, tcl)
xml = newNode("text/xml; charset=utf-8", "xml", matchers.Xml,
x3d, kml, collada, gml, gpx, tcx)
json = newNode("application/json", "json", matchers.Json)
html = newNode("text/html; charset=utf-8", "html", matchers.Html)
php = newNode("text/x-php; charset=utf-8", "php", matchers.Php)
rtf = newNode("text/rtf", "rtf", matchers.Rtf)
json = newNode("application/json", "json", matchers.Json, geoJson)
geoJson = newNode("application/geo+json", "geojson", matchers.GeoJson)
html = newNode("text/html; charset=utf-8", "html", matchers.Html)
php = newNode("text/x-php; charset=utf-8", "php", matchers.Php)
rtf = newNode("text/rtf", "rtf", matchers.Rtf)

js = newNode("application/javascript", "js", matchers.Js)
lua = newNode("text/x-lua", "lua", matchers.Lua)
Expand Down

0 comments on commit db305cf

Please sign in to comment.