▄▄▄▄ ▄▄▄ ██▀███ ▄▄▄ ██ ▄█▀▄▄▄ ▓█████▄ ▒████▄ ▓██ ▒ ██▒▒████▄ ██▄█▒▒████▄ ▒██▒ ▄██▒██ ▀█▄ ▓██ ░▄█ ▒▒██ ▀█▄ ▓███▄░▒██ ▀█▄ ▒██░█▀ ░██▄▄▄▄██ ▒██▀▀█▄ ░██▄▄▄▄██ ▓██ █▄░██▄▄▄▄██ ░▓█ ▀█▓ ▓█ ▓██▒░██▓ ▒██▒ ▓█ ▓██▒▒██▒ █▄▓█ ▓██▒ ░▒▓███▀▒ ▒▒ ▓▒█░░ ▒▓ ░▒▓░ ▒▒ ▓▒█░▒ ▒▒ ▓▒▒▒ ▓▒█░ ▒░▒ ░ ▒ ▒▒ ░ ░▒ ░ ▒░ ▒ ▒▒ ░░ ░▒ ▒░ ▒ ▒▒ ░ ░ ░ ░ ▒ ░░ ░ ░ ▒ ░ ░░ ░ ░ ▒ ░ ░ ░ ░ ░ ░░ ░ ░ ░ ░
a tool for handling file uploads for http servers
makes it easier to make operations with files from the http request.
go get github.com/xis/baraka/v2funcmain() {
// create a parserparser:=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) {
// parserequest, err:=parser.Parse(c.Request)
iferr!=nil {
fmt.Println(err)
}
// get the formimages, err:=request.GetForm("images")
iferr!=nil {
fmt.Println(err)
}
// saveforkey, image:=rangeimages {
err=store.Save("images", "image_"+strconv.Itoa(key), image)
iferr!=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.
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 parserparser:=baraka.NewParser(baraka.ParserOptions{
MaxFileSize: 5<<20,
MaxFileCount: 5,
MaxParseCount: 5,
})
// give parser an inspectorparser.SetInspector(baraka.NewDefaultInspector(512))
// give parser a filterparser.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.
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.