diff --git a/server/auth.go b/server/auth.go index 60dbfd56..88517d2d 100644 --- a/server/auth.go +++ b/server/auth.go @@ -45,7 +45,6 @@ func (s *Server) registerAuthRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user response").SetInternal(err) } - return nil }) @@ -113,7 +112,6 @@ func (s *Server) registerAuthRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode created user response").SetInternal(err) } - return nil }) } diff --git a/server/basic_auth.go b/server/basic_auth.go index 64841a98..3a8370da 100644 --- a/server/basic_auth.go +++ b/server/basic_auth.go @@ -32,7 +32,6 @@ func setUserSession(c echo.Context, user *api.User) error { if err != nil { return fmt.Errorf("failed to set session, err: %w", err) } - return nil } @@ -48,7 +47,6 @@ func removeUserSession(c echo.Context) error { if err != nil { return fmt.Errorf("failed to set session, err: %w", err) } - return nil } diff --git a/server/memo.go b/server/memo.go index c7bc71fd..28896da4 100644 --- a/server/memo.go +++ b/server/memo.go @@ -30,7 +30,6 @@ func (s *Server) registerMemoRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err) } - return nil }) @@ -56,7 +55,6 @@ func (s *Server) registerMemoRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err) } - return nil }) @@ -96,7 +94,6 @@ func (s *Server) registerMemoRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(list)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo list response").SetInternal(err) } - return nil }) @@ -135,7 +132,6 @@ func (s *Server) registerMemoRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err) } - return nil }) @@ -161,7 +157,6 @@ func (s *Server) registerMemoRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err) } - return nil }) @@ -181,7 +176,6 @@ func (s *Server) registerMemoRoutes(g *echo.Group) { } c.JSON(http.StatusOK, true) - return nil }) @@ -202,7 +196,6 @@ func (s *Server) registerMemoRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(len(memoList))); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo amount").SetInternal(err) } - return nil }) } diff --git a/server/resource.go b/server/resource.go index f65c7556..6a708d06 100644 --- a/server/resource.go +++ b/server/resource.go @@ -56,7 +56,6 @@ func (s *Server) registerResourceRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(resource)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode resource response").SetInternal(err) } - return nil }) @@ -74,7 +73,51 @@ func (s *Server) registerResourceRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(list)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode resource list response").SetInternal(err) } + return nil + }) + g.GET("/resource/:resourceId", func(c echo.Context) error { + resourceID, err := strconv.Atoi(c.Param("resourceId")) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("resourceId"))).SetInternal(err) + } + + userID := c.Get(getUserIDContextKey()).(int) + resourceFind := &api.ResourceFind{ + ID: &resourceID, + CreatorID: &userID, + } + resource, err := s.Store.FindResource(resourceFind) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource").SetInternal(err) + } + + c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) + if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(resource)); err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode resource response").SetInternal(err) + } + return nil + }) + + g.GET("/resource/:resourceId/blob", func(c echo.Context) error { + resourceID, err := strconv.Atoi(c.Param("resourceId")) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("resourceId"))).SetInternal(err) + } + + userID := c.Get(getUserIDContextKey()).(int) + resourceFind := &api.ResourceFind{ + ID: &resourceID, + CreatorID: &userID, + } + resource, err := s.Store.FindResource(resourceFind) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource").SetInternal(err) + } + + c.Response().Writer.WriteHeader(http.StatusOK) + c.Response().Writer.Header().Set("Content-Type", resource.Type) + c.Response().Writer.Write(resource.Blob) return nil }) @@ -92,7 +135,6 @@ func (s *Server) registerResourceRoutes(g *echo.Group) { } c.JSON(http.StatusOK, true) - return nil }) } diff --git a/server/shortcut.go b/server/shortcut.go index c1dd66b2..dd1c9122 100644 --- a/server/shortcut.go +++ b/server/shortcut.go @@ -29,7 +29,6 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(shortcut)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode shortcut response").SetInternal(err) } - return nil }) @@ -55,7 +54,6 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(shortcut)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode shortcut response").SetInternal(err) } - return nil }) @@ -73,7 +71,6 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(list)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode shortcut list response").SetInternal(err) } - return nil }) @@ -95,7 +92,6 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(shortcut)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode shortcut response").SetInternal(err) } - return nil }) @@ -113,7 +109,6 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) { } c.JSON(http.StatusOK, true) - return nil }) } diff --git a/server/system.go b/server/system.go index 58058cbe..24fc16c7 100644 --- a/server/system.go +++ b/server/system.go @@ -10,11 +10,12 @@ import ( func (s *Server) registerSystemRoutes(g *echo.Group) { g.GET("/ping", func(c echo.Context) error { + data := s.Profile + c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) - if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(s.Profile)); err != nil { + if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(data)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose system profile").SetInternal(err) } - return nil }) @@ -28,6 +29,9 @@ func (s *Server) registerSystemRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find owner user").SetInternal(err) } + // data desensitize + ownerUser.OpenID = "" + systemStatus := api.SystemStatus{ Owner: ownerUser, Profile: s.Profile, @@ -37,7 +41,6 @@ func (s *Server) registerSystemRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(systemStatus)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode system status response").SetInternal(err) } - return nil }) } diff --git a/server/tag.go b/server/tag.go index 205445cf..c6a94fab 100644 --- a/server/tag.go +++ b/server/tag.go @@ -47,7 +47,6 @@ func (s *Server) registerTagRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(tagList)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode tags response").SetInternal(err) } - return nil }) } diff --git a/server/user.go b/server/user.go index 3bacf4e7..14cda7bd 100644 --- a/server/user.go +++ b/server/user.go @@ -34,7 +34,6 @@ func (s *Server) registerUserRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user response").SetInternal(err) } - return nil }) @@ -48,7 +47,6 @@ func (s *Server) registerUserRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(userList)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user list response").SetInternal(err) } - return nil }) @@ -72,7 +70,6 @@ func (s *Server) registerUserRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user response").SetInternal(err) } - return nil }) @@ -109,7 +106,6 @@ func (s *Server) registerUserRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user response").SetInternal(err) } - return nil }) @@ -158,7 +154,6 @@ func (s *Server) registerUserRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user response").SetInternal(err) } - return nil }) } diff --git a/server/webhook.go b/server/webhook.go index ee9bbf3b..5b5661c4 100644 --- a/server/webhook.go +++ b/server/webhook.go @@ -45,7 +45,6 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err) } - return nil }) @@ -83,7 +82,6 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err) } - return nil }) @@ -133,7 +131,6 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(list)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo list response").SetInternal(err) } - return nil }) @@ -190,7 +187,6 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(resource)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode resource response").SetInternal(err) } - return nil }) @@ -201,12 +197,10 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) { } filename := c.Param("filename") - resourceFind := &api.ResourceFind{ ID: &resourceID, Filename: &filename, } - resource, err := s.Store.FindResource(resourceFind) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch resource ID: %v", resourceID)).SetInternal(err)