2021-12-14 15:08:12 +03:00
|
|
|
package store
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
import (
|
2022-08-07 05:17:12 +03:00
|
|
|
"context"
|
2023-10-28 05:42:39 +03:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/usememos/memos/internal/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// thumbnailImagePath is the directory to store image thumbnails.
|
|
|
|
thumbnailImagePath = ".thumbnail_cache"
|
2022-02-03 10:32:03 +03:00
|
|
|
)
|
2021-12-14 15:08:12 +03:00
|
|
|
|
2023-07-05 19:01:40 +03:00
|
|
|
type Resource struct {
|
2023-08-04 16:55:07 +03:00
|
|
|
ID int32
|
2022-05-19 13:32:04 +03:00
|
|
|
|
|
|
|
// Standard fields
|
2023-08-04 16:55:07 +03:00
|
|
|
CreatorID int32
|
2022-05-19 13:32:04 +03:00
|
|
|
CreatedTs int64
|
|
|
|
UpdatedTs int64
|
|
|
|
|
|
|
|
// Domain specific fields
|
2023-09-15 19:11:07 +03:00
|
|
|
Filename string
|
|
|
|
Blob []byte
|
|
|
|
InternalPath string
|
|
|
|
ExternalLink string
|
|
|
|
Type string
|
|
|
|
Size int64
|
2023-09-26 19:40:16 +03:00
|
|
|
MemoID *int32
|
2022-05-19 13:32:04 +03:00
|
|
|
}
|
|
|
|
|
2023-07-05 19:01:40 +03:00
|
|
|
type FindResource struct {
|
2023-09-16 06:48:53 +03:00
|
|
|
GetBlob bool
|
|
|
|
ID *int32
|
|
|
|
CreatorID *int32
|
|
|
|
Filename *string
|
|
|
|
MemoID *int32
|
|
|
|
HasRelatedMemo bool
|
|
|
|
Limit *int
|
|
|
|
Offset *int
|
2022-05-19 13:32:04 +03:00
|
|
|
}
|
|
|
|
|
2023-07-05 19:01:40 +03:00
|
|
|
type UpdateResource struct {
|
2023-08-04 16:55:07 +03:00
|
|
|
ID int32
|
2023-08-03 14:08:39 +03:00
|
|
|
UpdatedTs *int64
|
|
|
|
Filename *string
|
|
|
|
InternalPath *string
|
2023-09-26 19:40:16 +03:00
|
|
|
MemoID *int32
|
2023-08-03 14:08:39 +03:00
|
|
|
Blob []byte
|
2023-07-05 19:01:40 +03:00
|
|
|
}
|
2022-10-01 05:37:02 +03:00
|
|
|
|
2023-07-05 19:01:40 +03:00
|
|
|
type DeleteResource struct {
|
2023-09-26 19:40:16 +03:00
|
|
|
ID int32
|
|
|
|
MemoID *int32
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2021-12-14 15:08:12 +03:00
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
func (s *Store) CreateResource(ctx context.Context, create *Resource) (*Resource, error) {
|
2023-09-26 14:04:07 +03:00
|
|
|
return s.driver.CreateResource(ctx, create)
|
2023-05-25 19:38:27 +03:00
|
|
|
}
|
|
|
|
|
2023-07-05 19:01:40 +03:00
|
|
|
func (s *Store) ListResources(ctx context.Context, find *FindResource) ([]*Resource, error) {
|
2023-09-26 14:04:07 +03:00
|
|
|
return s.driver.ListResources(ctx, find)
|
2022-11-06 07:21:58 +03:00
|
|
|
}
|
|
|
|
|
2023-07-20 18:15:56 +03:00
|
|
|
func (s *Store) GetResource(ctx context.Context, find *FindResource) (*Resource, error) {
|
|
|
|
resources, err := s.ListResources(ctx, find)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resources) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return resources[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) UpdateResource(ctx context.Context, update *UpdateResource) (*Resource, error) {
|
2023-09-26 14:04:07 +03:00
|
|
|
return s.driver.UpdateResource(ctx, update)
|
2023-07-20 18:15:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) DeleteResource(ctx context.Context, delete *DeleteResource) error {
|
2023-10-28 05:42:39 +03:00
|
|
|
resource, err := s.GetResource(ctx, &FindResource{ID: &delete.ID})
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to get resource")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the local file.
|
|
|
|
if resource.InternalPath != "" {
|
|
|
|
_ = os.Remove(resource.InternalPath)
|
|
|
|
}
|
|
|
|
// Delete the thumbnail.
|
|
|
|
if 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))
|
|
|
|
_ = os.Remove(thumbnailPath)
|
|
|
|
}
|
2023-09-27 04:27:31 +03:00
|
|
|
return s.driver.DeleteResource(ctx, delete)
|
2021-12-14 15:08:12 +03:00
|
|
|
}
|