本文整理匯總了Golang中github.com/beego/wetalk/modules/models.User.AvatarKey方法的典型用法代碼示例。如果您正苦於以下問題:Golang User.AvatarKey方法的具體用法?Golang User.AvatarKey怎麽用?Golang User.AvatarKey使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/beego/wetalk/modules/models.User
的用法示例。
在下文中一共展示了User.AvatarKey方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: UploadUserAvatarToQiniu
//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
}