|
|
@ -53,6 +53,21 @@ func GetImageSize(img image.Rectangle) (int, int) {
|
|
|
|
return width, height
|
|
|
|
return width, height
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func fileCheck(uploadType string, size int64) error {
|
|
|
|
|
|
|
|
if uploadType != "public/video" &&
|
|
|
|
|
|
|
|
uploadType != "public/image" &&
|
|
|
|
|
|
|
|
uploadType != "public/avatar" &&
|
|
|
|
|
|
|
|
uploadType != "attachment" {
|
|
|
|
|
|
|
|
return errcode.InvalidParams
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if size > 1024*1024*100 {
|
|
|
|
|
|
|
|
return errcode.FileInvalidSize.WithDetails("最大允许100MB")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func UploadAttachment(c *gin.Context) {
|
|
|
|
func UploadAttachment(c *gin.Context) {
|
|
|
|
response := app.NewResponse(c)
|
|
|
|
response := app.NewResponse(c)
|
|
|
|
svc := service.New(c)
|
|
|
|
svc := service.New(c)
|
|
|
@ -66,16 +81,9 @@ func UploadAttachment(c *gin.Context) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
|
|
if uploadType != "public/video" &&
|
|
|
|
if err = fileCheck(uploadType, fileHeader.Size); err != nil {
|
|
|
|
uploadType != "public/image" &&
|
|
|
|
cErr, _ := err.(*errcode.Error)
|
|
|
|
uploadType != "public/avatar" &&
|
|
|
|
response.ToErrorResponse(cErr)
|
|
|
|
uploadType != "attachment" {
|
|
|
|
|
|
|
|
response.ToErrorResponse(errcode.InvalidParams)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if fileHeader.Size > 1024*1024*100 {
|
|
|
|
|
|
|
|
response.ToErrorResponse(errcode.FileInvalidSize.WithDetails("最大允许100MB"))
|
|
|
|
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -129,20 +137,21 @@ func UploadAttachment(c *gin.Context) {
|
|
|
|
attachment.UserID = userID.(int64)
|
|
|
|
attachment.UserID = userID.(int64)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if uploadType == "public/image" || uploadType == "public/avatar" {
|
|
|
|
var uploadAttachmentTypeMap = map[string]model.AttachmentType{
|
|
|
|
attachment.Type = model.ATTACHMENT_TYPE_IMAGE
|
|
|
|
"public/image": model.ATTACHMENT_TYPE_IMAGE,
|
|
|
|
|
|
|
|
"public/avatar": model.ATTACHMENT_TYPE_IMAGE,
|
|
|
|
|
|
|
|
"public/video": model.ATTACHMENT_TYPE_VIDEO,
|
|
|
|
|
|
|
|
"attachment": model.ATTACHMENT_TYPE_OTHER,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
src, err := imaging.Decode(file)
|
|
|
|
attachment.Type = uploadAttachmentTypeMap[uploadType]
|
|
|
|
|
|
|
|
if attachment.Type == model.ATTACHMENT_TYPE_IMAGE {
|
|
|
|
|
|
|
|
var src image.Image
|
|
|
|
|
|
|
|
src, err = imaging.Decode(file)
|
|
|
|
if err == nil {
|
|
|
|
if err == nil {
|
|
|
|
attachment.ImgWidth, attachment.ImgHeight = GetImageSize(src.Bounds())
|
|
|
|
attachment.ImgWidth, attachment.ImgHeight = GetImageSize(src.Bounds())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if uploadType == "public/video" {
|
|
|
|
|
|
|
|
attachment.Type = model.ATTACHMENT_TYPE_VIDEO
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if uploadType == "attachment" {
|
|
|
|
|
|
|
|
attachment.Type = model.ATTACHMENT_TYPE_OTHER
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
attachment, err = svc.CreateAttachment(attachment)
|
|
|
|
attachment, err = svc.CreateAttachment(attachment)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|