MCPcopy Create free account
hub / github.com/gogs/gogs / UpdateAvatarSetting

Function UpdateAvatarSetting

internal/route/user/setting.go:121–165  ·  view source on GitHub ↗

FIXME: limit upload size

(c *context.Context, f form.Avatar, ctxUser *database.User)

Source from the content-addressed store, hash-verified

119
120// FIXME: limit upload size
121func UpdateAvatarSetting(c *context.Context, f form.Avatar, ctxUser *database.User) error {
122 if f.Source == form.AvatarLookup && f.Gravatar != "" {
123 avatar := cryptoutil.MD5(f.Gravatar)
124 err := database.Handle.Users().Update(
125 c.Req.Context(),
126 ctxUser.ID,
127 database.UpdateUserOptions{
128 Avatar: &avatar,
129 AvatarEmail: &f.Gravatar,
130 },
131 )
132 if err != nil {
133 return errors.Wrap(err, "update user")
134 }
135
136 err = database.Handle.Users().DeleteCustomAvatar(c.Req.Context(), c.User.ID)
137 if err != nil {
138 return errors.Wrap(err, "delete custom avatar")
139 }
140 return nil
141 }
142
143 if f.Avatar != nil && f.Avatar.Filename != "" {
144 r, err := f.Avatar.Open()
145 if err != nil {
146 return errors.Newf("open avatar reader: %v", err)
147 }
148 defer func() { _ = r.Close() }()
149
150 data, err := io.ReadAll(r)
151 if err != nil {
152 return errors.Newf("read avatar content: %v", err)
153 }
154 if !tool.IsImageFile(data) {
155 return errors.New(c.Tr("settings.uploaded_avatar_not_a_image"))
156 }
157
158 err = database.Handle.Users().UseCustomAvatar(c.Req.Context(), ctxUser.ID, data)
159 if err != nil {
160 return errors.Wrap(err, "save avatar")
161 }
162 return nil
163 }
164 return nil
165}
166
167func SettingsAvatar(c *context.Context) {
168 c.Title("settings.avatar")

Callers 2

SettingsAvatarFunction · 0.92
SettingsAvatarPostFunction · 0.70

Calls 7

MD5Function · 0.92
IsImageFileFunction · 0.92
UsersMethod · 0.80
DeleteCustomAvatarMethod · 0.80
TrMethod · 0.80
UseCustomAvatarMethod · 0.80
UpdateMethod · 0.65

Tested by

no test coverage detected