mirror of
https://github.com/usememos/memos.git
synced 2024-12-24 03:33:52 +03:00
201 lines
6.7 KiB
Go
201 lines
6.7 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"html"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/usememos/memos/api"
|
|
"github.com/usememos/memos/common"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
func (s *Server) registerResourceRoutes(g *echo.Group) {
|
|
g.POST("/resource", 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")
|
|
}
|
|
|
|
err := c.Request().ParseMultipartForm(64 << 20)
|
|
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 {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to open file").SetInternal(err)
|
|
}
|
|
defer src.Close()
|
|
|
|
fileBytes, err := io.ReadAll(src)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to read file").SetInternal(err)
|
|
}
|
|
|
|
resourceCreate := &api.ResourceCreate{
|
|
Filename: filename,
|
|
Type: filetype,
|
|
Size: size,
|
|
Blob: fileBytes,
|
|
CreatorID: userID,
|
|
}
|
|
|
|
resource, err := s.Store.CreateResource(ctx, resourceCreate)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create 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", 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")
|
|
}
|
|
resourceFind := &api.ResourceFind{
|
|
CreatorID: &userID,
|
|
}
|
|
list, err := s.Store.FindResourceList(ctx, resourceFind)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource list").SetInternal(err)
|
|
}
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
|
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 {
|
|
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)
|
|
}
|
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
if !ok {
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
}
|
|
resourceFind := &api.ResourceFind{
|
|
ID: &resourceID,
|
|
CreatorID: &userID,
|
|
}
|
|
resource, err := s.Store.FindResource(ctx, 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 {
|
|
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)
|
|
}
|
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
if !ok {
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
}
|
|
resourceFind := &api.ResourceFind{
|
|
ID: &resourceID,
|
|
CreatorID: &userID,
|
|
}
|
|
resource, err := s.Store.FindResource(ctx, 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)
|
|
if _, err := c.Response().Writer.Write(resource.Blob); err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to write resource blob").SetInternal(err)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
g.DELETE("/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)
|
|
}
|
|
|
|
resourceDelete := &api.ResourceDelete{
|
|
ID: resourceID,
|
|
CreatorID: userID,
|
|
}
|
|
if err := s.Store.DeleteResource(ctx, resourceDelete); err != nil {
|
|
if common.ErrorCode(err) == common.NotFound {
|
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Resource ID not found: %d", resourceID))
|
|
}
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete resource").SetInternal(err)
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, true)
|
|
})
|
|
}
|
|
|
|
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)
|
|
c.Response().Header().Set(echo.HeaderCacheControl, "max-age=31536000, immutable")
|
|
if _, err := c.Response().Writer.Write(resource.Blob); err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to write response").SetInternal(err)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|