Skip to content

Commit bcd718d

Browse files
gorodet-skykirillov.anton2
andauthored
gedcom: add support for text/vnd.familysearch.gedcom (#789)
Co-authored-by: kirillov.anton2 <[email protected]>
1 parent 2995287 commit bcd718d

4 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎internal/magic/text.go‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,3 +623,44 @@ func lineHasRFC822Hint(b scan.Bytes, hints []rfc822Hint) bool {
623623
}
624624
return false
625625
}
626+
627+
func GEDCOM(raw []byte, limit uint32) bool {
628+
// Skip if empty
629+
if len(raw) == 0 {
630+
return false
631+
}
632+
633+
// GEDCOM header fits within first 4KB
634+
searchLimit := min(len(raw), 4096)
635+
raw = raw[:searchLimit]
636+
637+
b := scan.Bytes(raw)
638+
639+
// Skip BOM if present: UTF-8, UTF-16BE, UTF-16LE
640+
for _, bom := range [][]byte{
641+
{0xEF, 0xBB, 0xBF}, // UTF-8
642+
{0xFE, 0xFF}, // UTF-16BE
643+
{0xFF, 0xFE}, // UTF-16LE
644+
} {
645+
if bytes.HasPrefix(b, bom) {
646+
b.Advance(len(bom))
647+
break // Only one BOM can exist at the start
648+
}
649+
}
650+
651+
b.TrimLWS()
652+
653+
firstLine := b.Line()
654+
if !bytes.Equal(firstLine, []byte("0 HEAD")) {
655+
return false
656+
}
657+
658+
// "1 GEDC" is mandatory in the header
659+
for i := 0; i < 10; i++ {
660+
line := b.Line()
661+
if bytes.Equal(line, []byte("1 GEDC")) {
662+
return true
663+
}
664+
}
665+
return false
666+
}

‎mimetype_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ ENDHDR`,
402402
{"zlib", "\x78\x5e\xf3\xc8\xe4\x02\x00\x01\xb7\x00\xbc", "application/zlib", one},
403403
{"zst", "(\xb5/\xfd", "application/zstd", all},
404404
{"zst skippable frame", "\x50\x2A\x4D\x18", "application/zstd", none},
405+
{"gedcom", "0 HEAD\r\n1 GEDC\r\n2 VERS 5.5.5\r\n2 FORM LINEAGE-LINKED", "text/vnd.familysearch.gedcom", none},
405406
}
406407

407408
func TestDetect(t *testing.T) {

‎supported_mimes.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 203 Supported MIME types
1+
## 204 Supported MIME types
22
This file is automatically generated when running tests. Do not edit manually.
33

44
Extension | MIME type <br> Aliases | Hierarchy
@@ -206,3 +206,4 @@ Extension | MIME type <br> Aliases | Hierarchy
206206
**.ppm** | **image/x-portable-pixmap** | ppm>txt>root
207207
**.pam** | **image/x-portable-arbitrarymap** | pam>txt>root
208208
**.eml** | **message/rfc822** | eml>txt>root
209+
**.ged** | **text/vnd.familysearch.gedcom** | ged>txt>root

‎tree.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ var (
8686
alias("application/x-ogg")
8787
oggAudio = newMIME("audio/ogg", ".oga", magic.OggAudio)
8888
oggVideo = newMIME("video/ogg", ".ogv", magic.OggVideo)
89-
text = newMIME("text/plain", ".txt", magic.Text, svg, html, xml, php, js, lua, perl, python, ruby, json, ndJSON, rtf, srt, tcl, csv, tsv, vCard, iCalendar, warc, vtt, shell, netpbm, netpgm, netppm, netpam, rfc822)
89+
text = newMIME("text/plain", ".txt", magic.Text, svg, html, xml, php, js, lua, perl, python, ruby, json, ndJSON, rtf, srt, tcl, csv, tsv, vCard, iCalendar, warc, vtt, shell, netpbm, netpgm, netppm, netpam, rfc822, gedcom)
9090
xml = newMIME("text/xml", ".xml", magic.XML, rss, atom, x3d, kml, xliff, collada, gml, gpx, tcx, amf, threemf, xfdf, owl2, xhtml, cdxxml).
9191
alias("application/xml")
9292
xhtml = newMIME("application/xhtml+xml", ".html", magic.XHTML)
@@ -303,5 +303,6 @@ var (
303303
hlp = newMIME("application/x-os2-hlp", ".hlp", magic.Hlp)
304304
fm = newMIME("application/vnd.framemaker", ".fm", magic.FrameMaker)
305305
bufr = newMIME("application/bufr", ".bufr", magic.BUFR)
306+
gedcom = newMIME("text/vnd.familysearch.gedcom", ".ged", magic.GEDCOM)
306307
pcap = newMIME("application/vnd.tcpdump.pcap", ".pcap", magic.Pcap)
307308
)

0 commit comments

Comments
 (0)