2022-02-03 15:32:03 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-01-12 00:00:44 +08:00
|
|
|
"bytes"
|
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"
|
2022-12-12 20:00:21 +08:00
|
|
|
"net/url"
|
2022-02-03 15:32:03 +08:00
|
|
|
"strconv"
|
2023-01-07 10:51:34 +08:00
|
|
|
"strings"
|
2022-10-29 15:40:09 +08:00
|
|
|
"time"
|
2022-02-03 15:32:03 +08:00
|
|
|
|
2023-02-13 19:36:48 +08:00
|
|
|
"github.com/labstack/echo/v4"
|
2023-01-02 23:18:12 +08:00
|
|
|
"github.com/pkg/errors"
|
2022-06-27 22:09:06 +08:00
|
|
|
"github.com/usememos/memos/api"
|
2022-09-03 18:54:22 +08:00
|
|
|
"github.com/usememos/memos/common"
|
2023-02-13 19:36:48 +08:00
|
|
|
"github.com/usememos/memos/plugin/storage/s3"
|
2022-02-03 15:32:03 +08:00
|
|
|
)
|
|
|
|
|
2022-11-18 21:17:52 +08:00
|
|
|
const (
|
|
|
|
// The max file size is 32MB.
|
2023-01-21 08:46:49 +08:00
|
|
|
maxFileSize = 32 << 20
|
2022-11-18 21:17:52 +08:00
|
|
|
)
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
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
|
|
|
|
2023-01-21 08:46:49 +08:00
|
|
|
resourceCreate := &api.ResourceCreate{}
|
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(resourceCreate); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post resource request").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceCreate.CreatorID = userID
|
2023-02-11 17:34:29 +08:00
|
|
|
// Only allow those external links with http prefix.
|
|
|
|
if resourceCreate.ExternalLink != "" && !strings.HasPrefix(resourceCreate.ExternalLink, "http") {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid external link")
|
|
|
|
}
|
2023-02-27 22:16:33 +08:00
|
|
|
if resourceCreate.Visibility == "" {
|
|
|
|
userResourceVisibilitySetting, err := s.Store.FindUserSetting(ctx, &api.UserSettingFind{
|
|
|
|
UserID: userID,
|
|
|
|
Key: api.UserSettingResourceVisibilityKey,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user setting").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if userResourceVisibilitySetting != nil {
|
|
|
|
resourceVisibility := api.Private
|
|
|
|
err := json.Unmarshal([]byte(userResourceVisibilitySetting.Value), &resourceVisibility)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal user setting value").SetInternal(err)
|
|
|
|
}
|
|
|
|
resourceCreate.Visibility = resourceVisibility
|
|
|
|
} else {
|
|
|
|
// Private is the default resource visibility.
|
|
|
|
resourceCreate.Visibility = api.Private
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-21 08:46:49 +08:00
|
|
|
resource, err := s.Store.CreateResource(ctx, resourceCreate)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create resource").SetInternal(err)
|
|
|
|
}
|
|
|
|
if err := s.createResourceCreateActivity(c, resource); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err)
|
|
|
|
}
|
2023-02-17 23:55:56 +08:00
|
|
|
return c.JSON(http.StatusOK, composeResponse(resource))
|
2023-01-21 08:46:49 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
g.POST("/resource/blob", 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")
|
|
|
|
}
|
|
|
|
|
2022-11-18 21:17:52 +08:00
|
|
|
if err := c.Request().ParseMultipartForm(maxFileSize); err != nil {
|
2022-02-03 15:32:03 +08:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Upload file overload max size").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := c.FormFile("file")
|
|
|
|
if err != nil {
|
2022-11-18 21:17:52 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get uploading file").SetInternal(err)
|
|
|
|
}
|
|
|
|
if file == nil {
|
2022-02-03 15:32:03 +08: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 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()
|
|
|
|
|
2023-02-15 22:54:46 +08:00
|
|
|
systemSetting, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{Name: api.SystemSettingStorageServiceIDName})
|
2023-02-13 19:36:48 +08:00
|
|
|
if err != nil && common.ErrorCode(err) != common.NotFound {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find storage").SetInternal(err)
|
|
|
|
}
|
2023-02-15 22:54:46 +08:00
|
|
|
storageServiceID := 0
|
|
|
|
if systemSetting != nil {
|
|
|
|
err = json.Unmarshal([]byte(systemSetting.Value), &storageServiceID)
|
2023-02-14 09:56:04 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal storage service id").SetInternal(err)
|
|
|
|
}
|
|
|
|
}
|
2023-02-15 22:54:46 +08:00
|
|
|
|
|
|
|
var resourceCreate *api.ResourceCreate
|
|
|
|
if storageServiceID == 0 {
|
2023-02-13 19:36:48 +08:00
|
|
|
fileBytes, err := io.ReadAll(src)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to read file").SetInternal(err)
|
|
|
|
}
|
|
|
|
resourceCreate = &api.ResourceCreate{
|
2023-02-27 22:16:33 +08:00
|
|
|
CreatorID: userID,
|
|
|
|
Filename: filename,
|
|
|
|
Type: filetype,
|
|
|
|
Size: size,
|
|
|
|
Blob: fileBytes,
|
2023-02-13 19:36:48 +08:00
|
|
|
}
|
|
|
|
} else {
|
2023-02-15 22:54:46 +08:00
|
|
|
storage, err := s.Store.FindStorage(ctx, &api.StorageFind{ID: &storageServiceID})
|
2023-02-13 19:36:48 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find storage").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2023-02-24 00:02:51 +08:00
|
|
|
if storage.Type == api.StorageS3 {
|
|
|
|
s3Config := storage.Config.S3Config
|
|
|
|
s3client, err := s3.NewClient(ctx, &s3.Config{
|
|
|
|
AccessKey: s3Config.AccessKey,
|
|
|
|
SecretKey: s3Config.SecretKey,
|
|
|
|
EndPoint: s3Config.EndPoint,
|
2023-03-04 07:59:44 +08:00
|
|
|
Path: s3Config.Path,
|
2023-02-24 00:02:51 +08:00
|
|
|
Region: s3Config.Region,
|
|
|
|
Bucket: s3Config.Bucket,
|
|
|
|
URLPrefix: s3Config.URLPrefix,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to new s3 client").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
link, err := s3client.UploadFile(ctx, filename, filetype, src)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upload via s3 client").SetInternal(err)
|
|
|
|
}
|
|
|
|
resourceCreate = &api.ResourceCreate{
|
|
|
|
CreatorID: userID,
|
|
|
|
Filename: filename,
|
|
|
|
Type: filetype,
|
|
|
|
ExternalLink: link,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Unsupported storage type")
|
2023-02-13 19:36:48 +08:00
|
|
|
}
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
2023-02-27 22:16:33 +08:00
|
|
|
if resourceCreate.Visibility == "" {
|
|
|
|
userResourceVisibilitySetting, err := s.Store.FindUserSetting(ctx, &api.UserSettingFind{
|
|
|
|
UserID: userID,
|
|
|
|
Key: api.UserSettingResourceVisibilityKey,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user setting").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if userResourceVisibilitySetting != nil {
|
|
|
|
resourceVisibility := api.Private
|
|
|
|
err := json.Unmarshal([]byte(userResourceVisibilitySetting.Value), &resourceVisibility)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal user setting value").SetInternal(err)
|
|
|
|
}
|
|
|
|
resourceCreate.Visibility = resourceVisibility
|
|
|
|
} else {
|
|
|
|
// Private is the default resource visibility.
|
|
|
|
resourceCreate.Visibility = api.Private
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2023-01-02 23:18:12 +08:00
|
|
|
if err := s.createResourceCreateActivity(c, resource); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err)
|
|
|
|
}
|
2023-02-17 23:55:56 +08:00
|
|
|
return c.JSON(http.StatusOK, composeResponse(resource))
|
2022-02-03 15:32:03 +08:00
|
|
|
})
|
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)
|
|
|
|
}
|
|
|
|
|
2022-10-03 09:39:49 +08:00
|
|
|
for _, resource := range list {
|
2022-11-26 14:23:29 +08:00
|
|
|
memoResourceList, err := s.Store.FindMemoResourceList(ctx, &api.MemoResourceFind{
|
2022-10-03 09:39:49 +08:00
|
|
|
ResourceID: &resource.ID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo resource list").SetInternal(err)
|
|
|
|
}
|
2022-11-26 14:23:29 +08:00
|
|
|
resource.LinkedMemoAmount = len(memoResourceList)
|
2022-10-03 09:39:49 +08:00
|
|
|
}
|
2023-02-17 23:55:56 +08:00
|
|
|
return c.JSON(http.StatusOK, composeResponse(list))
|
2022-06-22 19:16:31 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
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,
|
2023-01-25 16:11:02 +08:00
|
|
|
GetBlob: true,
|
2022-06-22 19:16:31 +08:00
|
|
|
}
|
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)
|
|
|
|
}
|
2023-02-17 23:55:56 +08:00
|
|
|
return c.JSON(http.StatusOK, composeResponse(resource))
|
2022-06-22 19:16:31 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
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,
|
2023-01-25 16:11:02 +08:00
|
|
|
GetBlob: true,
|
2022-06-22 19:16:31 +08:00
|
|
|
}
|
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)
|
|
|
|
}
|
2023-02-11 17:34:29 +08:00
|
|
|
return c.Stream(http.StatusOK, resource.Type, bytes.NewReader(resource.Blob))
|
2022-02-03 15:32:03 +08:00
|
|
|
})
|
2022-02-18 22:21:10 +08:00
|
|
|
|
2022-12-19 18:45:17 +08:00
|
|
|
g.PATCH("/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)
|
|
|
|
}
|
|
|
|
|
2022-12-19 18:45:17 +08:00
|
|
|
resourceFind := &api.ResourceFind{
|
2022-12-28 20:22:52 +08:00
|
|
|
ID: &resourceID,
|
2022-11-06 12:21:58 +08:00
|
|
|
}
|
2022-12-28 20:22:52 +08:00
|
|
|
resource, err := s.Store.FindResource(ctx, resourceFind)
|
|
|
|
if err != nil {
|
2022-12-19 18:45:17 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find resource").SetInternal(err)
|
2022-11-06 12:21:58 +08:00
|
|
|
}
|
2022-12-28 20:22:52 +08:00
|
|
|
if resource.CreatorID != userID {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
|
|
|
|
}
|
2022-11-06 12:21:58 +08:00
|
|
|
|
2022-12-19 18:45:17 +08:00
|
|
|
currentTs := time.Now().Unix()
|
|
|
|
resourcePatch := &api.ResourcePatch{
|
|
|
|
UpdatedTs: ¤tTs,
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-12-19 18:45:17 +08:00
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(resourcePatch); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch resource request").SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
2023-02-09 23:20:36 +08:00
|
|
|
resourcePatch.ID = resourceID
|
2022-12-28 20:22:52 +08:00
|
|
|
resource, err = s.Store.PatchResource(ctx, resourcePatch)
|
2022-12-19 18:45:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch resource").SetInternal(err)
|
|
|
|
}
|
2023-02-17 23:55:56 +08:00
|
|
|
return c.JSON(http.StatusOK, composeResponse(resource))
|
2022-02-03 15:32:03 +08:00
|
|
|
})
|
2022-10-29 15:40:09 +08:00
|
|
|
|
2022-12-19 18:45:17 +08:00
|
|
|
g.DELETE("/resource/:resourceId", func(c echo.Context) error {
|
2022-10-29 15:40:09 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-12-19 18:45:17 +08:00
|
|
|
resource, err := s.Store.FindResource(ctx, &api.ResourceFind{
|
2022-10-29 15:40:09 +08:00
|
|
|
ID: &resourceID,
|
|
|
|
CreatorID: &userID,
|
2022-12-19 18:45:17 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-10-29 15:40:09 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find resource").SetInternal(err)
|
|
|
|
}
|
2022-12-28 20:22:52 +08:00
|
|
|
if resource.CreatorID != userID {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
|
2022-10-29 15:40:09 +08:00
|
|
|
}
|
|
|
|
|
2022-12-19 18:45:17 +08:00
|
|
|
resourceDelete := &api.ResourceDelete{
|
|
|
|
ID: resourceID,
|
2022-10-29 15:40:09 +08:00
|
|
|
}
|
2022-12-19 18:45:17 +08:00
|
|
|
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)
|
2022-10-29 15:40:09 +08:00
|
|
|
}
|
2022-12-19 18:45:17 +08:00
|
|
|
return c.JSON(http.StatusOK, true)
|
2022-10-29 15:40:09 +08:00
|
|
|
})
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-09-09 00:50:58 +08: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)
|
|
|
|
}
|
2022-12-12 20:00:21 +08:00
|
|
|
filename, err := url.QueryUnescape(c.Param("filename"))
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("filename is invalid: %s", c.Param("filename"))).SetInternal(err)
|
|
|
|
}
|
2022-09-09 00:50:58 +08:00
|
|
|
resourceFind := &api.ResourceFind{
|
|
|
|
ID: &resourceID,
|
|
|
|
Filename: &filename,
|
2023-01-25 16:11:02 +08:00
|
|
|
GetBlob: true,
|
2022-09-09 00:50:58 +08:00
|
|
|
}
|
|
|
|
resource, err := s.Store.FindResource(ctx, resourceFind)
|
|
|
|
if err != nil {
|
2023-02-11 17:34:29 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find resource by ID: %v", resourceID)).SetInternal(err)
|
2022-09-09 00:50:58 +08:00
|
|
|
}
|
|
|
|
|
2022-11-21 23:23:05 +08:00
|
|
|
c.Response().Writer.Header().Set(echo.HeaderCacheControl, "max-age=31536000, immutable")
|
2022-12-23 20:02:42 +08:00
|
|
|
c.Response().Writer.Header().Set(echo.HeaderContentSecurityPolicy, "default-src 'self'")
|
2023-02-11 17:34:29 +08:00
|
|
|
resourceType := strings.ToLower(resource.Type)
|
|
|
|
if strings.HasPrefix(resourceType, "text") {
|
|
|
|
resourceType = echo.MIMETextPlainCharsetUTF8
|
|
|
|
} else if strings.HasPrefix(resourceType, "video") || strings.HasPrefix(resourceType, "audio") {
|
2023-01-20 16:52:38 +08:00
|
|
|
http.ServeContent(c.Response(), c.Request(), resource.Filename, time.Unix(resource.UpdatedTs, 0), bytes.NewReader(resource.Blob))
|
|
|
|
return nil
|
|
|
|
}
|
2023-01-12 00:00:44 +08:00
|
|
|
return c.Stream(http.StatusOK, resourceType, bytes.NewReader(resource.Blob))
|
2022-09-09 00:50:58 +08:00
|
|
|
})
|
|
|
|
}
|
2023-01-02 23:18:12 +08:00
|
|
|
|
|
|
|
func (s *Server) createResourceCreateActivity(c echo.Context, resource *api.Resource) error {
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
payload := api.ActivityResourceCreatePayload{
|
|
|
|
Filename: resource.Filename,
|
|
|
|
Type: resource.Type,
|
|
|
|
Size: resource.Size,
|
|
|
|
}
|
2023-02-17 23:55:56 +08:00
|
|
|
payloadBytes, err := json.Marshal(payload)
|
2023-01-02 23:18:12 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to marshal activity payload")
|
|
|
|
}
|
2023-01-05 20:56:50 +08:00
|
|
|
activity, err := s.Store.CreateActivity(ctx, &api.ActivityCreate{
|
2023-01-02 23:18:12 +08:00
|
|
|
CreatorID: resource.CreatorID,
|
|
|
|
Type: api.ActivityResourceCreate,
|
|
|
|
Level: api.ActivityInfo,
|
2023-02-17 23:55:56 +08:00
|
|
|
Payload: string(payloadBytes),
|
2023-01-02 23:18:12 +08:00
|
|
|
})
|
2023-01-07 11:49:58 +08:00
|
|
|
if err != nil || activity == nil {
|
|
|
|
return errors.Wrap(err, "failed to create activity")
|
|
|
|
}
|
2023-01-02 23:18:12 +08:00
|
|
|
return err
|
|
|
|
}
|