♻️ Use paperclip to store avatar and more
This commit is contained in:
@ -38,7 +38,7 @@ func getUserinfo(c *fiber.Ctx) error {
|
||||
resp["preferred_username"] = data.Nick
|
||||
|
||||
if len(data.Avatar) > 0 {
|
||||
resp["picture"] = fmt.Sprintf("https://%s/api/avatar/%s", viper.GetString("domain"), data.Avatar)
|
||||
resp["picture"] = fmt.Sprintf("%s/api/attachments/%s", viper.GetString("paperclip.endpoint"), data.Avatar)
|
||||
}
|
||||
|
||||
return c.JSON(resp)
|
||||
|
@ -1,50 +1,34 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"context"
|
||||
"fmt"
|
||||
pcpb "git.solsynth.dev/hydrogen/paperclip/pkg/grpc/proto"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/database"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/grpc"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/models"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
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
|
||||
|
||||
var data struct {
|
||||
AttachmentID string `json:"attachment"`
|
||||
}
|
||||
|
||||
var previous string
|
||||
if len(user.Avatar) > 0 {
|
||||
previous = user.GetAvatarPath()
|
||||
if _, err := grpc.Attachments.CheckAttachmentExists(context.Background(), &pcpb.AttachmentLookupRequest{
|
||||
Uuid: &data.AttachmentID,
|
||||
Usage: lo.ToPtr("p.avatar"),
|
||||
}); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("avatar was not found in repository: %v", err))
|
||||
}
|
||||
|
||||
user.Avatar = uuid.NewString()
|
||||
user.Avatar = data.AttachmentID
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
if err := database.C.Save(&user).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
@ -52,31 +36,21 @@ func setAvatar(c *fiber.Ctx) error {
|
||||
|
||||
func setBanner(c *fiber.Ctx) error {
|
||||
user := c.Locals("principal").(models.Account)
|
||||
file, err := c.FormFile("banner")
|
||||
if err != nil {
|
||||
return err
|
||||
var data struct {
|
||||
AttachmentID string `json:"attachment"`
|
||||
}
|
||||
|
||||
var previous string
|
||||
if len(user.Banner) > 0 {
|
||||
previous = user.GetBannerPath()
|
||||
if _, err := grpc.Attachments.CheckAttachmentExists(context.Background(), &pcpb.AttachmentLookupRequest{
|
||||
Uuid: &data.AttachmentID,
|
||||
Usage: lo.ToPtr("p.banner"),
|
||||
}); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("banner was not found in repository: %v", err))
|
||||
}
|
||||
|
||||
user.Banner = uuid.NewString()
|
||||
user.Banner = data.AttachmentID
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
if err := database.C.Save(&user).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
|
@ -66,8 +66,6 @@ func NewServer() {
|
||||
|
||||
api := A.Group("/api").Name("API")
|
||||
{
|
||||
api.Get("/avatar/:avatarId", getAvatar)
|
||||
|
||||
notify := api.Group("/notifications").Name("Notifications API")
|
||||
{
|
||||
notify.Get("/", authMiddleware, getNotifications)
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"github.com/gomarkdown/markdown"
|
||||
"github.com/gomarkdown/markdown/html"
|
||||
"github.com/gomarkdown/markdown/parser"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/sujit-baniya/flash"
|
||||
"html/template"
|
||||
"time"
|
||||
@ -44,5 +45,7 @@ func selfUserinfoPage(c *fiber.Ctx) error {
|
||||
"birthday_at": birthday,
|
||||
"personal_page": template.HTML(markdown.Render(doc, renderer)),
|
||||
"userinfo": data,
|
||||
"avatar": fmt.Sprintf("%s/api/attachments/%s", viper.GetString("paperclip.endpoint"), data.Avatar),
|
||||
"banner": fmt.Sprintf("%s/api/attachments/%s", viper.GetString("paperclip.endpoint"), data.Banner),
|
||||
}, "views/layouts/user-center")
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"github.com/gomarkdown/markdown"
|
||||
"github.com/gomarkdown/markdown/html"
|
||||
"github.com/gomarkdown/markdown/parser"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/sujit-baniya/flash"
|
||||
"html/template"
|
||||
"time"
|
||||
@ -44,5 +45,7 @@ func otherUserinfoPage(c *fiber.Ctx) error {
|
||||
"birthday_at": birthday,
|
||||
"personal_page": template.HTML(markdown.Render(doc, renderer)),
|
||||
"userinfo": data,
|
||||
"avatar": fmt.Sprintf("%s/api/attachments/%s", viper.GetString("paperclip.endpoint"), data.Avatar),
|
||||
"banner": fmt.Sprintf("%s/api/attachments/%s", viper.GetString("paperclip.endpoint"), data.Banner),
|
||||
}, "views/layouts/user-center")
|
||||
}
|
||||
|
Reference in New Issue
Block a user