You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
4.5 KiB
113 lines
4.5 KiB
package interfaces
|
|
|
|
import (
|
|
"fm-upload/internal/data/model"
|
|
response "fm-upload/internal/data/model"
|
|
"fm-upload/internal/data/model/request"
|
|
"fm-upload/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type UploadHTTPServer interface {
|
|
Ping(*gin.Context)
|
|
Upload(*gin.Context, *request.UploadRequest) (*request.UploadResponse, error)
|
|
ImagesController(*gin.Context, *request.CompressImagesRequest) (*request.ImagesCompressResponse, error)
|
|
ImageController(*gin.Context, *request.CompressImageRequest) (*model.ImageCompressResponse, error)
|
|
}
|
|
|
|
// @Summary 上传文件
|
|
// @Description 上传文件,类型:image/video/doc/avatar
|
|
// @Tags 华为云/上传
|
|
// @Param file formData file true "上传文件"
|
|
// @Param body formData request.UploadRequest true "form表单其他参数"
|
|
// @Produce json
|
|
// @Success 200 {object} request.UploadResponse "非video类型文件:没有转码/截图相关字段"
|
|
// @Failure 210 {string} string "请求参数错误 <br/> {"message":{"desc":"input format error"}}"
|
|
// @Failure 211 {string} string "文件大小超过上限 <br/> {"message":{"desc":"file too large"}}"
|
|
// @Failure 423 {string} string "转码参数错误 <br/> {"message":{"desc":"转码参数格式错误"}}"
|
|
// @Failure 424 {string} string "华为云返回error <br/> {"message":{"desc":"七牛云返回错误","error":...}}"
|
|
// @Router /hw/upload [post]
|
|
func Upload(srv UploadHTTPServer) func(ctx *gin.Context) {
|
|
return func(ctx *gin.Context) {
|
|
var in request.UploadRequest
|
|
if err := ctx.ShouldBind(&in); err != nil {
|
|
response.FailWithMessage("input format error", ctx)
|
|
return
|
|
}
|
|
if utils.LimitFileType(in.File.Filename) {
|
|
response.FailWithMessage("file type forbid", ctx)
|
|
return
|
|
}
|
|
resp, err := srv.Upload(ctx, &in)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), ctx)
|
|
return
|
|
}
|
|
response.OkWithData(resp, ctx)
|
|
}
|
|
}
|
|
|
|
// @Summary 单图片批量压缩多个格式
|
|
// @Description 单张图片批量压缩多个格式
|
|
// @Tags 华为云/文件处理
|
|
// @Param file formData file false "上传文件"
|
|
// @Param body body model.CompressImagesRequest true "压缩参数"
|
|
// @Produce json
|
|
// @Success 200 {object} model.ImagesCompressResponse "图片压缩结果"
|
|
// @Failure 210 {string} string "请求参数错误 <br/> {"message":{"desc":"input format error"}}"
|
|
// @Failure 423 {string} string "访问原文件url出错 <br/> {"message":{"desc":"访问url出错"}}"
|
|
// @Failure 425 {string} string "文件处理过程中出错 <br/> {"message":{"desc":"image process error","error":...}}"
|
|
// @Failure 426 {string} string "原文件上传至华为云出错 <br/> {"message":{"desc":"error upload to qiniuyun","error":...}}"
|
|
// @Router /hw/images_compress [post]
|
|
func ImagesController(srv UploadHTTPServer) func(ctx *gin.Context) {
|
|
return func(ctx *gin.Context) {
|
|
var in request.CompressImagesRequest
|
|
if err := ctx.ShouldBind(&in); err != nil {
|
|
response.FailWithMessage("input format error", ctx)
|
|
return
|
|
}
|
|
if in.ImageUrl == "" && in.File == nil {
|
|
response.FailWithMessage("imageUrl/file empty", ctx)
|
|
return
|
|
}
|
|
resp, err := srv.ImagesController(ctx, &in)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), ctx)
|
|
return
|
|
}
|
|
response.OkWithData(resp, ctx)
|
|
}
|
|
}
|
|
|
|
// @Summary 图片压缩
|
|
// @Description 单张图片压缩
|
|
// @Tags 华为云/文件处理
|
|
// @Param file formData file false "上传文件"
|
|
// @Param body formData model.CompressImageRequest true "压缩参数"
|
|
// @Produce json
|
|
// @Success 200 {object} model.ImageCompressResponse "图片压缩结果"
|
|
// @Failure 210 {string} string "请求参数错误 <br/> {"message":{"desc":"input format error"}}"
|
|
// @Failure 423 {string} string "访问原文件url出错 <br/> {"message":{"desc":"访问url出错"}}"
|
|
// @Failure 425 {string} string "文件处理过程中出错 <br/> {"message":{"desc":"image process error","error":...}}"
|
|
// @Failure 426 {string} string "原文件上传至七牛云出错 <br/> {"message":{"desc":"error upload to qiniuyun","error":...}}"
|
|
// @Router /hw/image_compress [post]
|
|
func ImageController(srv UploadHTTPServer) func(ctx *gin.Context) {
|
|
return func(ctx *gin.Context) {
|
|
var in request.CompressImageRequest
|
|
if err := ctx.ShouldBind(&in); err != nil {
|
|
response.FailWithMessage("input format error", ctx)
|
|
return
|
|
}
|
|
if in.ImageUrl == "" && in.File == nil {
|
|
response.FailWithMessage("imageUrl/file empty", ctx)
|
|
return
|
|
}
|
|
resp, err := srv.ImageController(ctx, &in)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), ctx)
|
|
return
|
|
}
|
|
response.OkWithData(resp, ctx)
|
|
}
|
|
}
|
|
|