h2non / filetype

Fast, dependency-free Go package to infer binary file types based on the magic numbers header signature

Home Page:https://pkg.go.dev/github.com/h2non/filetype?tab=doc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

add text/plain support ?

Valdenirmezadri opened this issue · comments

can you add this?

i try use AddMatcher but not work

my code:

var txtType = filetype.NewType("txt", "text/plain")

func txtMatcher(buf []byte) bool {
return len(buf) > 1 && buf[0] == 0x01 && buf[1] == 0x02
}

filetype.AddMatcher(txtType, txtMatcher)

// Check if the new type is supported by extension
if filetype.IsSupported("txt") {
	fmt.Println("New supported type: txt")
}

// Check if the new type is supported by MIME
if filetype.IsMIMESupported("text/plain") {
	fmt.Println("New supported MIME type: text/plain")
}

head := make([]byte, 261)
f, _ := file.Open()
f.Read(head)
kind, _ := filetype.Match(head)
if kind == filetype.Unknown {
	fmt.Println("Unknown file type")
	fmt.Println(kind)
	fmt.Println(head)
	return errors.New("Este arquivo não é permitido")
} else {
	fmt.Printf("File type matched: %s\n", kind.Extension)
}

image

The main problem is because there's no Magic Bytes for .txt, the same "problem" happens with csv, so there's no way to use this kind of validation.

You can implement some outside function to deal with this validation just checking the extension.