šŸ“š baraka - Awesome Go Library for File Handling

Go Gopher mascot for baraka

A library to process http file uploads easily.

šŸ·ļø File Handling
šŸ“‚ Libraries for handling files and file systems.
ā­ 55 stars
View on GitHub šŸ”—

Detailed Description of 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.

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.NewFilesystemStorage("./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.

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