Skip to content

Fix user avatar name #8547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Dec 27, 2019
Prev Previous commit
Next Next commit
Update user avatar name
  • Loading branch information
masudur-rahman committed Dec 26, 2019
commit 7947e9c29d418de88aa70ac23da99a995296922c
51 changes: 24 additions & 27 deletions models/migrations/v115.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
package migrations

import (
"crypto/md5"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"

"code.gitea.io/gitea/modules/setting"

Expand All @@ -36,37 +35,35 @@ func renameExistingUserAvatarName(x *xorm.Engine) error {

deleteList := make([]string, 0, len(users))
for _, user := range users {
if !strings.HasPrefix(user.Avatar, strconv.FormatInt(user.ID, 10)+"-") {
oldAvatar := user.Avatar
newAvatar := fmt.Sprintf("%d-%s", user.ID, user.Avatar)
oldAvatar := user.Avatar
newAvatar := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%d-%s", user.ID, user.Avatar))))

if _, err := os.Stat(filepath.Join(setting.AvatarUploadPath, oldAvatar)); err != nil {
continue
}

fr, err := os.Open(filepath.Join(setting.AvatarUploadPath, oldAvatar))
if err != nil {
return err
}
defer fr.Close()
if _, err := os.Stat(filepath.Join(setting.AvatarUploadPath, oldAvatar)); err != nil {
continue
}

fw, err := os.Create(filepath.Join(setting.AvatarUploadPath, newAvatar))
if err != nil {
return err
}
defer fw.Close()
fr, err := os.Open(filepath.Join(setting.AvatarUploadPath, oldAvatar))
if err != nil {
return err
}
defer fr.Close()

if _, err := io.Copy(fw, fr); err != nil {
return err
}
fw, err := os.Create(filepath.Join(setting.AvatarUploadPath, newAvatar))
if err != nil {
return err
}
defer fw.Close()

user.Avatar = newAvatar
if _, err := sess.ID(user.ID).Update(&user); err != nil {
return err
}
if _, err := io.Copy(fw, fr); err != nil {
return err
}

deleteList = append(deleteList, filepath.Join(setting.AvatarUploadPath, oldAvatar))
user.Avatar = newAvatar
if _, err := sess.ID(user.ID).Update(&user); err != nil {
return err
}

deleteList = append(deleteList, filepath.Join(setting.AvatarUploadPath, oldAvatar))
}
for _, file := range deleteList {
if err := os.Remove(file); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ func (u *User) UploadAvatar(data []byte) error {
// If we prefix it with u.ID, it will be separated
// Otherwise, if any of the users delete his avatar
// Other users will lose their avatars too.
u.Avatar = fmt.Sprintf("%d-%x", u.ID, md5.Sum(data))
u.Avatar = fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%d-%x", u.ID, md5.Sum(data)))))
if err = updateUser(sess, u); err != nil {
return fmt.Errorf("updateUser: %v", err)
}
Expand Down