Documentation
¶
Overview ¶
Package magic identifies the physical format and text encoding of file content.
Index ¶
Examples ¶
Constants ¶
const ( FormatLFSPointer = "git-lfs-pointer" FormatText = "text" FormatHTML = "html" FormatXML = "xml" FormatSVG = "svg" FormatJSON = "json" FormatZIP = "zip" FormatTAR = "tar" FormatPHAR = "phar" FormatGZIP = "gzip" FormatBZIP2 = "bzip2" FormatXZ = "xz" FormatZstd = "zstd" FormatPDF = "pdf" FormatCFBF = "cfbf" FormatPNG = "png" FormatJPEG = "jpeg" FormatGIF = "gif" FormatELF = "elf" FormatMachO = "mach-o" FormatJavaClass = "java-class" FormatPE = "pe" FormatWASM = "wasm" FormatAR = "ar" )
Format values reported in Result.Format.
const ( EncodingUTF8 = "utf-8" EncodingUTF16LE = "utf-16le" EncodingUTF16BE = "utf-16be" EncodingUTF32LE = "utf-32le" EncodingUTF32BE = "utf-32be" )
Encoding values reported in Result.Encoding.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Reason ¶
type Reason string
Reason explains why a classification is provisional or unknown.
const ( // ReasonNone means the classification is final for the supplied input. ReasonNone Reason = "" // ReasonNeedMore means more bytes could change a prefix classification. ReasonNeedMore Reason = "need-more" // ReasonInvalidText means the bytes are neither accepted text nor // recognised binary content. ReasonInvalidText Reason = "invalid-text" )
type Result ¶
type Result struct {
Kind Kind
MIME string
Format string
Encoding string
Reason Reason
NeedBytes int
}
Result describes the physical format and text properties of content.
MIME never includes a charset parameter. Encoding uses lowercase registered names. A BOM identifies Encoding even when its content is invalid.
func Detect ¶
Detect classifies data as the complete content of a file.
Example ¶
package main
import (
"fmt"
"github.com/git-pkgs/magic"
)
func main() {
result := magic.Detect([]byte("package main\n"))
fmt.Println(result.Kind, result.Format, result.MIME, result.Encoding)
}
Output: text text text/plain utf-8
Example (LfsPointer) ¶
result := Detect([]byte(lfsTestPointer)) fmt.Println(result.Kind, result.Format, result.MIME, result.Encoding)
Output: text git-lfs-pointer text/plain utf-8
func DetectPrefix ¶
DetectPrefix classifies an intentionally bounded file prefix.
ReasonNeedMore reports that later bytes could change the answer. NeedBytes is reserved for a known minimum total length and is zero in this release.
Example ¶
package main
import (
"fmt"
"github.com/git-pkgs/magic"
)
func main() {
result := magic.DetectPrefix([]byte("hello"))
fmt.Println(result.Kind, result.Reason)
}
Output: text need-more
func DetectWithOptions ¶ added in v0.5.0
DetectWithOptions applies the same control policy to UTF-8 and decoded Unicode. Binary format signatures take precedence over text options.
type TextValidator ¶ added in v0.5.0
type TextValidator struct {
// contains filtered or unexported fields
}
TextValidator validates chunks in order with fixed memory and no input references. The zero value is ready for use. Assign TextValidator{} to start another stream. It checks text encoding and controls; format signatures require Detect or DetectPrefix.
Example ¶
package main
import (
"fmt"
"io"
"strings"
"github.com/git-pkgs/magic"
)
func main() {
var validator magic.TextValidator
reader := strings.NewReader("package main\nfunc main() {}\n")
if _, err := io.Copy(&validator, reader); err != nil {
panic(err)
}
result := validator.Result(magic.Options{})
fmt.Println(result.Kind, result.Encoding)
}
Output: text utf-8
func (TextValidator) Result ¶ added in v0.5.0
func (v TextValidator) Result(options Options) Result
Result snapshots the bytes written so far without ending the stream. Options apply to this snapshot. Prefix permits an incomplete final code point. Only Kind, Encoding and Reason are populated; prefixes report ReasonNeedMore.