Avatar

This commit is contained in:
2024-02-01 00:14:25 +08:00
parent d4e437624a
commit afa55c2112
6 changed files with 121 additions and 30 deletions

35
pkg/server/avatar_api.go Normal file
View File

@ -0,0 +1,35 @@
package server
import (
"code.smartsheep.studio/hydrogen/passport/pkg/database"
"code.smartsheep.studio/hydrogen/passport/pkg/models"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/spf13/viper"
"path/filepath"
)
func getAvatar(c *fiber.Ctx) error {
id := c.Params("avatarId")
basepath := viper.GetString("content")
return c.SendFile(filepath.Join(basepath, id))
}
func setAvatar(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
file, err := c.FormFile("avatar")
if err != nil {
return err
}
user.Avatar = uuid.NewString()
if err := c.SaveFile(file, user.GetAvatarPath()); err != nil {
return err
} else {
database.C.Save(&user)
}
return c.SendStatus(fiber.StatusOK)
}

View File

@ -57,6 +57,9 @@ func NewServer() {
api := A.Group("/api").Name("API")
{
api.Get("/avatar/:avatarId", getAvatar)
api.Put("/avatar", auth, setAvatar)
api.Get("/users/me", auth, getUserinfo)
api.Put("/users/me", auth, editUserinfo)
api.Get("/users/me/events", auth, getEvents)