memos/server/route/resource/resource.go

164 lines
5.3 KiB
Go
Raw Normal View History

2023-10-08 19:40:30 +08:00
package resource
import (
"bytes"
"fmt"
"io"
2024-02-29 23:54:43 +08:00
"log/slog"
2023-10-08 19:40:30 +08:00
"net/http"
"os"
"path/filepath"
"strings"
"sync/atomic"
"time"
"github.com/disintegration/imaging"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
"github.com/usememos/memos/internal/util"
2023-10-08 19:40:30 +08:00
"github.com/usememos/memos/server/profile"
"github.com/usememos/memos/store"
)
const (
// The key name used to store user id in the context
// user id is extracted from the jwt token subject field.
userIDContextKey = "user-id"
// thumbnailImagePath is the directory to store image thumbnails.
thumbnailImagePath = ".thumbnail_cache"
)
2024-01-29 21:04:35 +08:00
type ResourceService struct {
2023-10-08 19:40:30 +08:00
Profile *profile.Profile
Store *store.Store
}
2024-01-29 21:04:35 +08:00
func NewResourceService(profile *profile.Profile, store *store.Store) *ResourceService {
return &ResourceService{
2023-10-08 19:40:30 +08:00
Profile: profile,
Store: store,
}
}
2024-01-29 21:04:35 +08:00
func (s *ResourceService) RegisterRoutes(g *echo.Group) {
2024-01-21 10:55:49 +08:00
g.GET("/r/:resourceName", s.streamResource)
g.GET("/r/:resourceName/*", s.streamResource)
2023-10-08 19:40:30 +08:00
}
2024-01-29 21:04:35 +08:00
func (s *ResourceService) streamResource(c echo.Context) error {
2023-10-08 19:40:30 +08:00
ctx := c.Request().Context()
2024-01-21 10:55:49 +08:00
resourceName := c.Param("resourceName")
2023-10-08 19:40:30 +08:00
resource, err := s.Store.GetResource(ctx, &store.FindResource{
2024-01-21 10:55:49 +08:00
ResourceName: &resourceName,
GetBlob: true,
2023-10-08 19:40:30 +08:00
})
if err != nil {
2024-01-21 10:55:49 +08:00
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find resource by id: %s", resourceName)).SetInternal(err)
2023-10-08 19:40:30 +08:00
}
if resource == nil {
2024-01-21 10:55:49 +08:00
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Resource not found: %s", resourceName))
2023-10-08 19:40:30 +08:00
}
// Check the related memo visibility.
if resource.MemoID != nil {
memo, err := s.Store.GetMemo(ctx, &store.FindMemo{
ID: resource.MemoID,
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find memo by ID: %v", resource.MemoID)).SetInternal(err)
}
if memo != nil && memo.Visibility != store.Public {
userID, ok := c.Get(userIDContextKey).(int32)
if !ok || (memo.Visibility == store.Private && userID != resource.CreatorID) {
return echo.NewHTTPError(http.StatusUnauthorized, "Resource visibility not match")
}
}
}
blob := resource.Blob
if resource.InternalPath != "" {
resourcePath := filepath.FromSlash(resource.InternalPath)
if !filepath.IsAbs(resourcePath) {
resourcePath = filepath.Join(s.Profile.Data, resourcePath)
}
2023-10-08 19:40:30 +08:00
src, err := os.Open(resourcePath)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to open the local resource: %s", resourcePath)).SetInternal(err)
}
defer src.Close()
blob, err = io.ReadAll(src)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to read the local resource: %s", resourcePath)).SetInternal(err)
}
}
if c.QueryParam("thumbnail") == "1" && util.HasPrefixes(resource.Type, "image/png", "image/jpeg") {
ext := filepath.Ext(resource.Filename)
thumbnailPath := filepath.Join(s.Profile.Data, thumbnailImagePath, fmt.Sprintf("%d%s", resource.ID, ext))
thumbnailBlob, err := getOrGenerateThumbnailImage(blob, thumbnailPath)
if err != nil {
2024-02-29 23:54:43 +08:00
slog.Warn("failed to get or generate thumbnail image", err)
2023-10-08 19:40:30 +08:00
} else {
blob = thumbnailBlob
}
}
2023-11-07 07:24:41 +08:00
c.Response().Writer.Header().Set(echo.HeaderCacheControl, "max-age=3600")
c.Response().Writer.Header().Set(echo.HeaderContentSecurityPolicy, "default-src 'none'; script-src 'none'; img-src 'self'; media-src 'self'; sandbox;")
c.Response().Writer.Header().Set("Content-Disposition", fmt.Sprintf(`filename="%s"`, resource.Filename))
2023-10-08 19:40:30 +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") {
http.ServeContent(c.Response(), c.Request(), resource.Filename, time.Unix(resource.UpdatedTs, 0), bytes.NewReader(blob))
return nil
}
return c.Stream(http.StatusOK, resourceType, bytes.NewReader(blob))
}
var availableGeneratorAmount int32 = 32
func getOrGenerateThumbnailImage(srcBlob []byte, dstPath string) ([]byte, error) {
if _, err := os.Stat(dstPath); err != nil {
if !errors.Is(err, os.ErrNotExist) {
return nil, errors.Wrap(err, "failed to check thumbnail image stat")
}
if atomic.LoadInt32(&availableGeneratorAmount) <= 0 {
return nil, errors.New("not enough available generator amount")
}
atomic.AddInt32(&availableGeneratorAmount, -1)
defer func() {
atomic.AddInt32(&availableGeneratorAmount, 1)
}()
reader := bytes.NewReader(srcBlob)
src, err := imaging.Decode(reader, imaging.AutoOrientation(true))
if err != nil {
return nil, errors.Wrap(err, "failed to decode thumbnail image")
}
thumbnailImage := imaging.Resize(src, 512, 0, imaging.Lanczos)
dstDir := filepath.Dir(dstPath)
2023-10-08 19:40:30 +08:00
if err := os.MkdirAll(dstDir, os.ModePerm); err != nil {
return nil, errors.Wrap(err, "failed to create thumbnail dir")
}
if err := imaging.Save(thumbnailImage, dstPath); err != nil {
return nil, errors.Wrap(err, "failed to resize thumbnail image")
}
}
dstFile, err := os.Open(dstPath)
if err != nil {
return nil, errors.Wrap(err, "failed to open the local resource")
}
defer dstFile.Close()
dstBlob, err := io.ReadAll(dstFile)
if err != nil {
return nil, errors.Wrap(err, "failed to read the local resource")
}
return dstBlob, nil
}