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
ac81d856
Commit
ac81d856
authored
Oct 28, 2023
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: delete resource file sync
parent
88fb79e4
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
27 additions
and
36 deletions
+27
-36
resource.go
api/v1/resource.go
+0
-15
resource_service.go
api/v2/resource_service.go
+0
-21
resource.go
store/resource.go
+27
-0
No files found.
api/v1/resource.go
View file @
ac81d856
...
@@ -63,9 +63,6 @@ const (
...
@@ -63,9 +63,6 @@ const (
// This is unrelated to maximum upload size limit, which is now set through system setting.
// This is unrelated to maximum upload size limit, which is now set through system setting.
maxUploadBufferSizeBytes
=
32
<<
20
maxUploadBufferSizeBytes
=
32
<<
20
MebiByte
=
1024
*
1024
MebiByte
=
1024
*
1024
// thumbnailImagePath is the directory to store image thumbnails.
thumbnailImagePath
=
".thumbnail_cache"
)
)
var
fileKeyPattern
=
regexp
.
MustCompile
(
`\{[a-z]{1,9}\}`
)
var
fileKeyPattern
=
regexp
.
MustCompile
(
`\{[a-z]{1,9}\}`
)
...
@@ -268,18 +265,6 @@ func (s *APIV1Service) DeleteResource(c echo.Context) error {
...
@@ -268,18 +265,6 @@ func (s *APIV1Service) DeleteResource(c echo.Context) error {
return
echo
.
NewHTTPError
(
http
.
StatusNotFound
,
fmt
.
Sprintf
(
"Resource not found: %d"
,
resourceID
))
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
{
if
err
:=
s
.
Store
.
DeleteResource
(
ctx
,
&
store
.
DeleteResource
{
ID
:
resourceID
,
ID
:
resourceID
,
});
err
!=
nil
{
});
err
!=
nil
{
...
...
api/v2/resource_service.go
View file @
ac81d856
...
@@ -2,26 +2,16 @@ package v2
...
@@ -2,26 +2,16 @@ package v2
import
(
import
(
"context"
"context"
"fmt"
"os"
"path/filepath"
"time"
"time"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/usememos/memos/internal/log"
apiv2pb
"github.com/usememos/memos/proto/gen/api/v2"
apiv2pb
"github.com/usememos/memos/proto/gen/api/v2"
"github.com/usememos/memos/store"
"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
)
{
func
(
s
*
APIV2Service
)
ListResources
(
ctx
context
.
Context
,
_
*
apiv2pb
.
ListResourcesRequest
)
(
*
apiv2pb
.
ListResourcesResponse
,
error
)
{
user
,
err
:=
getCurrentUser
(
ctx
,
s
.
Store
)
user
,
err
:=
getCurrentUser
(
ctx
,
s
.
Store
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -83,17 +73,6 @@ func (s *APIV2Service) DeleteResource(ctx context.Context, request *apiv2pb.Dele
...
@@ -83,17 +73,6 @@ func (s *APIV2Service) DeleteResource(ctx context.Context, request *apiv2pb.Dele
if
resource
==
nil
{
if
resource
==
nil
{
return
nil
,
status
.
Errorf
(
codes
.
NotFound
,
"resource not found"
)
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.
// Delete the resource from the database.
if
err
:=
s
.
Store
.
DeleteResource
(
ctx
,
&
store
.
DeleteResource
{
if
err
:=
s
.
Store
.
DeleteResource
(
ctx
,
&
store
.
DeleteResource
{
ID
:
resource
.
ID
,
ID
:
resource
.
ID
,
...
...
store/resource.go
View file @
ac81d856
...
@@ -2,6 +2,18 @@ package store
...
@@ -2,6 +2,18 @@ package store
import
(
import
(
"context"
"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
{
type
Resource
struct
{
...
@@ -73,5 +85,20 @@ func (s *Store) UpdateResource(ctx context.Context, update *UpdateResource) (*Re
...
@@ -73,5 +85,20 @@ func (s *Store) UpdateResource(ctx context.Context, update *UpdateResource) (*Re
}
}
func
(
s
*
Store
)
DeleteResource
(
ctx
context
.
Context
,
delete
*
DeleteResource
)
error
{
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
)
return
s
.
driver
.
DeleteResource
(
ctx
,
delete
)
}
}
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