resource

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: 16 Imported by: 0

Documentation

Overview

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) {
	filename := c.Param("filename")
	filePath := path.Join(config2.Upload.Path, config2.Upload.File.Path, filename)
	if !fs.PathExists(filePath) {

		http.NotFound(c.Writer(), c.Request())
		return
	}
	http.ServeFile(c.Writer(), c.Request(), filePath)
})
View Source
var Image = router.Handler(func(c router.Context) {
	var (
		img      image.Image
		err      error
		filename = c.Param("filename")
		file     *os.File
		query    Query
	)
	originImagePath := path.Join(config.Upload.Path, config.Upload.Image.Path, filename)
	if !fs.PathExists(originImagePath) {

		http.NotFound(c.Writer(), c.Request())
		return
	}

	defer func() {
		if err != nil {
			http.Error(c.Writer(), err.Error(), http.StatusInternalServerError)
		}
	}()

	if err = c.ShouldBindQuery(&query); err != nil {
		return
	}

	if err = validator.ValidateStruct(query); err != nil {
		return
	}

	defer func() {
		if err != nil {
			http.ServeFile(c.Writer(), c.Request(), originImagePath)
		}
	}()

	if query.Scale != nil {
		extname := strings.ToLower(path.Ext(filename))

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

		defer func() {
			if err = file.Close(); err != nil {
				return
			}
		}()

		if img, err = DecodeImage(file); err != nil {
			return
		}

		c.Header("Content-Type", mime.TypeByExtension(extname))

		if err = EncodeImage(&img, file, c.Writer(), query); err != nil {
			return
		}

		return
	}

	if query.Width != nil || query.Height != nil {
		extname := strings.ToLower(path.Ext(filename))

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

		defer func() {
			if err = file.Close(); err != nil {
				return
			}
		}()

		if img, err = DecodeImage(file); err != nil {
			return
		}

		c.Header("Content-Type", mime.TypeByExtension(extname))

		if err = EncodeImage(&img, file, c.Writer(), query); err != nil {
			return
		}

		return
	}

	http.ServeFile(c.Writer(), c.Request(), originImagePath)
})

Functions

func DecodeImage added in v0.7.0

func DecodeImage(file *os.File) (img image.Image, err error)

将文件解析为 img

func EncodeImage added in v0.7.0

func EncodeImage(img *image.Image, file *os.File, writer io.Writer, option Query) (err error)

将图片写到流中

Types

type Query added in v0.7.0

type Query struct {
	Scale  *float64 `json:"scale" url:"scale" validate:"omitempty,gt=0,lt=1" comment:"缩放比例"` // 缩放比例
	Width  *int     `json:"width" url:"width" validate:"omitempty,gt=0" comment:"宽度"`        // 指定图片的宽度
	Height *int     `json:"height" url:"height" validate:"omitempty,gt=0" comment:"高度"`      // 指定图片的高度
}

Jump to

Keyboard shortcuts

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