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 ¶
Types ¶
type ImageResponse ¶
type ImageResponse struct {
schema.FileResponse
}
Click to show internal directories.
Click to hide internal directories.