Skip to content

Commit

Permalink
Add support for RSS, Atom, AMF and 3MF
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-vasile committed Jun 24, 2019
1 parent db9fd35 commit 40d6218
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 11 deletions.
3 changes: 3 additions & 0 deletions internal/matchers/signature.go
Expand Up @@ -109,6 +109,9 @@ func (xSig xmlSig) detect(in []byte) bool {
if len(xSig.localName) == 0 {
return bytes.Index(in, xSig.xmlns) > 0
}
if len(xSig.xmlns) == 0 {
return bytes.Index(in, xSig.localName) > 0
}

localNameIndex := bytes.Index(in, xSig.localName)
return localNameIndex != -1 && localNameIndex < bytes.Index(in, xSig.xmlns)
Expand Down
34 changes: 33 additions & 1 deletion internal/matchers/text.go
Expand Up @@ -29,14 +29,20 @@ var (
xmlSigs = []sig{
markupSig("<?XML"),
}
rssSigs = []sig{
newXmlSig("rss", ""),
}
atomSigs = []sig{
newXmlSig("feed", `xmlns="http://www.w3.org/2005/Atom"`),
}
kmlSigs = []sig{
newXmlSig("kml", `xmlns="http://www.opengis.net/kml/2.2"`),
newXmlSig("kml", `xmlns="http://earth.google.com/kml/2.0"`),
newXmlSig("kml", `xmlns="http://earth.google.com/kml/2.1"`),
newXmlSig("kml", `xmlns="http://earth.google.com/kml/2.2"`),
}
xliffSigs = []sig{
newXmlSig("xliff", `urn:oasis:names:tc:xliff:document:1.2`),
newXmlSig("xliff", `xmlns="urn:oasis:names:tc:xliff:document:1.2"`),
}
colladaSigs = []sig{
newXmlSig("COLLADA", `xmlns="http://www.collada.org/2005/11/COLLADASchema"`),
Expand All @@ -54,6 +60,12 @@ var (
x3dSigs = []sig{
newXmlSig("X3D", `xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"`),
}
amfSigs = []sig{
newXmlSig("amf", ""),
}
threemfSigs = []sig{
newXmlSig("model", `xmlns="http://schemas.microsoft.com/3dmanufacturing/core/2015/02"`),
}
vCardSigs = []sig{
ciSig("BEGIN:VCARD\n"),
}
Expand Down Expand Up @@ -222,6 +234,16 @@ func Svg(in []byte) bool {
return bytes.Contains(in, []byte("<svg"))
}

// Rss matches a Rich Site Summary file.
func Rss(in []byte) bool {
return detect(in, rssSigs)
}

// Atom matches an Atom Syndication Format file.
func Atom(in []byte) bool {
return detect(in, atomSigs)
}

// Kml matches a Keyhole Markup Language file.
func Kml(in []byte) bool {
return detect(in, kmlSigs)
Expand Down Expand Up @@ -252,6 +274,16 @@ func Tcx(in []byte) bool {
return detect(in, tcxSigs)
}

// Amf matches an Additive Manufacturing XML file.
func Amf(in []byte) bool {
return detect(in, amfSigs)
}

// Threemf matches a 3D Manufacturing Format file.
func Threemf(in []byte) bool {
return detect(in, threemfSigs)
}

// X3d matches an Extensible 3D Graphics file.
func X3d(in []byte) bool {
return detect(in, x3dSigs)
Expand Down
20 changes: 12 additions & 8 deletions mime_test.go
Expand Up @@ -123,14 +123,18 @@ var files = map[string]*node{
"woff2.woff2": woff2,

// XML and subtypes of XML
"xml.xml": xml,
"kml.kml": kml,
"xlf.xlf": xliff,
"dae.dae": collada,
"gml.gml": gml,
"gpx.gpx": gpx,
"tcx.tcx": tcx,
"x3d.x3d": x3d,
"xml.xml": xml,
"kml.kml": kml,
"xlf.xlf": xliff,
"dae.dae": collada,
"gml.gml": gml,
"gpx.gpx": gpx,
"tcx.tcx": tcx,
"x3d.x3d": x3d,
"amf.amf": amf,
"3mf.3mf": threemf,
"rss.rss": rss,
"atom.atom": atom,

"shp.shp": shp,
"shx.shx": shx,
Expand Down
6 changes: 5 additions & 1 deletion supported_mimes.md
@@ -1,4 +1,4 @@
## 106 Supported MIME types
## 110 Supported MIME types
This file is automatically generated when running tests. Do not edit manually.

Extension | MIME type
Expand Down Expand Up @@ -72,13 +72,17 @@ Extension | MIME type
**html** | text/html; charset=utf-8
**svg** | image/svg+xml
**xml** | text/xml; charset=utf-8
**rss** | application/rss+xml
**atom** | application/atom+xml
**x3d** | model/x3d+xml
**kml** | application/vnd.google-earth.kml+xml
**xlf** | application/x-xliff+xml
**dae** | model/vnd.collada+xml
**gml** | application/gml+xml
**gpx** | application/gpx+xml
**tcx** | application/vnd.garmin.tcx+xml
**amf** | application/x-amf
**3mf** | application/vnd.ms-package.3dmanufacturing-3dmodel+xml
**php** | text/x-php; charset=utf-8
**js** | application/javascript
**lua** | text/x-lua
Expand Down
39 changes: 39 additions & 0 deletions testdata/3mf.3mf
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<model unit="inch" xml:lang="en-US" xmlns="http://schemas.microsoft.com/3dmanufacturing/core/2015/02">
<metadata name="Copyright">
Copyright (c) 2015 3MF Consortium. All rights reserved.
</metadata>
<resources>
<object id="1" type="model">
<mesh>
<vertices>
<vertex x="0" y="0" z="0" />
<vertex x="1" y="0" z="0" />
<vertex x="1" y="2" z="0" />
<vertex x="0" y="2" z="0" />
<vertex x="0" y="0" z="3" />
<vertex x="1" y="0" z="3" />
<vertex x="1" y="2" z="3" />
<vertex x="0" y="2" z="3" />
</vertices>
<triangles>
<triangle v1="3" v2="2" v3="1" />
<triangle v1="1" v2="0" v3="3" />
<triangle v1="4" v2="5" v3="6" />
<triangle v1="6" v2="7" v3="4" />
<triangle v1="0" v2="1" v3="5" />
<triangle v1="5" v2="4" v3="0" />
<triangle v1="1" v2="2" v3="6" />
<triangle v1="6" v2="5" v3="1" />
<triangle v1="2" v2="3" v3="7" />
<triangle v1="7" v2="6" v3="2" />
<triangle v1="3" v2="0" v3="4" />
<triangle v1="4" v2="7" v3="3" />
</triangles>
</mesh>
</object>
</resources>
<build>
<item objectid="1" />
</build>
</model>
109 changes: 109 additions & 0 deletions testdata/amf.amf
@@ -0,0 +1,109 @@
<?xml version="1.0" encoding="utf-8"?>
<amf unit="inch" version="1.1">
<metadata type="name">Split Pyramid</metadata>
<metadata type="author">John Smith</metadata>
<object id="1">
<mesh>
<vertices>
<vertex>
<coordinates>
<x>0</x>
<y>0</y>
<z>0</z>
</coordinates>
</vertex>
<vertex>
<coordinates>
<x>1</x>
<y>0</y>
<z>0</z>
</coordinates>
</vertex>
<vertex>
<coordinates>
<x>0</x>
<y>1</y>
<z>0</z>
</coordinates>
</vertex>
<vertex>
<coordinates>
<x>1</x>
<y>1</y>
<z>0</z>
</coordinates>
</vertex>
<vertex>
<coordinates>
<x>0.5</x>
<y>0.5</y>
<z>1</z>
</coordinates>
</vertex>
</vertices>
<volume materialid="2">
<metadata type="name">Hard side</metadata>
<triangle>
<v1>2</v1>
<v2>1</v2>
<v3>0</v3>
</triangle>
<triangle>
<v1>0</v1>
<v2>1</v2>
<v3>4</v3>
</triangle>
<triangle>
<v1>4</v1>
<v2>1</v2>
<v3>2</v3>
</triangle>
<triangle>
<v1>0</v1>
<v2>4</v2>
<v3>2</v3>
</triangle>
</volume>
<volume materialid="3">
<metadata type="name">Soft side</metadata>
<triangle>
<v1>2</v1>
<v2>3</v2>
<v3>1</v3>
</triangle>
<triangle>
<v1>1</v1>
<v2>3</v2>
<v3>4</v3>
</triangle>
<triangle>
<v1>4</v1>
<v2>3</v2>
<v3>2</v3>
</triangle>
<triangle>
<v1>4</v1>
<v2>2</v2>
<v3>1</v3>
</triangle>
</volume>
</mesh>
</object>
<material id="2">
<metadata type="name">Hard material</metadata>
<color>
<r>0.1</r>
<g>0.1</g>
<b>0.1</b>
</color>
</material>
<material id="3">
<metadata type="name">Soft material</metadata>
<color>
<r>0</r>
<g>0.9</g>
<b>0.9</b>
<a>0.5</a>
</color>
</material>
</amf>
17 changes: 17 additions & 0 deletions testdata/atom.atom
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Example Feed</title>
<link href="http://example.org/" />
<updated>2003-12-13T18:30:02Z</updated>
<author>
<name>John Doe</name>
</author>
<id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
<entry>
<title>Atom-Powered Robots Run Amok</title>
<link href="http://example.org/2003/12/13/atom03" />
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
<updated>2003-12-13T18:30:02Z</updated>
<summary>Some text.</summary>
</entry>
</feed>
18 changes: 18 additions & 0 deletions testdata/rss.rss
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>RSS Title</title>
<description>This is an example of an RSS feed</description>
<link>http://www.example.com/main.html</link>
<lastBuildDate>Mon, 06 Sep 2010 00:01:00 +0000 </lastBuildDate>
<pubDate>Sun, 06 Sep 2009 16:20:00 +0000</pubDate>
<ttl>1800</ttl>
<item>
<title>Example entry</title>
<description>Here is some text containing an interesting description.</description>
<link>http://www.example.com/blog/post/1</link>
<guid isPermaLink="false">7bd204c6-1655-4c27-aeee-53f933c5395f</guid>
<pubDate>Sun, 06 Sep 2009 16:20:00 +0000</pubDate>
</item>
</channel>
</rss>
6 changes: 5 additions & 1 deletion tree.go
Expand Up @@ -35,7 +35,7 @@ var (
fits = newNode("application/fits", "fits", matchers.Fits)
ogg = newNode("application/ogg", "ogg", matchers.Ogg)
txt = newNode("text/plain", "txt", matchers.Txt, html, svg, xml, php, js, lua, perl, python, json, rtf, tcl, csv, tsv, vCard)
xml = newNode("text/xml; charset=utf-8", "xml", matchers.Xml, x3d, kml, xliff, collada, gml, gpx, tcx)
xml = newNode("text/xml; charset=utf-8", "xml", matchers.Xml, rss, atom, x3d, kml, xliff, collada, gml, gpx, tcx, amf, threemf)
json = newNode("application/json", "json", matchers.Json, geoJson)
csv = newNode("text/csv", "csv", matchers.Csv)
tsv = newNode("text/tab-separated-values", "tsv", matchers.Tsv)
Expand All @@ -50,13 +50,17 @@ var (
tcl = newNode("text/x-tcl", "tcl", matchers.Tcl)
vCard = newNode("text/vcard", "vcf", matchers.VCard)
svg = newNode("image/svg+xml", "svg", matchers.Svg)
rss = newNode("application/rss+xml", "rss", matchers.Rss)
atom = newNode("application/atom+xml", "atom", matchers.Atom)
x3d = newNode("model/x3d+xml", "x3d", matchers.X3d)
kml = newNode("application/vnd.google-earth.kml+xml", "kml", matchers.Kml)
xliff = newNode("application/x-xliff+xml", "xlf", matchers.Xliff)
collada = newNode("model/vnd.collada+xml", "dae", matchers.Collada)
gml = newNode("application/gml+xml", "gml", matchers.Gml)
gpx = newNode("application/gpx+xml", "gpx", matchers.Gpx)
tcx = newNode("application/vnd.garmin.tcx+xml", "tcx", matchers.Tcx)
amf = newNode("application/x-amf", "amf", matchers.Amf)
threemf = newNode("application/vnd.ms-package.3dmanufacturing-3dmodel+xml", "3mf", matchers.Threemf)
png = newNode("image/png", "png", matchers.Png)
jpg = newNode("image/jpeg", "jpg", matchers.Jpg)
bpg = newNode("image/bpg", "bpg", matchers.Bpg)
Expand Down

0 comments on commit 40d6218

Please sign in to comment.