Updated updateUser func
parent
ddbaac904c
commit
bf6aa2788a
|
@ -13,6 +13,7 @@ import (
|
||||||
"git.umbach.dev/app-idea/rest-api/modules/database"
|
"git.umbach.dev/app-idea/rest-api/modules/database"
|
||||||
"git.umbach.dev/app-idea/rest-api/modules/rabbitmq"
|
"git.umbach.dev/app-idea/rest-api/modules/rabbitmq"
|
||||||
"git.umbach.dev/app-idea/rest-api/modules/structs"
|
"git.umbach.dev/app-idea/rest-api/modules/structs"
|
||||||
|
"git.umbach.dev/app-idea/rest-api/modules/utils"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
@ -229,8 +230,6 @@ func GetUserBySessionId(sessionId string) (structs.User, error) {
|
||||||
return user, err
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infoln("getUserBySessionId", user)
|
|
||||||
|
|
||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -286,6 +285,59 @@ func GetUsers(c *fiber.Ctx) error {
|
||||||
return c.JSON(users)
|
return c.JSON(users)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func UpdateUser(c *fiber.Ctx) error {
|
||||||
|
file, err := c.FormFile("image")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Debugln("FormFile err", err)
|
||||||
|
return c.SendStatus(fiber.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes, err := utils.FormFileToBytes(file)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Debugln("formFileToBytes err", err)
|
||||||
|
return c.SendStatus(fiber.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info(len(bytes))
|
||||||
|
|
||||||
|
user, err := GetUserBySessionId(c.Cookies(config.Cfg.Settings.Cookies.SessionId))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return c.SendStatus(fiber.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
// update user avatar
|
||||||
|
filename, err := UpdateAvatar(c.Cookies(config.Cfg.Settings.Cookies.SessionId), user, bytes, file.Filename)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Debugln("savepic err", err)
|
||||||
|
return c.SendStatus(fiber.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(fiber.Map{"filename": filename})
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateAvatar(sessionId string, user structs.User, bytes []byte, filename string) (string, error) {
|
||||||
|
newFilename := strings.Replace("avatar"+filename, "-", "", -1)
|
||||||
|
|
||||||
|
rabbitmq.PublishPicture(structs.RabbitmqPictureMessage{Picture: bytes, UserId: user.Id, Filename: newFilename})
|
||||||
|
|
||||||
|
db := database.DB
|
||||||
|
|
||||||
|
res := db.Model(&structs.User{}).Where("id = ?", user.Id).Update("avatar_url", newFilename)
|
||||||
|
|
||||||
|
// TODO: delete old avatar from storage server
|
||||||
|
|
||||||
|
if res.Error != nil {
|
||||||
|
log.Warnln("Failed to insert user to db:", res.Error)
|
||||||
|
return "", res.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
return newFilename, nil
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
func GetUsers(c *fiber.Ctx) error {
|
func GetUsers(c *fiber.Ctx) error {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue