LyricTian / fuh

A file upload library for Go.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

A file upload library for Go.

License:MIT License


Languages

Language:Go 100.0%