Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
15eb95f9
Commit
15eb95f9
authored
Oct 06, 2023
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: delete resource file synchronously
parent
ed96d656
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
6 deletions
+31
-6
resource_service.go
api/v2/resource_service.go
+28
-3
v2.go
api/v2/v2.go
+3
-3
No files found.
api/v2/resource_service.go
View file @
15eb95f9
...
...
@@ -2,25 +2,38 @@ 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/common/log"
apiv2pb
"github.com/usememos/memos/proto/gen/api/v2"
"github.com/usememos/memos/server/profile"
"github.com/usememos/memos/store"
)
const
(
// thumbnailImagePath is the directory to store image thumbnails.
thumbnailImagePath
=
".thumbnail_cache"
)
type
ResourceService
struct
{
apiv2pb
.
UnimplementedResourceServiceServer
Profile
*
profile
.
Profile
Store
*
store
.
Store
}
// NewResourceService creates a new ResourceService.
func
NewResourceService
(
store
*
store
.
Store
)
*
ResourceService
{
func
NewResourceService
(
profile
*
profile
.
Profile
,
store
*
store
.
Store
)
*
ResourceService
{
return
&
ResourceService
{
Profile
:
profile
,
Store
:
store
,
}
}
...
...
@@ -82,6 +95,18 @@ func (s *ResourceService) DeleteResource(ctx context.Context, request *apiv2pb.D
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
,
});
err
!=
nil
{
...
...
api/v2/v2.go
View file @
15eb95f9
...
...
@@ -4,7 +4,7 @@ import (
"context"
"fmt"
grpcRuntime
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"github.com/labstack/echo/v4"
"google.golang.org/grpc"
...
...
@@ -37,7 +37,7 @@ func NewAPIV2Service(secret string, profile *profile.Profile, store *store.Store
apiv2pb
.
RegisterUserServiceServer
(
grpcServer
,
NewUserService
(
store
,
secret
))
apiv2pb
.
RegisterMemoServiceServer
(
grpcServer
,
NewMemoService
(
store
))
apiv2pb
.
RegisterTagServiceServer
(
grpcServer
,
NewTagService
(
store
))
apiv2pb
.
RegisterResourceServiceServer
(
grpcServer
,
NewResourceService
(
store
))
apiv2pb
.
RegisterResourceServiceServer
(
grpcServer
,
NewResourceService
(
profile
,
store
))
reflection
.
Register
(
grpcServer
)
return
&
APIV2Service
{
...
...
@@ -66,7 +66,7 @@ func (s *APIV2Service) RegisterGateway(ctx context.Context, e *echo.Echo) error
return
err
}
gwMux
:=
grpcR
untime
.
NewServeMux
()
gwMux
:=
r
untime
.
NewServeMux
()
if
err
:=
apiv2pb
.
RegisterSystemServiceHandler
(
context
.
Background
(),
gwMux
,
conn
);
err
!=
nil
{
return
err
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment