fuh

package module
v2.1.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2019 License: MIT Imports: 9 Imported by: 0

README

Golang File Upload Handler

Build Codecov ReportCard GoDoc License

Quick Start

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

import (
	"context"
	"encoding/json"
	"net/http"
	"path/filepath"

	"github.com/LyricTian/fuh"
)

func main() {
	upl := 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 := upl.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
  • Supports storage extensions
  • Context support

MIT License

Copyright (c) 2017 Lyric

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 NewContextInfoContext

func NewContextInfoContext(ctx context.Context, ci ContextInfo) context.Context

NewContextInfoContext returns a new Context that context information ci.

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

func FromContextInfoContext

func FromContextInfoContext(ctx context.Context) (ContextInfo, bool)

FromContextInfoContext returns the ContextInfo value stored in ctx, if any.

type FileInfo

type FileInfo interface {
	FullName() string
	Name() 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
	BasePath string
}

FileStore file storage

func NewFileStore

func NewFileStore() *FileStore

NewFileStore create a file store

func NewFileStoreWithBasePath

func NewFileStoreWithBasePath(basePath string) *FileStore

NewFileStoreWithBasePath create a file store with base path

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

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