Git Product home page Git Product logo

entity-file-uploader's Introduction

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)

Handle file uploads over http for multipart formdata

err := fileManager.ReceiveMultiPartFormDataAndSaveToDir(r, "logo", fileModel.ID)

entity-file-uploader's People

Contributors

joegasewicz avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

entity-file-uploader's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.