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) }() maxSize := c.Application().ConfigurationReadOnly().GetPostMaxMemory() err = c.Request().ParseMultipartForm(maxSize) if err != nil { return } form := c.Request().MultipartForm files := form.File["file"] 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 == false { 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 = config2.Upload.Image.MaxSize // 最大上传大小 err error data = make([]ImageResponse, 0) imageDir = path.Join(config2.Upload.Path, config2.Upload.Image.Path) ) defer func() { c.JSON(err, data, nil) }() maxSize := c.Application().ConfigurationReadOnly().GetPostMaxMemory() err = c.Request().ParseMultipartForm(maxSize) if err != nil { return } form := c.Request().MultipartForm files := form.File["file"] for _, file := range files { var ( src multipart.File // 要读取的文件 dist *os.File // 最终输出的文件 ) extname := strings.ToLower(path.Ext(file.Filename)) { if isImage(extname) == false { 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, }, Thumbnail: false, } if _, er := GenerateThumbnail(distPath); er == nil { res.Thumbnail = true res.ThumbnailPath = "/v1/resource/thumbnail/" + fileName } data = append(data, res) } })
Functions ¶
func GenerateThumbnail ¶
* Generate thumbnail
Types ¶
type ImageResponse ¶
type ImageResponse struct { schema.FileResponse Thumbnail bool `json:"thumbnail"` // 是否拥有缩略图 ThumbnailPath string `json:"thumbnail_path"` // 缩略图的路径 }
Click to show internal directories.
Click to hide internal directories.