baraka

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2020 License: MIT Imports: 10 Imported by: 0

README

baraka

Go Report Card codecov Build Status go.dev reference

a tool for handling file uploads for http servers

makes it easier to make operations with files from the http request.

install

go get github.com/xis/baraka

usage

func main() {
	// create a parser
	parser := baraka.NewParser(baraka.ParserOptions{
		MaxFileSize:   5 << 20,
		MaxFileCount:  5,
		MaxParseCount: 5,
	})

	router := gin.Default()
	router.POST("/upload", func(c *gin.Context) {
		// parsing
		p, err := parser.Parse(c.Request)
		if err != nil {
			fmt.Println(err)
		}
		// saving
		err = p.Save("image_", "./")
		if err != nil {
			fmt.Println(err)
		}
		// getting the part in the []byte format
		parts := p.Content()
		buf := parts[0].Content
		fmt.Println(len(buf))
	})
	router.Run()
}

you can use baraka with the other http server libraries, just pass the http.Request to the parser.Parse function.

filter function

filter function is a custom function which filters the files that comes from the request. for example you can read file bytes and identify the file, return true if you wanna pass the file, return false if you don't.

filter example

func main() {
	// create a parserr
	parser := baraka.NewParser(baraka.ParserOptions{
		// passing filter function
		Filter: func(data []byte) bool {
			// get first 512 bytes for checking content type
			buf := data[:512]
			// detect the content type
			fileType := http.DetectContentType(buf)
			// if it is jpeg then pass the file
			if fileType == "image/jpeg" {
				return true
			}
			// if not then don't pass
			return false
		},
	})

getting information

p, err := parser.Parse(c.Request)
if err != nil {
	fmt.Println(err)
}
// prints filenames
fmt.Println(p.Filenames())
// prints total files count
fmt.Println(p.Length())
// prints content types of files
fmt.Println(p.ContentTypes())

getting json data

p, err := parser.Parse(c.Request)
if err != nil {
   fmt.Println(err)
}
jsonStrings, err := p.GetJSON()
if err != nil {
   fmt.Println(err)
}

more

v1.1.1 Handling file uploads simple and memory friendly in Go with Baraka

i will make a blog post for v1.2.0

contributing

pull requests are welcome. please open an issue first to discuss what you would like to change.

please make sure to update tests as appropriate.

license

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Informer added in v1.0.2

type Informer interface {
	Content() []*Part
	Length() int
	Filenames() []string
	ContentTypes() []string
	GetJSON() ([]string, error)
}

Informer is a interface that wraps information functions about parsed multipart request

type Parser

type Parser interface {
	Parse(r *http.Request) (Processor, error)
}

Parser is an interface which determine which method to use when parsing multipart files

func NewParser added in v1.2.0

func NewParser(options ParserOptions) Parser

NewParser creates a new Parser, if you give an empty ParserOptions it uses defaults.

type ParserOptions added in v1.2.0

type ParserOptions struct {
	MaxFileSize       int
	MaxFileCount      int
	MaxParseCount     int
	MaxAvailableSlice int
	Filter            func(b []byte) bool
}

ParserOptions contains parser's options about parsing. filter function runs when parsing the file

type Part added in v1.2.0

type Part struct {
	Name    string
	Headers textproto.MIMEHeader
	Size    int
	Content []byte
}

Part is a struct that you can access the all contents of the multipart.Part

func NewPart added in v1.2.0

func NewPart(name string, headers *textproto.MIMEHeader, size int, content []byte) Part

NewPart creates a new Part struct

type Processor added in v1.2.0

type Processor interface {
	Saver
	Informer
}

Processor is the interface that wraps the Saver and the Informer interfaces.

type Request added in v1.2.0

type Request struct {
	// contains filtered or unexported fields
}

Request implements the Processor interface. contains an array of parts. parser.Parse() returns Request as Processor.

func NewRequest added in v1.2.0

func NewRequest(parts ...*Part) *Request

NewRequest creates a new Request with parts inside

func (*Request) Content added in v1.2.0

func (s *Request) Content() []*Part

Content method gives you []*Part, so you can access all the data of the parts.

func (*Request) ContentTypes added in v1.2.0

func (request *Request) ContentTypes() []string

ContentTypes returns content types of the parts

func (*Request) Filenames added in v1.2.0

func (request *Request) Filenames() []string

Filenames returns filenames of the parts

func (*Request) GetJSON added in v1.2.0

func (request *Request) GetJSON() ([]string, error)

GetJSON returns json strings of the application/json parts separately

func (*Request) Length added in v1.2.0

func (request *Request) Length() int

Length returns length of the parts

func (*Request) Save added in v1.2.0

func (s *Request) Save(prefix string, path string, excludedContentTypes ...string) error

Save is a method for saving parts into disk.

type Saver

type Saver interface {
	Save(prefix string, path string, excludedContentTypes ...string) error
}

Saver is a interface that wraps the Save function

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL