baraka

package module
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2021 License: MIT Imports: 9 Imported by: 0

README

                         
 ▄▄▄▄    ▄▄▄       ██▀███   ▄▄▄       ██ ▄█▀▄▄▄      
▓█████▄ ▒████▄    ▓██ ▒ ██▒▒████▄     ██▄█▒▒████▄    
▒██▒ ▄██▒██  ▀█▄  ▓██ ░▄█ ▒▒██  ▀█▄  ▓███▄░▒██  ▀█▄  
▒██░█▀  ░██▄▄▄▄██ ▒██▀▀█▄  ░██▄▄▄▄██ ▓██ █▄░██▄▄▄▄██ 
░▓█  ▀█▓ ▓█   ▓██▒░██▓ ▒██▒ ▓█   ▓██▒▒██▒ █▄▓█   ▓██▒
░▒▓███▀▒ ▒▒   ▓▒█░░ ▒▓ ░▒▓░ ▒▒   ▓▒█░▒ ▒▒ ▓▒▒▒   ▓▒█░
▒░▒   ░   ▒   ▒▒ ░  ░▒ ░ ▒░  ▒   ▒▒ ░░ ░▒ ▒░ ▒   ▒▒ ░
 ░    ░   ░   ▒     ░░   ░   ░   ▒   ░ ░░ ░  ░   ▒   
 ░            ░  ░   ░           ░  ░░  ░        ░  ░
      ░                                              
	

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.

Contents

Install

go get github.com/xis/baraka/v2

Simple Usage

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

	store := baraka.NewFileSystemStore("./files")

	router := gin.Default()
	router.POST("/upload", func(c *gin.Context) {
		// parse
		request, err := parser.Parse(c.Request)
		if err != nil {
			fmt.Println(err)
		}

		// get the form
		images, err := request.GetForm("images")
		if err != nil {
			fmt.Println(err)
		}

		// save
		for key, image := range images {
			err = store.Save("images", "image_"+strconv.Itoa(key), image)
			if err != nil {
				fmt.Println(err)
			}
		}
	})
	router.Run()
}

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

Filtering Parts

You can filter parts by their properties, like part's content type. Parser can inspect the part's bytes and detect the type of the part with the Inspector.

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

// give parser an inspector
parser.SetInspector(baraka.NewDefaultInspector(512))
// give parser a filter
parser.SetFilter(baraka.NewExtensionFilter(".jpg"))

Now parser will inspect the each part and it will just return the jpeg ones from the Parse function. You can make your own Inspector and Filter.

More

Handling multipart file uploads simple in Go with Baraka

Contribute

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 DefaultInspector added in v2.0.2

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

DefaultInspector is the default inspector uses http.DetectContentType function to find the content type

func (*DefaultInspector) Inspect added in v2.0.2

func (inspector *DefaultInspector) Inspect(data []byte) string

Inspect finds the content type of the byte array

type ExtensionFilter added in v2.0.2

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

ExtensionFilter is a filter which filters the unwanted extensions passes the part if the part's extension is in the extensions field

func (*ExtensionFilter) Filter added in v2.0.2

func (f *ExtensionFilter) Filter(part *Part) bool

Filter function filters the part with it's extension

type FileSystemStore added in v2.0.2

type FileSystemStore struct {
	Path string
}

FileSystemStore is the default store to save parts

func NewFileSystemStore added in v2.0.2

func NewFileSystemStore(path string) FileSystemStore

NewFileSystemStore creates a new FileSystemStore

func (FileSystemStore) Save added in v2.0.2

func (s FileSystemStore) Save(path string, filename string, part *Part) error

Save is a method for saving parts into disk.

type Filter added in v2.0.2

type Filter interface {
	Filter(part *Part) bool
}

Filter is a interface which wraps the Filter function you can create your own filters with this

func NewExtensionFilter added in v2.0.2

func NewExtensionFilter(extensions ...string) Filter

NewExtensionFilter creates a new extension filter

type Inspector added in v2.0.2

type Inspector interface {
	Inspect(data []byte) string
}

Inspector is a interface for finding the content type of the byte array

func NewDefaultInspector added in v2.0.2

func NewDefaultInspector(maxSampleSize int) Inspector

NewDefaultInspector creates a new default inspector

type Parser

type Parser struct {
	Options   ParserOptions
	Filter    Filter
	Inspector Inspector
}

Parser contains parsing options and interfaces to do some other actions

func DefaultParser added in v2.0.2

func DefaultParser() *Parser

DefaultParser creates a new parser with the default settings

func NewParser

func NewParser(options ParserOptions) *Parser

NewParser creates a new Parser.

func (*Parser) Parse

func (parser *Parser) Parse(r *http.Request) (*Request, error)

Parse parses the http request with the multipart.Reader. reads parts inside the loop which iterates up to MaxParseCount at most. creates a []byte (buf) which gonna contain the part data.

func (*Parser) SetFilter added in v2.0.2

func (parser *Parser) SetFilter(filter Filter) *Parser

SetFilter sets the filter of the parser

func (*Parser) SetInspector added in v2.0.2

func (parser *Parser) SetInspector(inspector Inspector) *Parser

SetInspector sets the inspector of the parser

type ParserOptions

type ParserOptions struct {
	MaxFileSize   int
	MaxFileCount  int
	MaxParseCount int
}

ParserOptions contains parser's options about parsing.

type Part

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

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

func (*Part) GetHeader added in v2.0.2

func (p *Part) GetHeader(key string) string

GetHeader returns the value of the given header key

type Request

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

Request contains parts to use in other transactions.

func NewRequest

func NewRequest(parts map[string][]*Part) *Request

NewRequest creates a new Request

func (*Request) GetForm added in v2.0.2

func (r *Request) GetForm(formname string) ([]*Part, error)

GetForm returns the parts of the requested form, returns error if form does not exists

type Saver

type Saver interface {
	Save(path string, filename string, part *Part) 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