memos/server/resource.go

99 lines
2.9 KiB
Go
Raw Normal View History

2022-02-03 10:32:03 +03:00
package server
import (
"encoding/json"
2022-02-03 10:32:03 +03:00
"fmt"
"io/ioutil"
"memos/api"
"net/http"
"strconv"
"github.com/labstack/echo/v4"
)
func (s *Server) registerResourceRoutes(g *echo.Group) {
g.POST("/resource", func(c echo.Context) error {
2022-05-02 21:05:43 +03:00
userID := c.Get(getUserIDContextKey()).(int)
2022-02-03 10:32:03 +03:00
err := c.Request().ParseMultipartForm(64 << 20)
2022-02-03 10:32:03 +03: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 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-02-04 13:54:24 +03:00
fileBytes, err := ioutil.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-05-16 02:37:23 +03:00
resource, err := s.Store.CreateResource(resourceCreate)
2022-02-03 10:32:03 +03: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 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-05-02 21:05:43 +03:00
userID := c.Get(getUserIDContextKey()).(int)
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-05-16 02:37:23 +03:00
list, err := s.Store.FindResourceList(resourceFind)
2022-02-03 10:32:03 +03: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 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
}
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-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)
}
resourceDelete := &api.ResourceDelete{
2022-05-02 21:05:43 +03:00
ID: resourceID,
2022-02-03 10:32:03 +03:00
}
2022-05-16 02:37:23 +03:00
if err := s.Store.DeleteResource(resourceDelete); err != nil {
2022-02-03 10:32:03 +03:00
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete resource").SetInternal(err)
}
2022-02-05 08:17:43 +03:00
c.JSON(http.StatusOK, true)
2022-02-03 10:32:03 +03:00
return nil
})
}