✨ Avatar
This commit is contained in:
35
pkg/server/avatar_api.go
Normal file
35
pkg/server/avatar_api.go
Normal 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)
|
||||
}
|
@ -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)
|
||||
|
Reference in New Issue
Block a user