resource.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package store
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "github.com/pkg/errors"
  8. "github.com/usememos/memos/internal/util"
  9. )
  10. const (
  11. // thumbnailImagePath is the directory to store image thumbnails.
  12. thumbnailImagePath = ".thumbnail_cache"
  13. )
  14. type Resource struct {
  15. ID int32
  16. ResourceName string
  17. // Standard fields
  18. CreatorID int32
  19. CreatedTs int64
  20. UpdatedTs int64
  21. // Domain specific fields
  22. Filename string
  23. Blob []byte
  24. InternalPath string
  25. ExternalLink string
  26. Type string
  27. Size int64
  28. MemoID *int32
  29. }
  30. type FindResource struct {
  31. GetBlob bool
  32. ID *int32
  33. ResourceName *string
  34. CreatorID *int32
  35. Filename *string
  36. MemoID *int32
  37. HasRelatedMemo bool
  38. Limit *int
  39. Offset *int
  40. }
  41. type UpdateResource struct {
  42. ID int32
  43. ResourceName *string
  44. UpdatedTs *int64
  45. Filename *string
  46. InternalPath *string
  47. ExternalLink *string
  48. MemoID *int32
  49. Blob []byte
  50. }
  51. type DeleteResource struct {
  52. ID int32
  53. MemoID *int32
  54. }
  55. func (s *Store) CreateResource(ctx context.Context, create *Resource) (*Resource, error) {
  56. if !util.ResourceNameMatcher.MatchString(create.ResourceName) {
  57. return nil, errors.New("invalid resource name")
  58. }
  59. return s.driver.CreateResource(ctx, create)
  60. }
  61. func (s *Store) ListResources(ctx context.Context, find *FindResource) ([]*Resource, error) {
  62. return s.driver.ListResources(ctx, find)
  63. }
  64. func (s *Store) GetResource(ctx context.Context, find *FindResource) (*Resource, error) {
  65. resources, err := s.ListResources(ctx, find)
  66. if err != nil {
  67. return nil, err
  68. }
  69. if len(resources) == 0 {
  70. return nil, nil
  71. }
  72. return resources[0], nil
  73. }
  74. func (s *Store) UpdateResource(ctx context.Context, update *UpdateResource) (*Resource, error) {
  75. if update.ResourceName != nil && !util.ResourceNameMatcher.MatchString(*update.ResourceName) {
  76. return nil, errors.New("invalid resource name")
  77. }
  78. return s.driver.UpdateResource(ctx, update)
  79. }
  80. func (s *Store) DeleteResource(ctx context.Context, delete *DeleteResource) error {
  81. resource, err := s.GetResource(ctx, &FindResource{ID: &delete.ID})
  82. if err != nil {
  83. return errors.Wrap(err, "failed to get resource")
  84. }
  85. if resource == nil {
  86. return errors.Wrap(nil, "resource not found")
  87. }
  88. // Delete the local file.
  89. if resource.InternalPath != "" {
  90. resourcePath := filepath.FromSlash(resource.InternalPath)
  91. if !filepath.IsAbs(resourcePath) {
  92. resourcePath = filepath.Join(s.Profile.Data, resourcePath)
  93. }
  94. _ = os.Remove(resourcePath)
  95. }
  96. // Delete the thumbnail.
  97. if util.HasPrefixes(resource.Type, "image/png", "image/jpeg") {
  98. ext := filepath.Ext(resource.Filename)
  99. thumbnailPath := filepath.Join(s.Profile.Data, thumbnailImagePath, fmt.Sprintf("%d%s", resource.ID, ext))
  100. _ = os.Remove(thumbnailPath)
  101. }
  102. return s.driver.DeleteResource(ctx, delete)
  103. }