-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Fix user avatar name #8547
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
66cead0
Fix user avatar name
masudur-rahman e958c75
Add migration renameExistingUserAvatarName
masudur-rahman bb14679
Use custom User table for migration
masudur-rahman 7947e9c
Update user avatar name
masudur-rahman e39cf68
Commit session before every os.Error and also interval commit session
masudur-rahman 819db1c
Process updating user in batches
masudur-rahman d096b9b
Move copying avatar part to a function
masudur-rahman 717add4
Rollback session before returning error
masudur-rahman ee97b69
Generate newAvatar from the avatar file
masudur-rahman 5d39aab
Merge branch 'master' into fix-avatar-name
masudur-rahman f87ddd2
Cope with partial rerun of migration
zeripath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package migrations | ||
|
||
import ( | ||
"crypto/md5" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/setting" | ||
|
||
"xorm.io/xorm" | ||
) | ||
|
||
func renameExistingUserAvatarName(x *xorm.Engine) error { | ||
sess := x.NewSession() | ||
defer sess.Close() | ||
|
||
type User struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
LowerName string `xorm:"UNIQUE NOT NULL"` | ||
Avatar string | ||
} | ||
deleteList := make(map[string]struct{}) | ||
start := 0 | ||
for { | ||
if err := sess.Begin(); err != nil { | ||
return fmt.Errorf("session.Begin: %v", err) | ||
} | ||
users := make([]*User, 0, 50) | ||
if err := sess.Table("user").Asc("id").Limit(50, start).Find(&users); err != nil { | ||
return fmt.Errorf("select users from id [%d]: %v", start, err) | ||
} | ||
if len(users) == 0 { | ||
_ = sess.Rollback() | ||
break | ||
} | ||
|
||
log.Info("select users [%d - %d]", start, start+len(users)) | ||
start += 50 | ||
|
||
for _, user := range users { | ||
oldAvatar := user.Avatar | ||
|
||
if _, err := os.Stat(filepath.Join(setting.AvatarUploadPath, oldAvatar)); err != nil { | ||
log.Warn("[user: %s] os.Stat: %v", user.LowerName, err) | ||
// avatar doesn't exist in the storage | ||
// no need to move avatar and update database | ||
// we can just skip this | ||
continue | ||
} | ||
|
||
newAvatar, err := copyOldAvatarToNewLocation(user.ID, oldAvatar) | ||
if err != nil { | ||
_ = sess.Rollback() | ||
return fmt.Errorf("[user: %s] %v", user.LowerName, err) | ||
} | ||
|
||
user.Avatar = newAvatar | ||
if _, err := sess.ID(user.ID).Cols("avatar").Update(user); err != nil { | ||
_ = sess.Rollback() | ||
return fmt.Errorf("[user: %s] user table update: %v", user.LowerName, err) | ||
} | ||
|
||
deleteList[filepath.Join(setting.AvatarUploadPath, oldAvatar)] = struct{}{} | ||
zeripath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
if err := sess.Commit(); err != nil { | ||
_ = sess.Rollback() | ||
return fmt.Errorf("commit session: %v", err) | ||
} | ||
} | ||
|
||
for file := range deleteList { | ||
if err := os.Remove(file); err != nil { | ||
log.Warn("os.Remove: %v", err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// copyOldAvatarToNewLocation copies oldAvatar to newAvatarLocation | ||
// and returns newAvatar location | ||
func copyOldAvatarToNewLocation(userID int64, oldAvatar string) (string, error) { | ||
fr, err := os.Open(filepath.Join(setting.AvatarUploadPath, oldAvatar)) | ||
if err != nil { | ||
return "", fmt.Errorf("os.Open: %v", err) | ||
} | ||
defer fr.Close() | ||
masudur-rahman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
data, err := ioutil.ReadAll(fr) | ||
if err != nil { | ||
return "", fmt.Errorf("ioutil.ReadAll: %v", err) | ||
} | ||
|
||
newAvatar := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%d-%x", userID, md5.Sum(data))))) | ||
|
||
zeripath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err := ioutil.WriteFile(filepath.Join(setting.AvatarUploadPath, newAvatar), data, 0666); err != nil { | ||
return "", fmt.Errorf("ioutil.WriteFile: %v", err) | ||
} | ||
|
||
return newAvatar, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.