uploader

package module
v0.0.0-...-727c504 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2017 License: Apache-2.0 Imports: 18 Imported by: 2

README

go文件上传模块

完整实例

  • 文件上传
  • 限制上传文件的后缀名
  • 限制上传文件的大小
  • Hash去重,防止重复上传
  • 图片自动生成缩略图
  • 全由配置

如何运行

go get -v github.com/axetroy/gin-uploader
package main

import (
	"github.com/gin-gonic/gin"
	"github.com/axetroy/gin-uploader"
)

func main() {
	Router := gin.Default()

	uploader.Resolve(Router, uploader.TConfig{
		Path: "upload",
		UrlPrefix: "/api/v1",
		File: uploader.FileConfig{
			Path:      "files",
			MaxSize:   10485760,
			AllowType: []string{},
		},
		Image: uploader.ImageConfig{
			Path:    "images",
			MaxSize: 10485760,
			Thumbnail: uploader.ThumbnailConfig{
				Path:      "thumbnail",
				MaxWidth:  300,
				MaxHeight: 300,
			},
		},
	})

	if err := Router.Run("localhost:9090"); err != nil {
		panic(err)
		return
	}

})

API


# 上传相关
[POST]  /upload/image                           # 图片上传
[POST]  /upload/file                            # 其他文件上传
[GET]   /upload/example                         # 上传demo,仅在开发模式下
# 下载相关
[GET]   /download/image/origin/:filename        # 获取上传的原始图片
[GET]   /download/image/thumbnail/:filename     # 获取上传的缩略图片
[GET]   /download/file/download/:filename       # 下载文件
[GET]   /download/file/raw/:filename            # 获取文件

License

FOSSA Status

Documentation

Index

Constants

View Source
const FIELD = "file"

Variables

This section is empty.

Functions

func DownloadFile

func DownloadFile(context *gin.Context)

* Download a file

func GetFileRaw

func GetFileRaw(context *gin.Context)

* Get file raw

func GetOriginImage

func GetOriginImage(context *gin.Context)

* Get Origin image

func GetThumbnailImage

func GetThumbnailImage(context *gin.Context)

* Get thumbnail image

func InitUploader

func InitUploader(c *TConfig) (err error)

func UploadFile

func UploadFile(context *gin.Context)

* Upload file handler

func UploaderImage

func UploaderImage(context *gin.Context)

* Upload image handler

func UploaderTemplate

func UploaderTemplate(template string) func(context *gin.Context)

* Generate Upload example Template

Types

type FileConfig

type FileConfig struct {
	Path      string   `valid:"required,length(1|20)"`  // 普通文件的存放目录
	MaxSize   int      `valid:"required"`               // 普通文件上传的限制大小,单位byte, 最大单位1GB
	AllowType []string `valid:"required,length(0|100)"` // 允许上传的文件后缀名
}

type ImageConfig

type ImageConfig struct {
	Path      string `valid:"required,length(1|20)"` // 图片存储路径
	MaxSize   int    `valid:"required"`              // 最大图片上传限制,单位byte
	Thumbnail ThumbnailConfig
}

type TConfig

type TConfig struct {
	Path      string `valid:"required,length(1|20)"` //文件上传的根目录
	UrlPrefix string `valid:"required,length(0|20)"` // api的url前缀
	File      FileConfig
	Image     ImageConfig
}
var Config TConfig

type ThumbnailConfig

type ThumbnailConfig struct {
	Path      string `valid:"required,length(1|20)"` // 缩略图存放路径
	MaxWidth  int    `valid:"required"`              // 缩略图最大宽度
	MaxHeight int    `valid:"required"`              // 缩略图最大高度
}

type Uploader

type Uploader struct {
	Upload   *gin.RouterGroup
	Download *gin.RouterGroup
	Config   TConfig
	Engine   *gin.Engine
}

func New

func New(e *gin.Engine, c TConfig) (u *Uploader, err error)

* Create Router

func (*Uploader) Resolve

func (u *Uploader) Resolve()

* Resolve

Jump to

Keyboard shortcuts

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