2022-02-03 15:32:03 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-02-04 16:51:48 +08:00
|
|
|
"encoding/json"
|
2022-02-03 15:32:03 +08:00
|
|
|
"fmt"
|
2022-08-20 11:36:24 +08:00
|
|
|
"io"
|
2022-02-03 15:32:03 +08:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2022-06-27 22:09:06 +08:00
|
|
|
"github.com/usememos/memos/api"
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) registerResourceRoutes(g *echo.Group) {
|
|
|
|
g.POST("/resource", func(c echo.Context) error {
|
2022-08-07 10:17:12 +08:00
|
|
|
ctx := c.Request().Context()
|
2022-07-28 20:09:25 +08:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
2022-02-03 15:32:03 +08:00
|
|
|
|
2022-05-15 22:54:21 +08:00
|
|
|
err := c.Request().ParseMultipartForm(64 << 20)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Upload file overload max size").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := c.FormFile("file")
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Upload file not found").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := file.Filename
|
|
|
|
filetype := file.Header.Get("Content-Type")
|
|
|
|
size := file.Size
|
|
|
|
src, err := file.Open()
|
|
|
|
if err != nil {
|
2022-02-04 18:54:24 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to open file").SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
defer src.Close()
|
|
|
|
|
2022-08-20 11:36:24 +08:00
|
|
|
fileBytes, err := io.ReadAll(src)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
2022-02-04 18:54:24 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to read file").SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
resourceCreate := &api.ResourceCreate{
|
|
|
|
Filename: filename,
|
|
|
|
Type: filetype,
|
|
|
|
Size: size,
|
2022-02-04 18:54:24 +08:00
|
|
|
Blob: fileBytes,
|
2022-05-03 02:05:43 +08:00
|
|
|
CreatorID: userID,
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
resource, err := s.Store.CreateResource(ctx, resourceCreate)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create resource").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-02-04 17:06:04 +08:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(resource)); err != nil {
|
2022-05-03 02:05:43 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode resource response").SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-18 22:21:10 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
g.GET("/resource", func(c echo.Context) error {
|
2022-08-07 10:17:12 +08:00
|
|
|
ctx := c.Request().Context()
|
2022-07-28 20:09:25 +08:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
2022-02-03 15:32:03 +08:00
|
|
|
resourceFind := &api.ResourceFind{
|
2022-05-03 02:05:43 +08:00
|
|
|
CreatorID: &userID,
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-08-07 10:17:12 +08:00
|
|
|
list, err := s.Store.FindResourceList(ctx, resourceFind)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource list").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-02-04 17:06:04 +08:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(list)); err != nil {
|
2022-02-05 11:43:25 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode resource list response").SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-06-22 19:16:31 +08:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
g.GET("/resource/:resourceId", func(c echo.Context) error {
|
2022-08-07 10:17:12 +08:00
|
|
|
ctx := c.Request().Context()
|
2022-06-22 19:16:31 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-07-28 20:09:25 +08:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
2022-06-22 19:16:31 +08:00
|
|
|
resourceFind := &api.ResourceFind{
|
|
|
|
ID: &resourceID,
|
|
|
|
CreatorID: &userID,
|
|
|
|
}
|
2022-08-07 10:17:12 +08:00
|
|
|
resource, err := s.Store.FindResource(ctx, resourceFind)
|
2022-06-22 19:16:31 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource").SetInternal(err)
|
|
|
|
}
|
2022-02-03 15:32:03 +08:00
|
|
|
|
2022-06-22 19:16:31 +08:00
|
|
|
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 {
|
2022-08-07 10:17:12 +08:00
|
|
|
ctx := c.Request().Context()
|
2022-06-22 19:16:31 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-07-28 20:09:25 +08:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
2022-06-22 19:16:31 +08:00
|
|
|
resourceFind := &api.ResourceFind{
|
|
|
|
ID: &resourceID,
|
|
|
|
CreatorID: &userID,
|
|
|
|
}
|
2022-08-07 10:17:12 +08:00
|
|
|
resource, err := s.Store.FindResource(ctx, resourceFind)
|
2022-06-22 19:16:31 +08:00
|
|
|
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)
|
2022-07-02 10:47:16 +08:00
|
|
|
if _, err := c.Response().Writer.Write(resource.Blob); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to write resource blob").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
return nil
|
|
|
|
})
|
2022-02-18 22:21:10 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
g.DELETE("/resource/:resourceId", func(c echo.Context) error {
|
2022-08-07 10:17:12 +08:00
|
|
|
ctx := c.Request().Context()
|
2022-08-07 01:30:48 +08:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
|
|
|
|
2022-05-03 02:05:43 +08:00
|
|
|
resourceID, err := strconv.Atoi(c.Param("resourceId"))
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("resourceId"))).SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceDelete := &api.ResourceDelete{
|
2022-08-07 01:30:48 +08:00
|
|
|
ID: resourceID,
|
|
|
|
CreatorID: userID,
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-08-07 10:17:12 +08:00
|
|
|
if err := s.Store.DeleteResource(ctx, resourceDelete); err != nil {
|
2022-02-03 15:32:03 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete resource").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-07-02 10:47:16 +08:00
|
|
|
return c.JSON(http.StatusOK, true)
|
2022-02-03 15:32:03 +08:00
|
|
|
})
|
|
|
|
}
|