fuh

package module
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2017 License: MIT Imports: 8 Imported by: 0

README

fuh

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 (
	"context"
	"encoding/json"
	"net/http"
	"path/filepath"

	"github.com/LyricTian/fuh"
)

func main() {
	uploader := fuh.NewUploader(&fuh.Config{
		BasePath:  "attach",
		SizeLimit: 1 << 20,
	}, fuh.NewFileStore())

	http.HandleFunc("/fileupload", func(w http.ResponseWriter, r *http.Request) {

		ctx := fuh.NewFileNameContext(context.Background(), func(ci fuh.ContextInfo) string {
			return filepath.Join(ci.BasePath(), ci.FileName())
		})

		finfos, err := uploader.Upload(ctx, r, "file")
		if err != nil {
			w.WriteHeader(500)
			return
		}
		json.NewEncoder(w).Encode(finfos)
	})

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

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

Features

  • Custom file name
  • Custom file size limit
  • Support timeout handler
  • Supports storage extensions

MIT License

Copyright (c) 2016 LyricTian

Documentation

Index

Constants

This section is empty.

Variables

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

Functions

func NewFileNameContext

func NewFileNameContext(ctx context.Context, fn FileNameHandle) context.Context

NewFileNameContext returns a new Context that carries value fn.

func NewFileSizeLimitContext

func NewFileSizeLimitContext(ctx context.Context, fsl FileSizeLimitHandle) context.Context

NewFileSizeLimitContext returns a new Context that carries value fsl.

func SetConfig

func SetConfig(cfg *Config)

SetConfig set the configuration parameters

func SetStore

func SetStore(store Storer)

SetStore set storage

Types

type Config

type Config struct {
	BasePath  string
	SizeLimit int64
	MaxMemory int64
}

Config basic configuration

type ContextInfo

type ContextInfo interface {
	BasePath() string
	FileName() string
	FileSize() int64
	FileHeader() textproto.MIMEHeader
	Request() *http.Request
}

ContextInfo the context information

type FileInfo

type FileInfo interface {
	FullName() string
	Size() int64
}

FileInfo upload the basic information of the file

func Upload

func Upload(ctx context.Context, r *http.Request, key string) ([]FileInfo, error)

Upload file upload

type FileNameHandle

type FileNameHandle func(ci ContextInfo) string

FileNameHandle the file name

func FromFileNameContext

func FromFileNameContext(ctx context.Context) (FileNameHandle, bool)

FromFileNameContext returns the FileNameHandle value stored in ctx, if any.

type FileSizeLimitHandle

type FileSizeLimitHandle func(ci ContextInfo) bool

FileSizeLimitHandle file size limit

func FromFileSizeLimitContext

func FromFileSizeLimitContext(ctx context.Context) (FileSizeLimitHandle, bool)

FromFileSizeLimitContext returns the FileSizeLimitHandle value stored in ctx, if any.

type FileStore

type FileStore struct {
	// rewrite the existing file
	Rewrite bool
}

FileStore file storage

func (*FileStore) Store

func (f *FileStore) Store(ctx context.Context, filename string, data io.Reader, size int64) error

Store store data to a local file

type Storer

type Storer interface {
	Store(ctx context.Context, filename string, data io.Reader, size int64) error
}

Storer file storage interface

func NewFileStore

func NewFileStore() Storer

NewFileStore create a file store

type Uploader

type Uploader interface {
	Upload(ctx context.Context, r *http.Request, key string) ([]FileInfo, error)
}

Uploader file upload interface

func NewUploader

func NewUploader(cfg *Config, store Storer) Uploader

NewUploader create a file upload interface

Jump to

Keyboard shortcuts

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