Banner

This commit is contained in:
2024-03-20 20:56:07 +08:00
parent fdc252abc3
commit 1bd0807d65
9 changed files with 196 additions and 79 deletions

View File

@ -1,12 +1,14 @@
package server
import (
"os"
"path/filepath"
"code.smartsheep.studio/hydrogen/identity/pkg/database"
"code.smartsheep.studio/hydrogen/identity/pkg/models"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/spf13/viper"
"path/filepath"
)
func getAvatar(c *fiber.Ctx) error {
@ -23,12 +25,58 @@ func setAvatar(c *fiber.Ctx) error {
return err
}
var previous string
if len(user.Avatar) > 0 {
previous = user.GetAvatarPath()
}
user.Avatar = uuid.NewString()
if err := c.SaveFile(file, user.GetAvatarPath()); err != nil {
return err
} else {
database.C.Save(&user)
// Clean up
if len(previous) > 0 {
basepath := viper.GetString("content")
filepath := filepath.Join(basepath, previous)
if info, err := os.Stat(filepath); err == nil && !info.IsDir() {
os.Remove(filepath)
}
}
}
return c.SendStatus(fiber.StatusOK)
}
func setBanner(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
file, err := c.FormFile("banner")
if err != nil {
return err
}
var previous string
if len(user.Banner) > 0 {
previous = user.GetBannerPath()
}
user.Banner = uuid.NewString()
if err := c.SaveFile(file, user.GetBannerPath()); err != nil {
return err
} else {
database.C.Save(&user)
// Clean up
if len(previous) > 0 {
basepath := viper.GetString("content")
filepath := filepath.Join(basepath, previous)
if info, err := os.Stat(filepath); err == nil && !info.IsDir() {
os.Remove(filepath)
}
}
}
return c.SendStatus(fiber.StatusOK)

View File

@ -1,6 +1,10 @@
package server
import (
"net/http"
"strings"
"time"
"code.smartsheep.studio/hydrogen/identity/pkg/views"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cache"
@ -11,9 +15,6 @@ import (
jsoniter "github.com/json-iterator/go"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"net/http"
"strings"
"time"
)
var A *fiber.App
@ -58,21 +59,28 @@ func NewServer() {
api := A.Group("/api").Name("API")
{
api.Get("/avatar/:avatarId", getAvatar)
api.Put("/avatar", authMiddleware, setAvatar)
api.Get("/notifications", authMiddleware, getNotifications)
api.Put("/notifications/:notificationId/read", authMiddleware, markNotificationRead)
api.Post("/notifications/subscribe", authMiddleware, addNotifySubscriber)
api.Get("/users/me", authMiddleware, getUserinfo)
api.Put("/users/me", authMiddleware, editUserinfo)
api.Get("/users/me/events", authMiddleware, getEvents)
api.Get("/users/me/challenges", authMiddleware, getChallenges)
api.Get("/users/me/sessions", authMiddleware, getSessions)
api.Delete("/users/me/sessions/:sessionId", authMiddleware, killSession)
me := api.Group("/users/me").Name("Myself Operations")
{
me.Put("/avatar", authMiddleware, setAvatar)
me.Put("/banner", authMiddleware, setBanner)
me.Get("/", authMiddleware, getUserinfo)
me.Put("/", authMiddleware, editUserinfo)
me.Get("/events", authMiddleware, getEvents)
me.Get("/challenges", authMiddleware, getChallenges)
me.Get("/sessions", authMiddleware, getSessions)
me.Delete("/sessions/:sessionId", authMiddleware, killSession)
me.Post("/confirm", doRegisterConfirm)
}
api.Post("/users", doRegister)
api.Post("/users/me/confirm", doRegisterConfirm)
api.Put("/auth", startChallenge)
api.Post("/auth", doChallenge)