entityfileuploader

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2023 License: MIT Imports: 7 Imported by: 1

README

Go GitHub license GitHub issues

Entity File Uploader

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

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

Examples

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)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetFileName

func GetFileName(r *http.Request, fileName string) (string, error)

func ImageTestHelper added in v0.0.8

func ImageTestHelper(t *testing.T) (*io.PipeReader, *multipart.Writer)

ImageTestHelper Adds an image form file to the handler

Types

type FileManager

type FileManager struct {
	*FileUpload
	TableName string
}

func (*FileManager) Delete

func (f *FileManager) Delete(fileName string, id uint) error

Delete Deletes the file from the entity file path

func (*FileManager) DeleteEntityByID

func (f *FileManager) DeleteEntityByID(id uint) error

DeleteEntityByID Deletes a single entity by a UUID

func (*FileManager) Get

func (f *FileManager) Get(fileName string, id uint) string

Get Gets the full file path including the filename

func (*FileManager) GetEntityDirPath

func (f *FileManager) GetEntityDirPath(id uint) string

GetEntityDirPath Gets the full entity file path only (does not include the filename)

func (*FileManager) GetEntityFilePath

func (f *FileManager) GetEntityFilePath(fullEntityDirPath string, fileName string) string

GetEntityFilePath Get the full path which is constructed of the following: upload path + entity name + plus the primary id + the filename e.g. `./assets/uploads/users/1/cats.jpg`

func (*FileManager) GetEntityURL added in v0.0.3

func (f *FileManager) GetEntityURL(fileName string, id uint) string

GetEntityURL GetEntityUrl gets the full url

func (*FileManager) ReceiveMultiPartFormDataAndSaveToDir added in v0.0.8

func (f *FileManager) ReceiveMultiPartFormDataAndSaveToDir(r *http.Request, field string, id uint) error

ReceiveMultiPartFormDataAndSaveToDir Handles a file uploaded via multipart form request over http The field argument is the form field's name

func (*FileManager) Update

func (f *FileManager) Update(fileName string, id uint, newFileName string) error

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

func (*FileManager) Upload

func (f *FileManager) Upload(w http.ResponseWriter, r *http.Request, id uint, formName string) (string, error)

Upload Uploads & stores the file on your server. If there is an error then the return value of string will be an empty string. If there are no errors to return the string will be the full path of with filename.

type FileUpload

type FileUpload struct {
	UploadDir string

	MaxFileSize uint8
	FileTypes   []string
	URL         string
	// contains filtered or unexported fields
}

func (*FileUpload) Init

func (f *FileUpload) Init(tableName string) (*FileManager, error)

Init is a factory function that returns a pointer to FileManager. tableName is the name of the entity associated with a database table name.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL