Entity File Uploader

Handles file uploads & organises files based on your database entities.
Read the docs.


go get -u github.com/joegasewicz/entity-file-uploader


Create a new File Manager Entity

import (
	entityfileuploader "github.com/joegasewicz/entity-file-uploader"

var FileUpload = entityfileuploader.FileUpload{
	UploadDir:   "uploads",
	MaxFileSize: 10,
	FileTypes:   []string{"png", "jpeg"},
	URL: "http://localhost:8080",

Create a FileManager

A FileManager is specific to your database table name

catUpload, err := FileUpload.Init("cats")

Upload a file

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

Fetch the file's filepath

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

Update a file

Updates only the filename not the file (Use Upload to update the file)

err := CatUpload.Update(Cat.Avatar, Cat.ID, "tomcat.png")

Delete a file

// Deletes the file from the entity file path

err := CatUpload.Delete(Cat.Avatar, Cat.ID)