From ac81d856f637c5e35fbc6246704ba748c1298855 Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 28 Oct 2023 10:42:39 +0800 Subject: [PATCH] chore: delete resource file sync --- api/v1/resource.go | 15 --------------- api/v2/resource_service.go | 21 --------------------- store/resource.go | 27 +++++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 36 deletions(-) diff --git a/api/v1/resource.go b/api/v1/resource.go index fc1ca1e9..959fb8fc 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 << 20 MebiByte = 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.go index bc490fdc..2ad7ad0e 100644 --- a/api/v2/resource_service.go +++ b/api/v2/resource_service.go @@ -2,26 +2,16 @@ package v2 import ( "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.Dele if 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.go index 82999564..ae3b9709 100644 --- a/store/resource.go +++ b/store/resource.go @@ -2,6 +2,18 @@ package store import ( "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) }