Removed old user avatar url field from the db

This commit is contained in:
Asaf Gartner 2022-02-13 21:52:29 +02:00
parent 6445567840
commit d32cd0a849
4 changed files with 49 additions and 111 deletions

View File

@ -495,109 +495,5 @@ func init() {
}
adminCommand.AddCommand(uploadProjectLogos)
uploadUserAvatars := &cobra.Command{
Use: "uploaduseravatars",
Short: "Uploads avatar imagefiles to S3 and replaces them with assets",
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
conn := db.NewConnPool(1, 1)
defer conn.Close()
type userQuery struct {
User models.User `db:"auth_user"`
}
allUsers, err := db.Query(ctx, conn, userQuery{}, `SELECT $columns FROM auth_user LEFT JOIN handmade_asset AS auth_user_avatar ON auth_user_avatar.id = auth_user.avatar_asset_id`)
if err != nil {
panic(oops.New(err, "Failed to fetch projects from db"))
}
var fixupUsers []*models.User
numImages := 0
for _, user := range allUsers {
u := &user.(*userQuery).User
if u.Avatar != nil && *u.Avatar != "" {
fixupUsers = append(fixupUsers, u)
numImages += 1
}
}
fmt.Printf("%d images to upload\n", numImages)
uploadImage := func(ctx context.Context, conn db.ConnOrTx, filepath string, owner *models.User) *models.Asset {
filepath = "./public/media/" + filepath
contents, err := os.ReadFile(filepath)
if err != nil {
panic(oops.New(err, fmt.Sprintf("Failed to read file: %s", filepath)))
}
width := 0
height := 0
mime := ""
fileExtensionOverrides := []string{".svg"}
fileExt := strings.ToLower(path.Ext(filepath))
tryDecode := true
for _, ext := range fileExtensionOverrides {
if fileExt == ext {
tryDecode = false
}
}
if tryDecode {
config, _, err := image.DecodeConfig(bytes.NewReader(contents))
if err != nil {
panic(oops.New(err, fmt.Sprintf("Failed to decode file: %s", filepath)))
}
width = config.Width
height = config.Height
mime = http.DetectContentType(contents)
} else {
if fileExt == ".svg" {
mime = "image/svg+xml"
}
}
filename := path.Base(filepath)
asset, err := assets.Create(ctx, conn, assets.CreateInput{
Content: contents,
Filename: filename,
ContentType: mime,
UploaderID: &owner.ID,
Width: width,
Height: height,
})
if err != nil {
panic(oops.New(err, "Failed to create asset"))
}
return asset
}
for _, u := range fixupUsers {
if u.Avatar != nil && *u.Avatar != "" {
avatarAsset := uploadImage(ctx, conn, *u.Avatar, u)
_, err := conn.Exec(ctx,
`
UPDATE auth_user
SET
avatar_asset_id = $2,
avatar = NULL
WHERE
id = $1
`,
u.ID,
avatarAsset.ID,
)
if err != nil {
panic(oops.New(err, "Failed to update user"))
}
numImages -= 1
fmt.Printf(".")
}
}
fmt.Printf("\nDone! %d images not patched for some reason.\n\n", numImages)
},
}
adminCommand.AddCommand(uploadUserAvatars)
addProjectCommands(adminCommand)
}

View File

@ -0,0 +1,47 @@
package migrations
import (
"context"
"time"
"git.handmade.network/hmn/hmn/src/migration/types"
"github.com/jackc/pgx/v4"
)
func init() {
registerMigration(RemoveUserAvatarUrl{})
}
type RemoveUserAvatarUrl struct{}
func (m RemoveUserAvatarUrl) Version() types.MigrationVersion {
return types.MigrationVersion(time.Date(2022, 2, 13, 13, 26, 53, 0, time.UTC))
}
func (m RemoveUserAvatarUrl) Name() string {
return "RemoveUserAvatarUrl"
}
func (m RemoveUserAvatarUrl) Description() string {
return "Remove avatar url field from users as we're using assets now"
}
func (m RemoveUserAvatarUrl) Up(ctx context.Context, tx pgx.Tx) error {
_, err := tx.Exec(ctx,
`
ALTER TABLE auth_user
DROP COLUMN avatar;
`,
)
return err
}
func (m RemoveUserAvatarUrl) Down(ctx context.Context, tx pgx.Tx) error {
_, err := tx.Exec(ctx,
`
ALTER TABLE auth_user
ADD COLUMN avatar character varying(100);
`,
)
return err
}

View File

@ -35,7 +35,6 @@ type User struct {
Bio string `db:"bio"`
Blurb string `db:"blurb"`
Signature string `db:"signature"`
Avatar *string `db:"avatar"`
AvatarAssetID *uuid.UUID `db:"avatar_asset_id"`
AvatarAsset *Asset `db:"avatar"`

View File

@ -173,12 +173,8 @@ func UserAvatarUrl(u *models.User, currentTheme string) string {
currentTheme = "light"
}
avatar := ""
if u != nil && (u.AvatarAsset != nil || (u.Avatar != nil && len(*u.Avatar) > 0)) {
if u.AvatarAsset != nil {
if u != nil && u.AvatarAsset != nil {
avatar = hmnurl.BuildS3Asset(u.AvatarAsset.S3Key)
} else {
avatar = hmnurl.BuildUserFile(*u.Avatar)
}
} else {
avatar = UserAvatarDefaultUrl(currentTheme)
}