//upload user avatar func UploadUserAvatarToQiniu(r io.ReadSeeker, filename string, mime string, bucketName string, user *models.User) error { var ext string // test image mime type switch mime { case "image/jpeg": ext = ".jpg" case "image/png": ext = ".png" case "image/gif": ext = ".gif" default: ext = filepath.Ext(filename) switch ext { case ".jpg", ".png", ".gif": default: return fmt.Errorf("unsupport image format `%s`", filename) } } // decode image var err error switch ext { case ".jpg": _, err = jpeg.Decode(r) case ".png": _, err = png.Decode(r) case ".gif": _, err = gif.Decode(r) } if err != nil { return err } //reset reader pointer if _, err := r.Seek(0, 0); err != nil { return err } var data []byte if data, err = ioutil.ReadAll(r); err != nil { return err } if len(data) > setting.AvatarImageMaxLength { return fmt.Errorf("avatar image size too large", filename) } //reset reader pointer again if _, err := r.Seek(0, 0); err != nil { return err } //save to qiniu var uptoken = utils.GetQiniuUptoken(bucketName) var putRet qio.PutRet var putExtra = &qio.PutExtra{ MimeType: mime, } err = qio.PutWithoutKey(nil, &putRet, uptoken, r, putExtra) if err != nil { return err } //update user user.AvatarKey = putRet.Key if err := user.Update("AvatarKey", "Updated"); err != nil { return err } return nil }