uploader

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2020 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Copyright 2019-2020 Axetroy. All rights reserved. MIT license.

Copyright 2019-2020 Axetroy. All rights reserved. MIT license.

Copyright 2019-2020 Axetroy. All rights reserved. MIT license.

Index

Constants

This section is empty.

Variables

View Source
var File = router.Handler(func(c router.Context) {
	var (
		isSupportFile bool
		maxUploadSize = config2.Upload.File.MaxSize   // 最大上传大小
		allowTypes    = config2.Upload.File.AllowType // 可上传的文件类型
		err           error
		data          = make([]schema.FileResponse, 0)
	)

	defer func() {
		c.JSON(err, data, nil)
	}()

	err = c.Request().ParseMultipartForm(maxUploadSize)

	if err != nil {
		return
	}

	form := c.Request().MultipartForm

	files := form.File["file"]

	if len(files) == 0 {
		err = exception.InvalidParams
		return
	}

	for _, file := range files {
		var (
			src  multipart.File // 要读取的文件
			dist *os.File       // 最终输出的文件
		)
		extname := path.Ext(file.Filename)

		{
			if len(allowTypes) != 0 {
				for i := 0; i < len(allowTypes); i++ {
					if allowTypes[i] == extname {
						isSupportFile = true
						break
					}
				}

				if !isSupportFile {
					err = exception.NotSupportType
					return
				}
			}

			if file.Size > int64(maxUploadSize) {
				err = exception.OutOfSize
				return
			}
		}

		if src, err = file.Open(); err != nil {
			return
		}

		hash := md5.New()

		if _, err = io.Copy(hash, src); err != nil {
			_ = src.Close()
			return
		} else {
			_ = src.Close()
		}

		md5string := hex.EncodeToString(hash.Sum([]byte("")))

		fileName := md5string + extname

		distPath := path.Join(config2.Upload.Path, config2.Upload.File.Path, fileName)

		if dist, err = os.Create(distPath); err != nil {
			return
		}

		if src, err = file.Open(); err != nil {
			return
		}

		if _, err = io.Copy(dist, src); err != nil {
			_ = src.Close()
			_ = dist.Close()
			return
		} else {
			_ = src.Close()
			_ = dist.Close()
		}

		res := schema.FileResponse{
			Hash:         md5string,
			Filename:     fileName,
			Origin:       file.Filename,
			Size:         file.Size,
			RawPath:      "/v1/resource/file/" + fileName,
			DownloadPath: "/v1/download/file/" + fileName,
		}

		data = append(data, res)

	}

})
View Source
var Image = router.Handler(func(c router.Context) {
	var (
		maxUploadSize = config.Upload.Image.MaxSize // 最大上传大小
		err           error
		data          = make([]ImageResponse, 0)
		imageDir      = path.Join(config.Upload.Path, config.Upload.Image.Path)
	)

	defer func() {
		c.JSON(err, data, nil)
	}()

	err = c.Request().ParseMultipartForm(maxUploadSize)

	if err != nil {
		return
	}

	form := c.Request().MultipartForm

	files := form.File["file"]

	if len(files) == 0 {
		err = exception.InvalidParams
		return
	}

	for _, file := range files {
		var (
			src  multipart.File // 要读取的文件
			dist *os.File       // 最终输出的文件
		)

		extname := strings.ToLower(path.Ext(file.Filename))

		{
			if !isImage(extname) {
				err = exception.NotSupportType
				return
			}

			if maxUploadSize > 0 && file.Size > int64(maxUploadSize) {
				err = exception.OutOfSize
				return
			}
		}

		if src, err = file.Open(); err != nil {
			return
		}

		hash := md5.New()

		if _, err = io.Copy(hash, src); err != nil {
			_ = src.Close()
			return
		} else {
			_ = src.Close()
		}

		md5string := hex.EncodeToString(hash.Sum([]byte("")))

		fileName := md5string + extname

		distPath := path.Join(imageDir, fileName)

		if dist, err = os.Create(distPath); err != nil {
			return
		}

		if src, err = file.Open(); err != nil {
			return
		}

		if _, err = io.Copy(dist, src); err != nil {
			_ = src.Close()
			_ = dist.Close()
			return
		} else {
			_ = src.Close()
			_ = dist.Close()
		}

		res := ImageResponse{
			FileResponse: schema.FileResponse{
				Hash:         md5string,
				Filename:     fileName,
				Origin:       file.Filename,
				Size:         file.Size,
				RawPath:      "/v1/resource/image/" + fileName,
				DownloadPath: "/v1/download/image/" + fileName,
			},
		}

		data = append(data, res)
	}
})

Functions

func Example

func Example(c iris.Context)

Types

type ImageResponse

type ImageResponse struct {
	schema.FileResponse
}

Jump to

Keyboard shortcuts

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