Giter VIP home page Giter VIP logo

filer's Introduction

Filer - Utils for dealing with file uploads and validation in Golang

Coverage StatusBuild Status

$ go get github.com/adelowo/filer
  • Validators : Validate files according to a set of rules

    • SizeValidator

    Validate a file by it's size

    max, _ := filer.LengthInBytes("2MB")
    min, _ := filer.LengthInBytes("200KB")
    
    val = NewSizeValidator(max, min)
    file, _ = os.Open("./path/to/file")
    
    isValid, err := val.Validate(file)
    • MimeTypeValidator

    Validate a file by it's mimetype.

    val = NewMimeTypeValidator([]string{"image/jpeg", "image/png"})
    file, _ = os.Open("./path/to/file")
    
    isValid, err := val.Validate(file)
    • ExtensionValidator

    Validate a file by it's extension.. Caveat, this is probably not what you need.

    val = NewExtensionValidator([]string{"go", "php", "md", "rb", "ts"})
    file, _ = os.Open("./path/to/file")
    
    isValid, err := val.Validate(file)

You can also make use of a chained validator to ease the pain of having to deal with multiple validators manually

validator := NewChainedValidator(
  NewExtensionValidator([]string{"go", "ts", "jpg"}),
  NewSizeValidator((1024*1024), (1024*6))) //1MB and 6 KB

file, _ := os.Open("./path/to/file.jpg")

isValid, err := validator.Validate(file)
  • File Name Generators:

    • MD5Generator :

    Get the ms5 hash sum of the file name

    gen := generator.NewMD5Generator()
    s := gen.Generate(file) //Assuming file is `os.Open("file")`
    • SlugGenerator :

    Generates a slugified version of the file name.

    gen := generator.NewSlugGenerator()
    s := gen.Generate(file)
    • RandomGenerator :

    This generates a random name for the file.. Discards the file name itself

    ran := generator.NewRandomGenerator(12) //random name with a length of 12
    generatedName := ran.Generate(file)
  • File Storage:

For storage, filer utilizes Afero. This is to allow making use of a single API while having multiple backends to choose from

fs = afero.NewMemMapFs() //Using an inmemory store here..
//Could be a local store,s3 store or anything.. As long as it implements `afero.Fs`
storeAdapter = storage.NewFilerStorage(fs, nil) //can also pass in a PathFunc instead of nil

A sample usage of this library would be :

package main

import (
	"io"
	"io/ioutil"
	"net/http"

	"github.com/adelowo/filer/storage"
	"github.com/adelowo/filer/validator"
	"github.com/spf13/afero"
)

func main() {
	http.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "GET" {
			io.WriteString(w, "You came here")
			return
		}

		if r.Method == "POST" {
			file, header, err := r.FormFile("formNameInHTMLform")

			if err != nil {
				io.WriteString(w, err.Error())
				return
			}

			buf, err := ioutil.ReadAll(file)

			if err != nil {
				io.WriteString(w, err.Error())
				return
			}

			//Move to a temporary location
			//This is allow us be able to get details like it's size and others

			f, err := ioutil.TempFile("", header.Filename)

			if err != nil {
				panic("An error occurred while trying to create a temporary file")
			}

			val = validator.NewMimeTypeValidator([]string{"image/jpeg", "image/png"})
			val2 = validator.NewSizeValidator((1024 * 1024 * 2), (200 * 1024)) //2MB(maxSize) and 200KB(minSize)

			if _, err := val.Validate(f); err != nil {
				panic("Validation failed")
			}
			if _, err := val2.Validate(f); err != nil {
				panic("Validation failed")
			}


			//Upload to some place
			fs = afero.NewMemMapFs() //Using an inmemory store here..
			storeAdapter = storage.NewFilerStorage(fs, nil)

			if err := storeAdapter.Write("some/path", f); err != nil {
				panic("An error occurred while writing the file")
			}

			io.WriteString(w, "The upload was successful")
      return
		}
	})
}

License

MIT

filer's People

Contributors

adelowo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

mybigman

filer's Issues

File storage

The validators are good enough now and the next point of action is to add file storage...

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.