Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.
Go dockerfossgomarkdownmemomicroblognote-takingnotesopen-sourceown-your-datareactself-hostedsqlitetimeline
chore: delete resource file sync
Stevenac81d856f637
diff --git a/api/v1/resource.go b/api/v1/resource.goindex fc1ca1e94..959fb8fc5 100644--- a/api/v1/resource.go+++ b/api/v1/resource.go@@ -63,9 +63,6 @@ const (// This is unrelated to maximum upload size limit, which is now set through system setting.maxUploadBufferSizeBytes = 32 << 20MebiByte = 1024 * 1024-- // thumbnailImagePath is the directory to store image thumbnails.- thumbnailImagePath = ".thumbnail_cache")var fileKeyPattern = regexp.MustCompile(`\{[a-z]{1,9}\}`)@@ -268,18 +265,6 @@ func (s *APIV1Service) DeleteResource(c echo.Context) error {return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Resource not found: %d", resourceID))}- if resource.InternalPath != "" {- if err := os.Remove(resource.InternalPath); err != nil {- log.Warn(fmt.Sprintf("failed to delete local file with path %s", resource.InternalPath), zap.Error(err))- }- }-- ext := filepath.Ext(resource.Filename)- thumbnailPath := filepath.Join(s.Profile.Data, thumbnailImagePath, fmt.Sprintf("%d%s", resource.ID, ext))- if err := os.Remove(thumbnailPath); err != nil {- log.Warn(fmt.Sprintf("failed to delete local thumbnail with path %s", thumbnailPath), zap.Error(err))- }-if err := s.Store.DeleteResource(ctx, &store.DeleteResource{ID: resourceID,}); err != nil {diff --git a/api/v2/resource_service.go b/api/v2/resource_service.goindex bc490fdc0..2ad7ad0e5 100644--- a/api/v2/resource_service.go+++ b/api/v2/resource_service.go@@ -2,26 +2,16 @@ package v2import ("context"- "fmt"- "os"- "path/filepath""time"- "go.uber.org/zap""google.golang.org/grpc/codes""google.golang.org/grpc/status""google.golang.org/protobuf/types/known/timestamppb"- "github.com/usememos/memos/internal/log"apiv2pb "github.com/usememos/memos/proto/gen/api/v2""github.com/usememos/memos/store")-const (- // thumbnailImagePath is the directory to store image thumbnails.- thumbnailImagePath = ".thumbnail_cache"-)-func (s *APIV2Service) ListResources(ctx context.Context, _ *apiv2pb.ListResourcesRequest) (*apiv2pb.ListResourcesResponse, error) {user, err := getCurrentUser(ctx, s.Store)if err != nil {@@ -83,17 +73,6 @@ func (s *APIV2Service) DeleteResource(ctx context.Context, request *apiv2pb.Deleif resource == nil {return nil, status.Errorf(codes.NotFound, "resource not found")}- // Delete the local file synchronously if it exists.- if resource.InternalPath != "" {- if err := os.Remove(resource.InternalPath); err != nil {- log.Warn(fmt.Sprintf("failed to delete local file with path %s", resource.InternalPath), zap.Error(err))- }- }- // Delete the local thumbnail synchronously if it exists.- thumbnailPath := filepath.Join(s.Profile.Data, thumbnailImagePath, fmt.Sprintf("%d%s", resource.ID, filepath.Ext(resource.Filename)))- if err := os.Remove(thumbnailPath); err != nil {- log.Warn(fmt.Sprintf("failed to delete local thumbnail with path %s", thumbnailPath), zap.Error(err))- }// Delete the resource from the database.if err := s.Store.DeleteResource(ctx, &store.DeleteResource{ID: resource.ID,diff --git a/store/resource.go b/store/resource.goindex 829995642..ae3b9709a 100644--- a/store/resource.go+++ b/store/resource.go@@ -2,6 +2,18 @@ package storeimport ("context"+ "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")type Resource struct {@@ -73,5 +85,20 @@ func (s *Store) UpdateResource(ctx context.Context, update *UpdateResource) (*Re}func (s *Store) DeleteResource(ctx context.Context, delete *DeleteResource) error {+ 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)+ }return s.driver.DeleteResource(ctx, delete)}