Handles file uploads & organises files based on your database entities. Read the docs.
go get -u github.com/joegasewicz/entity-file-uploader
import (
entityfileuploader "github.com/joegasewicz/entity-file-uploader"
)
var FileUpload = entityfileuploader.FileUpload{
UploadDir: "uploads",
MaxFileSize: 10,
FileTypes: []string{"png", "jpeg"},
URL: "http://localhost:8080",
}
A FileManager is specific to your database table name
catUpload, err := FileUpload.Init("cats")
We can now use our FileManager to save a file to /uploads/users/1/cats/catpic.png
// Cat.ID is the primary key value & Cat is a Gorm / ORM struct
// and we are uploading a file with a name of `catpic.jpg`
func Post(w http.ResponseWriter, r *http.Request) {
// GetFileName util function takes the form name of your file upload as the 2nd arg
avatarFileName, _ := entityfileuploader.GetFileName(r, "catImage")
// Example uses Gorm >>
Cat := models.Cat{
Avatar: avatarFileName,
}
result := DB.Create(&Cat)
// Gorm <<
// Upload method takes the UUID of your saved & created entity
fileName, err := CatUpload.Upload(w, r, Cat.ID, Cat.Avatar)
if err == nil {
// Handle error
}
fmt.Printf("Saved new file to: %s\n", fileName) // /uploads/users/1/cats/catpic.png
}
Gets the full file path including the filename
fileName := CatUpload.Get(Cat.Avatar, Cat.ID)
fmt.Println(fileName) // http://localhost:8080/uploads/cats/1/catpic.png
Updates only the filename not the file (Use Upload
to update the file)
err := CatUpload.Update(Cat.Avatar, Cat.ID, "tomcat.png")
// Deletes the file from the entity file path
err := CatUpload.Delete(Cat.Avatar, Cat.ID)
err := fileManager.ReceiveMultiPartFormDataAndSaveToDir(r, "logo", fileModel.ID)