Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

v20.go 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package migrations
  5. import (
  6. "crypto/md5"
  7. "errors"
  8. "fmt"
  9. "io/ioutil"
  10. "os"
  11. "path/filepath"
  12. "strconv"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. "github.com/go-xorm/xorm"
  16. )
  17. func useNewNameAvatars(x *xorm.Engine) error {
  18. d, err := os.Open(setting.AvatarUploadPath)
  19. if err != nil {
  20. if os.IsNotExist(err) {
  21. // Nothing to do if AvatarUploadPath does not exist
  22. return nil
  23. }
  24. return err
  25. }
  26. names, err := d.Readdirnames(0)
  27. if err != nil {
  28. return err
  29. }
  30. type User struct {
  31. ID int64 `xorm:"pk autoincr"`
  32. Avatar string
  33. UseCustomAvatar bool
  34. }
  35. for _, name := range names {
  36. userID, err := strconv.ParseInt(name, 10, 64)
  37. if err != nil {
  38. log.Warn("ignore avatar %s rename: %v", name, err)
  39. continue
  40. }
  41. var user User
  42. if has, err := x.ID(userID).Get(&user); err != nil {
  43. return err
  44. } else if !has {
  45. return errors.New("Avatar user is not exist")
  46. }
  47. fPath := filepath.Join(setting.AvatarUploadPath, name)
  48. bs, err := ioutil.ReadFile(fPath)
  49. if err != nil {
  50. return err
  51. }
  52. user.Avatar = fmt.Sprintf("%x", md5.Sum(bs))
  53. err = os.Rename(fPath, filepath.Join(setting.AvatarUploadPath, user.Avatar))
  54. if err != nil {
  55. return err
  56. }
  57. _, err = x.ID(userID).Cols("avatar").Update(&user)
  58. if err != nil {
  59. return err
  60. }
  61. }
  62. return nil
  63. }