2022-02-03 10:32:03 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-02-04 11:51:48 +03:00
|
|
|
"encoding/json"
|
2022-02-03 10:32:03 +03:00
|
|
|
"fmt"
|
2022-09-08 19:50:58 +03:00
|
|
|
"html"
|
2022-08-20 06:36:24 +03:00
|
|
|
"io"
|
2022-02-03 10:32:03 +03:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2022-10-29 10:40:09 +03:00
|
|
|
"time"
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2022-06-27 17:09:06 +03:00
|
|
|
"github.com/usememos/memos/api"
|
2022-09-03 13:54:22 +03:00
|
|
|
"github.com/usememos/memos/common"
|
2022-10-29 06:15:39 +03:00
|
|
|
metric "github.com/usememos/memos/plugin/metrics"
|
2022-06-27 17:09:06 +03:00
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2022-11-18 16:17:52 +03:00
|
|
|
const (
|
|
|
|
// The max file size is 32MB.
|
|
|
|
maxFileSize = (32 * 8) << 20
|
|
|
|
)
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
func (s *Server) registerResourceRoutes(g *echo.Group) {
|
|
|
|
g.POST("/resource", func(c echo.Context) error {
|
2022-08-07 05:17:12 +03:00
|
|
|
ctx := c.Request().Context()
|
2022-07-28 15:09:25 +03:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2022-11-18 16:17:52 +03:00
|
|
|
if err := c.Request().ParseMultipartForm(maxFileSize); err != nil {
|
2022-02-03 10:32:03 +03:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Upload file overload max size").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := c.FormFile("file")
|
|
|
|
if err != nil {
|
2022-11-18 16:17:52 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get uploading file").SetInternal(err)
|
|
|
|
}
|
|
|
|
if file == nil {
|
2022-02-03 10:32:03 +03:00
|
|
|
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 13:54:24 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to open file").SetInternal(err)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
defer src.Close()
|
|
|
|
|
2022-08-20 06:36:24 +03:00
|
|
|
fileBytes, err := io.ReadAll(src)
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
2022-02-04 13:54:24 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to read file").SetInternal(err)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
resourceCreate := &api.ResourceCreate{
|
|
|
|
Filename: filename,
|
|
|
|
Type: filetype,
|
|
|
|
Size: size,
|
2022-02-04 13:54:24 +03:00
|
|
|
Blob: fileBytes,
|
2022-05-02 21:05:43 +03:00
|
|
|
CreatorID: userID,
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
2022-08-07 05:17:12 +03:00
|
|
|
resource, err := s.Store.CreateResource(ctx, resourceCreate)
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create resource").SetInternal(err)
|
|
|
|
}
|
2022-10-29 06:15:39 +03:00
|
|
|
s.Collector.Collect(ctx, &metric.Metric{
|
|
|
|
Name: "resource created",
|
|
|
|
})
|
2022-02-03 10:32:03 +03:00
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-02-04 12:06:04 +03:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(resource)); err != nil {
|
2022-05-02 21:05:43 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode resource response").SetInternal(err)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-18 17:21:10 +03:00
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
g.GET("/resource", func(c echo.Context) error {
|
2022-08-07 05:17:12 +03:00
|
|
|
ctx := c.Request().Context()
|
2022-07-28 15:09:25 +03:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
2022-02-03 10:32:03 +03:00
|
|
|
resourceFind := &api.ResourceFind{
|
2022-05-02 21:05:43 +03:00
|
|
|
CreatorID: &userID,
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2022-08-07 05:17:12 +03:00
|
|
|
list, err := s.Store.FindResourceList(ctx, resourceFind)
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource list").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-10-03 04:39:49 +03:00
|
|
|
for _, resource := range list {
|
|
|
|
memoResoureceList, err := s.Store.FindMemoResourceList(ctx, &api.MemoResourceFind{
|
|
|
|
ResourceID: &resource.ID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo resource list").SetInternal(err)
|
|
|
|
}
|
|
|
|
resource.LinkedMemoAmount = len(memoResoureceList)
|
|
|
|
}
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-02-04 12:06:04 +03:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(list)); err != nil {
|
2022-02-05 06:43:25 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode resource list response").SetInternal(err)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2022-06-22 14:16:31 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
g.GET("/resource/:resourceId", func(c echo.Context) error {
|
2022-08-07 05:17:12 +03:00
|
|
|
ctx := c.Request().Context()
|
2022-06-22 14:16:31 +03: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 15:09:25 +03:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
2022-06-22 14:16:31 +03:00
|
|
|
resourceFind := &api.ResourceFind{
|
|
|
|
ID: &resourceID,
|
|
|
|
CreatorID: &userID,
|
|
|
|
}
|
2022-08-07 05:17:12 +03:00
|
|
|
resource, err := s.Store.FindResource(ctx, resourceFind)
|
2022-06-22 14:16:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource").SetInternal(err)
|
|
|
|
}
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2022-06-22 14:16:31 +03: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 05:17:12 +03:00
|
|
|
ctx := c.Request().Context()
|
2022-06-22 14:16:31 +03: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 15:09:25 +03:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
2022-06-22 14:16:31 +03:00
|
|
|
resourceFind := &api.ResourceFind{
|
|
|
|
ID: &resourceID,
|
|
|
|
CreatorID: &userID,
|
|
|
|
}
|
2022-08-07 05:17:12 +03:00
|
|
|
resource, err := s.Store.FindResource(ctx, resourceFind)
|
2022-06-22 14:16:31 +03: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 05:47:16 +03: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 10:32:03 +03:00
|
|
|
return nil
|
|
|
|
})
|
2022-02-18 17:21:10 +03:00
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
g.DELETE("/resource/:resourceId", func(c echo.Context) error {
|
2022-08-07 05:17:12 +03:00
|
|
|
ctx := c.Request().Context()
|
2022-08-06 20:30:48 +03:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
|
|
|
|
2022-05-02 21:05:43 +03:00
|
|
|
resourceID, err := strconv.Atoi(c.Param("resourceId"))
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("resourceId"))).SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-11-06 07:21:58 +03:00
|
|
|
resource, err := s.Store.FindResource(ctx, &api.ResourceFind{
|
|
|
|
ID: &resourceID,
|
|
|
|
CreatorID: &userID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find resource").SetInternal(err)
|
|
|
|
}
|
|
|
|
if resource == nil {
|
|
|
|
return echo.NewHTTPError(http.StatusNotFound, "Not find resource").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
resourceDelete := &api.ResourceDelete{
|
2022-11-06 07:21:58 +03:00
|
|
|
ID: resourceID,
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2022-08-07 05:17:12 +03:00
|
|
|
if err := s.Store.DeleteResource(ctx, resourceDelete); err != nil {
|
2022-09-03 13:54:22 +03:00
|
|
|
if common.ErrorCode(err) == common.NotFound {
|
|
|
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Resource ID not found: %d", resourceID))
|
|
|
|
}
|
2022-02-03 10:32:03 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete resource").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-07-02 05:47:16 +03:00
|
|
|
return c.JSON(http.StatusOK, true)
|
2022-02-03 10:32:03 +03:00
|
|
|
})
|
2022-10-29 10:40:09 +03:00
|
|
|
|
|
|
|
g.PATCH("/resource/:resourceId", func(c echo.Context) error {
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceFind := &api.ResourceFind{
|
|
|
|
ID: &resourceID,
|
|
|
|
CreatorID: &userID,
|
|
|
|
}
|
|
|
|
if _, err := s.Store.FindResource(ctx, resourceFind); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find resource").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
currentTs := time.Now().Unix()
|
|
|
|
resourcePatch := &api.ResourcePatch{
|
|
|
|
ID: resourceID,
|
|
|
|
UpdatedTs: ¤tTs,
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(resourcePatch); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch resource request").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resource, err := s.Store.PatchResource(ctx, resourcePatch)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch 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
|
|
|
|
})
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2022-09-08 19:50:58 +03:00
|
|
|
|
|
|
|
func (s *Server) registerResourcePublicRoutes(g *echo.Group) {
|
|
|
|
g.GET("/r/:resourceId/:filename", func(c echo.Context) error {
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := html.UnescapeString(c.Param("filename"))
|
|
|
|
resourceFind := &api.ResourceFind{
|
|
|
|
ID: &resourceID,
|
|
|
|
Filename: &filename,
|
|
|
|
}
|
|
|
|
resource, err := s.Store.FindResource(ctx, resourceFind)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch resource ID: %v", resourceID)).SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Writer.WriteHeader(http.StatusOK)
|
|
|
|
c.Response().Writer.Header().Set("Content-Type", resource.Type)
|
2022-11-21 18:23:05 +03:00
|
|
|
c.Response().Writer.Header().Set(echo.HeaderCacheControl, "max-age=31536000, immutable")
|
2022-09-08 19:50:58 +03:00
|
|
|
if _, err := c.Response().Writer.Write(resource.Blob); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to write response").SetInternal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|