Documentation
¶
Overview ¶
Package gofast is the front door of the go-fast libraries: it reads a file of any of their formats by sending it to the library that reads it, and opens zip, tar and gzip archives.
Detect decides where an input goes, from a declared media type, the extension of its name, and its bytes. Extractor reads it, and its Child method is a gofastcore.ChildFunc that reads embedded documents of any format, so a PDF attached to a message or a workbook embedded in a presentation is read too. The libraries themselves never import each other; this package imports them all.
The command cmd/gofast reads files, directories and archives in parallel, with each archive member a document of its own.
Index ¶
- Constants
- Variables
- func KnownExtension(name string) bool
- type Extractor
- func (e *Extractor) Child(ctx context.Context, name, mimeType string, r io.ReaderAt, size int64, ...) (*gofastcore.Document, error)
- func (e *Extractor) Extract(ctx context.Context, r io.ReaderAt, size int64, opts *gofastcore.Options) (*gofastcore.Document, error)
- func (e *Extractor) ExtractKind(ctx context.Context, k Kind, mimeType string, r io.ReaderAt, size int64, ...) (*gofastcore.Document, error)
- func (e *Extractor) Formats() []string
- func (e *Extractor) Walk(ctx context.Context, k Kind, name string, r io.ReaderAt, size int64, ...) error
- type Kind
- type Member
- type Skipped
- type WalkOptions
Constants ¶
const ( // DefaultMaxMemberBytes bounds the decompressed size of one archive // member, which is held in memory to be read (a member stored // uncompressed in a zip or plain tar file is read in place). DefaultMaxMemberBytes = 1 << 30 // DefaultMaxArchiveBytes bounds the bytes decompressed from one // archive and the archives inside it, the defence against an archive // that decompresses to far more than any real one. DefaultMaxArchiveBytes = 64 << 30 // DefaultMaxArchiveMembers bounds the members of an archive read as // one document with the members as its children (an archive embedded // in a document, or Extract). The command reads each member of an // archive it is given as a document of its own, with no such bound. DefaultMaxArchiveMembers = 10000 )
Defaults of the bounds on archives.
const ( LibPDF = "pdf" // go-fast-pdf LibOOXML = "ooxml" // go-fast-ooxml: Word, Excel and PowerPoint 2007 and later LibMSBin = "msbin" // go-fast-msbin: Word, Excel and PowerPoint 97-2003 LibODF = "odf" // go-fast-odf: OpenDocument LibRTF = "rtf" // go-fast-rtf LibEmail = "email" // go-fast-email: eml, mbox, msg, TNEF LibHTML = "html" // go-fast-html: HTML, Markdown, text, EPUB, CSV, TSV, notebooks LibXML = "xml" // go-fast-xml: XML of any schema, with profiles for some // LibArchive is an archive this package opens: Format is "zip", // "tar" or "gzip". LibArchive = "archive" )
The libraries an input can be routed to, and the archive formats this package opens itself.
Variables ¶
var ErrArchive = errors.New("gofast: archive cannot be read")
ErrArchive wraps an archive that cannot be read at all.
var ErrLimit = errors.New("gofast: archive bound reached")
ErrLimit wraps a walk stopped by MaxArchiveBytes.
Functions ¶
func KnownExtension ¶
KnownExtension reports whether a name's extension is one Detect routes to a library or an archive: the files a directory stands for.
Types ¶
type Extractor ¶
type Extractor struct {
// MaxMemberBytes bounds one archive member decompressed. Zero means
// DefaultMaxMemberBytes; negative, no bound.
MaxMemberBytes int64
// MaxArchiveBytes bounds the bytes decompressed from one archive and
// the archives inside it. Zero means DefaultMaxArchiveBytes;
// negative, no bound.
MaxArchiveBytes int64
// MaxArchiveMembers bounds the members read of an archive that is
// read as one document (Extract, or an archive embedded in a
// document). Zero means DefaultMaxArchiveMembers; negative, no bound.
MaxArchiveMembers int
// Charset is the character set, as a MIME charset label ("shift_jis",
// "windows-1251"), of text that declares none and whose set the
// library reading it cannot determine from its bytes. go-fast-email
// uses it for such text in messages (gofastemail.Extractor.Charset).
// HTML, Markdown, plain text, CSV, TSV, JSON and YAML that
// go-fast-html refuses with gofasthtml.ErrEncodingUnknown, and XML
// whose encoding go-fast-xml does not read, are read again in it; it
// is not given to either before, since there it would override a
// <meta> or an XML declaration. Empty means none: such input is
// refused, and a message's text keeps U+FFFD.
Charset string
// XMLProfiles are go-fast-xml profiles (gofastxml.LoadProfile reads
// one) tried, in order, before its built-in ones. Each is chosen by
// the root element as they are, so they can be given for a batch of
// any formats. A loaded profile is safe to share.
XMLProfiles []*gofastxml.Profile
// XMLAttributes decides, for XML read generically (no profile fits),
// whether each attribute is a note: gofastxml.AttributesNotes, the
// default, or gofastxml.AttributesNone, for data-style XML whose
// attributes are keys and codes.
XMLAttributes gofastxml.Attributes
}
Extractor reads any format of the go-fast libraries, and archives. The zero value is ready to use and safe for concurrent use.
func (*Extractor) Child ¶
func (e *Extractor) Child(ctx context.Context, name, mimeType string, r io.ReaderAt, size int64, opts *gofastcore.Options) (*gofastcore.Document, error)
Child is a gofastcore.ChildFunc that reads an embedded document of any format: the extractor every library's Options.Child can be set to.
func (*Extractor) Extract ¶
func (e *Extractor) Extract(ctx context.Context, r io.ReaderAt, size int64, opts *gofastcore.Options) (*gofastcore.Document, error)
Extract reads an input of any format, decided by Detect from Options.Name and the bytes. When Options.Child is nil, embedded documents are read by e.Child, so a PDF attached to a message or a workbook inside a presentation is read too. An archive is a document of format zip, tar or gzip with one empty body part (with part.no_text, at info), whose children are its members. An input no library reads gives an error wrapping gofastcore.ErrUnsupported.
func (*Extractor) ExtractKind ¶
func (e *Extractor) ExtractKind(ctx context.Context, k Kind, mimeType string, r io.ReaderAt, size int64, opts *gofastcore.Options) (*gofastcore.Document, error)
ExtractKind reads an input whose Kind is decided, as Detect gives it. mimeType is the media type the input is declared with, if any: a charset parameter is the encoding of HTML, Markdown, text, CSV and TSV, and header=present makes the first row of CSV and TSV a header row.
func (*Extractor) Formats ¶
Formats lists the formats Extract reads: those of every library, and the archives.
func (*Extractor) Walk ¶
func (e *Extractor) Walk(ctx context.Context, k Kind, name string, r io.ReaderAt, size int64, opts WalkOptions, fn func(Member) error, skip func(Skipped)) error
Walk calls fn for every file of the archive r (whose Kind is k, as Detect gives it, and whose name is name), in the order the archive stores them, and skip for every member left out. An archive inside it is opened in turn, its members' paths then beginning with its own ("a.zip/b.tar/c.pdf"), down to opts.MaxDepth; a deeper one is skipped. The bounds of e (MaxMemberBytes, MaxArchiveBytes) hold for the whole walk. fn owns each Member: it calls Release, when set, once it no longer needs the bytes, and may do so after Walk returns. An error from fn stops the walk and is returned; an archive that cannot be read gives an error wrapping ErrArchive, after the members before the damage, and one stopped by MaxArchiveBytes an error wrapping ErrLimit.
type Kind ¶
Kind is where an input goes: the library that reads it and, where the detection decides it, the format within that library (as the library's Extractor.Format names it), or the archive format. The zero Kind is an input no library reads.
func Detect ¶
Detect decides where an input goes. A declared media type (mimeType, as a Content-Type gives it) decides first, unless it is one mail programs give whatever the file (application/octet-stream, application/zip, text/plain); then the extension of name; then, but for text/plain, which is text, the bytes:
- "%PDF-1." or "%PDF-2." in the first 1 KiB is PDF;
- a zip archive is Office if it has [Content_Types].xml, OpenDocument or EPUB if its mimetype member says so, and otherwise an archive;
- gzip and tar (ustar) are archives;
- "{\rtf" is RTF;
- an OLE compound file is go-fast-msbin's when its directory holds the streams of a Word, Excel or PowerPoint 97-2003 file (go-fast-msbin's Detect), and otherwise email;
- a TNEF stream, a mailbox's "From " line (an address, then a date with a time) or a message's header lines (at least two of the headers messages have, one of them From, Received, Return-Path, Message-ID, MIME-Version or Delivered-To) are email;
- an OpenDocument flat XML file is OpenDocument;
- XML is go-fast-xml's: input that begins with an XML declaration, or whose first element declares a namespace, unless that element is html or svg, which go-fast-html reads;
- binary input (a NUL byte, or many control characters, in the first 4 KiB: images, audio, executables) gives the zero Kind;
- anything else is what go-fast-html's Detect says of it: HTML when it is markup, else plain text.
Whatever decides that an input is go-fast-xml's (its media type, its name or its bytes), an Office document saved as one XML file is go-fast-ooxml's: Flat OPC (pkg:package), Word 2003's XML document (w:wordDocument) and Excel 2003's XML spreadsheet (Workbook), by the root element and its namespace, within the first 64 KiB.
A name whose extension is an Office 97-2003 format (.doc, .xls, .ppt and their templates and shows), or one of their media types, is decided by the bytes: see legacy.
type Member ¶
type Member struct {
// Path is the member's name in the archive, slash-separated, with no
// leading slash, "." or ".." element, and no control character.
Path string
// Size is the member's size, decompressed.
Size int64
// R reads the member's bytes.
R io.ReaderAt
// Release, when not nil, is called once the member's bytes are no
// longer needed: it gives back what WalkOptions.Hold accounted for
// them, and for an archive the member is in that was held for it.
Release func()
// contains filtered or unexported fields
}
Member is one file in an archive.
type Skipped ¶
type Skipped struct {
Path string
Reason string
// Limit says that a bound of this package left it out (as opposed to
// a member that cannot be read: encrypted, compressed with a method
// not supported, damaged).
Limit bool
// Note says that nothing is missing: the member is another name for
// one that is read (a tar hard link).
Note bool
}
Skipped is a member that was not read, and why.
type WalkOptions ¶
type WalkOptions struct {
// MaxDepth bounds how deeply archives inside the archive are opened:
// 1 opens none. Zero means gofastcore.DefaultMaxDepth.
MaxDepth int
// Hold, when not nil, is called with the number of bytes a member
// (or a gzip file read into memory to be decompressed) will take in
// memory before they are read, and with own, the bytes
// the walk itself holds at the moment (the archives it is inside,
// which it cannot give back before it is out of them). It returns
// the function that gives the bytes back (part of Member.Release).
// It may wait, to bound the memory the members being read take
// together, but not for own; an error stops the walk.
Hold func(n, own int64) (release func(), err error)
}
WalkOptions bounds a walk of an archive.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
gofast
command
Command gofast extracts documents of every format the go-fast libraries read (PDF; Word, Excel and PowerPoint; OpenDocument; RTF; email; HTML, Markdown, text, EPUB, CSV, TSV and Jupyter notebooks) into their shared document model and prints them as JSON, Markdown or plain text.
|
Command gofast extracts documents of every format the go-fast libraries read (PDF; Word, Excel and PowerPoint; OpenDocument; RTF; email; HTML, Markdown, text, EPUB, CSV, TSV and Jupyter notebooks) into their shared document model and prints them as JSON, Markdown or plain text. |