resource.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. MemoID *int32
  48. Blob []byte
  49. }
  50. type DeleteResource struct {
  51. ID int32
  52. MemoID *int32
  53. }
  54. func (s *Store) CreateResource(ctx context.Context, create *Resource) (*Resource, error) {
  55. if !util.ResourceNameMatcher.MatchString(create.ResourceName) {
  56. return nil, errors.New("invalid resource name")
  57. }
  58. return s.driver.CreateResource(ctx, create)
  59. }
  60. func (s *Store) ListResources(ctx context.Context, find *FindResource) ([]*Resource, error) {
  61. return s.driver.ListResources(ctx, find)
  62. }
  63. func (s *Store) GetResource(ctx context.Context, find *FindResource) (*Resource, error) {
  64. resources, err := s.ListResources(ctx, find)
  65. if err != nil {
  66. return nil, err
  67. }
  68. if len(resources) == 0 {
  69. return nil, nil
  70. }
  71. return resources[0], nil
  72. }
  73. func (s *Store) UpdateResource(ctx context.Context, update *UpdateResource) (*Resource, error) {
  74. if update.ResourceName != nil && !util.ResourceNameMatcher.MatchString(*update.ResourceName) {
  75. return nil, errors.New("invalid resource name")
  76. }
  77. return s.driver.UpdateResource(ctx, update)
  78. }
  79. func (s *Store) DeleteResource(ctx context.Context, delete *DeleteResource) error {
  80. resource, err := s.GetResource(ctx, &FindResource{ID: &delete.ID})
  81. if err != nil {
  82. return errors.Wrap(err, "failed to get resource")
  83. }
  84. if resource == nil {
  85. return errors.Wrap(nil, "resource not found")
  86. }
  87. // Delete the local file.
  88. if resource.InternalPath != "" {
  89. resourcePath := filepath.FromSlash(resource.InternalPath)
  90. if !filepath.IsAbs(resourcePath) {
  91. resourcePath = filepath.Join(s.Profile.Data, resourcePath)
  92. }
  93. _ = os.Remove(resourcePath)
  94. }
  95. // Delete the thumbnail.
  96. if util.HasPrefixes(resource.Type, "image/png", "image/jpeg") {
  97. ext := filepath.Ext(resource.Filename)
  98. thumbnailPath := filepath.Join(s.Profile.Data, thumbnailImagePath, fmt.Sprintf("%d%s", resource.ID, ext))
  99. _ = os.Remove(thumbnailPath)
  100. }
  101. return s.driver.DeleteResource(ctx, delete)
  102. }