fuh

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2017 License: MIT Imports: 10 Imported by: 0

README

File upload handler

License ReportCard Build Coverage GoDoc

Quick Start

Download and install
$ go get github.com/LyricTian/fuh
Create file server.go
package main

import (
	"encoding/json"
	"net/http"

	"github.com/LyricTian/fuh"
)

func main() {
	http.HandleFunc("/fileupload", func(w http.ResponseWriter, r *http.Request) {
		finfo, err := fuh.Upload(r, "file", nil, nil)
		if err != nil {
			w.WriteHeader(http.StatusBadRequest)
			return
		}
		json.NewEncoder(w).Encode(finfo)
	})

	http.ListenAndServe(":8080", nil)
}

Build and run
$ go build server.go
$ ./server

Features

  • Supports single or multiple uploads
  • Custom file name
  • Custom file size limit

Test

$ go test -v

MIT License

Copyright (c) 2016 LyricTian

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMissingFile no such file
	ErrMissingFile = errors.New("no such file")
	// ErrFileTooLarge file too large
	ErrFileTooLarge = errors.New("file too large")
	// ErrFileExists file already exists
	ErrFileExists = errors.New("file already exists")
)

Functions

func SetConfig

func SetConfig(cfg *UploadConfig)

SetConfig set upload the configuration parameters

func SetStore

func SetStore(store Storer)

SetStore set upload data storage interface

Types

type FileInfo

type FileInfo struct {
	// file name
	Name string
	// file size
	Size int64
	// file the full path
	Path string
	// file extension
	Ext string
}

FileInfo file information

func Upload

func Upload(r *http.Request, key string, fnh FileNameHandle, fsh FileSizeHandle) (*FileInfo, error)

Upload upload a single file

func UploadMulti

func UploadMulti(r *http.Request, key string, fnh FileNameHandle, fsh FileSizeHandle) ([]*FileInfo, error)

UploadMulti upload multiple files

func UploadReader

func UploadReader(r io.Reader, fnh FileNameHandle, fsh FileSizeHandle) (*FileInfo, error)

UploadReader upload data stream file

type FileNameHandle

type FileNameHandle func(base, filename string) string

FileNameHandle filename handling

type FileSizeHandle

type FileSizeHandle func(size int64) bool

FileSizeHandle file size limit handing

type FileStoreConfig

type FileStoreConfig struct {
	// rewrite the existing files
	Rewrite bool
}

FileStoreConfig file store configuration parameters

type Storer

type Storer interface {
	// the file data storage
	Store(filename string, r io.Reader, size int64) error
}

Storer upload data storage interface

func NewFileStore

func NewFileStore(cfgs ...*FileStoreConfig) Storer

NewFileStore create a file store

type UploadConfig

type UploadConfig struct {
	// the base path of storing files
	BasePath string
	// file size limit(the default is not limit)
	SizeLimit int64
	// The whole request body is parsed and up to a total of maxMemory bytes of
	// its file parts are stored in memory, with the remainder stored on
	// disk in temporary files(the default of 32M).
	MaxMemory int64
}

UploadConfig upload the configuration parameters

type Uploader

type Uploader interface {
	// upload a single file
	Upload(r *http.Request, key string, fnh FileNameHandle, fsh FileSizeHandle) (*FileInfo, error)
	// upload multiple files
	UploadMulti(r *http.Request, key string, fnh FileNameHandle, fsh FileSizeHandle) ([]*FileInfo, error)
	// upload data stream file
	UploadReader(r io.Reader, fnh FileNameHandle, fsh FileSizeHandle) (*FileInfo, error)
}

Uploader file upload interface

func NewUploader

func NewUploader(store Storer, cfgs ...*UploadConfig) Uploader

NewUploader create a file uploader

Jump to

Keyboard shortcuts

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