New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

github.com/dholtzmann/filevalidator

Package Overview
Dependencies
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/dholtzmann/filevalidator

Source
Go Modules
Version
v0.0.0-20171229015643-c539dd93d8ed
Version published
Created
Source

filevalidator

Go simple package for validating file uploads.

Installation

go get -u github.com/dholtzmann/filevalidator

Basic Example

package main

import (
	fv "github.com/dholtzmann/filevalidator"
	"html/template"
	"net/http"
)

func main() {
	// setup HTTP server...
}

func indexPage(w http.ResponseWriter, r *http.Request) {
	tpl := template.Must(template.ParseGlob("./templates/*.html"))

	err := r.ParseMultipartForm(1 << 20)
	if err != nil {
		panic(err.Error())
	}

	fields := map[string]*Field{
		"imageupload":   NewField(true, false, FileSize(100, 100000), MimeTypes([]string{"image/png", "image/jpeg"}), MinPixelSize(100, 100), MaxPixelSize(500, 500)),
		"fileupload":    NewField(true, true, FileSize(100, 100000), MimeTypes([]string{"application/x-gzip"})),
		"missingupload": NewField(false, true, nil),
	}

	err, validator := New(fields)
	if err != nil {
		panic(err.Error())
	}

	isValid, errors := validator.Validate(req.MultipartForm.File)
	if isValid {
		// save file...
	} else {
		// display errors
		tplVars := make(map[string]interface{})
		tplVars["FormErrors"] = errors
		err = tpl.ExecuteTemplate(w, "index.gohtml", tplVars)
	}
}

Validation rules

  • NewField(required bool, singleFile bool, rules []Rule)
  • FileSize(min, max uint64)
  • MimeTypes(list []string)
  • MinPixelSize(width, height uint32)
  • MaxPixelSize(width, height uint32)

See the file 'validate_test.go' for examples of how to use the validation rules.

FAQs

Package last updated on 29 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts