diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 076fae6..7536419 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,11 +4,24 @@ - - + + + + + - + + + + + + + + + + + @@ -46,7 +59,7 @@ 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) diff --git a/pkg/server/avatar_api.go b/pkg/server/avatar_api.go index f189aeb..6cb6c53 100644 --- a/pkg/server/avatar_api.go +++ b/pkg/server/avatar_api.go @@ -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) diff --git a/pkg/server/startup.go b/pkg/server/startup.go index d119d19..48d8076 100644 --- a/pkg/server/startup.go +++ b/pkg/server/startup.go @@ -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) diff --git a/pkg/server/ui/accounts.go b/pkg/server/ui/accounts.go index cd22cfd..3368f20 100644 --- a/pkg/server/ui/accounts.go +++ b/pkg/server/ui/accounts.go @@ -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") } diff --git a/pkg/server/ui/directory.go b/pkg/server/ui/directory.go index 4f9e0c2..da241c6 100644 --- a/pkg/server/ui/directory.go +++ b/pkg/server/ui/directory.go @@ -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") } diff --git a/pkg/services/cleaner.go b/pkg/services/cleaner.go index 55550a0..1f398f0 100644 --- a/pkg/services/cleaner.go +++ b/pkg/services/cleaner.go @@ -11,7 +11,7 @@ func DoAutoDatabaseCleanup() { log.Debug().Time("deadline", deadline).Msg("Now cleaning up entire database...") var count int64 - for _, model := range database.DatabaseAutoActionRange { + for _, model := range database.AutoMaintainRange { tx := database.C.Unscoped().Delete(model, "deleted_at >= ?", deadline) if tx.Error != nil { log.Error().Err(tx.Error).Msg("An error occurred when running auth context cleanup...") diff --git a/pkg/views/users/directory/userinfo.gohtml b/pkg/views/users/directory/userinfo.gohtml index d7bf038..000f478 100644 --- a/pkg/views/users/directory/userinfo.gohtml +++ b/pkg/views/users/directory/userinfo.gohtml @@ -5,13 +5,13 @@ {{if gt (len .userinfo.Banner) 0}} - + {{end}} {{if gt (len .userinfo.Avatar) 0}} - + {{else}} account_circle diff --git a/pkg/views/users/me.gohtml b/pkg/views/users/me.gohtml index f9285fc..7aea606 100644 --- a/pkg/views/users/me.gohtml +++ b/pkg/views/users/me.gohtml @@ -5,13 +5,13 @@ {{if gt (len .userinfo.Banner) 0}} - + {{end}} {{if gt (len .userinfo.Avatar) 0}} - + {{else}} account_circle diff --git a/pkg/views/users/personalize.gohtml b/pkg/views/users/personalize.gohtml index b52c439..34fd4b2 100644 --- a/pkg/views/users/personalize.gohtml +++ b/pkg/views/users/personalize.gohtml @@ -9,33 +9,7 @@ - - Edit Avatar - account_circle - - - - - Edit Banner - background_replace - - + We doesn't support edit avatar / banner through Hydrogen.Passport web yet. Go try our Solian App! @@ -133,35 +107,4 @@ font-size: 20px; margin-bottom: 2px; } - - - \ No newline at end of file + \ No newline at end of file diff --git a/settings.toml b/settings.toml index c395627..b79c854 100644 --- a/settings.toml +++ b/settings.toml @@ -6,14 +6,16 @@ grpc_bind = "0.0.0.0:7444" domain = "localhost" secret = "LtTjzAGFLshwXhN4ZD4nG5KlMv1MWcsvfv03TSZYnT1VhiAnLIZFTnHUwR0XhGgi" -content = "uploads" - use_registration_magic_token = false [debug] database = false print_routes = false +[paperclip] +endpoint = "http://localhost:8443" +grpc_endpoint = "localhost:7443" + [external.firebase] credentials = "dist/firebase-certs.json"