|
|
|
|
@ -21,6 +21,7 @@ import (
|
|
|
|
|
|
|
|
|
|
"github.com/usememos/memos/internal/profile"
|
|
|
|
|
"github.com/usememos/memos/internal/util"
|
|
|
|
|
"github.com/usememos/memos/plugin/filter"
|
|
|
|
|
"github.com/usememos/memos/plugin/storage/s3"
|
|
|
|
|
v1pb "github.com/usememos/memos/proto/gen/api/v1"
|
|
|
|
|
storepb "github.com/usememos/memos/proto/gen/store"
|
|
|
|
|
@ -156,6 +157,14 @@ func (s *APIV1Service) ListAttachments(ctx context.Context, request *v1pb.ListAt
|
|
|
|
|
Offset: &offset,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse filter if provided
|
|
|
|
|
if request.Filter != "" {
|
|
|
|
|
if err := s.validateAttachmentFilter(ctx, request.Filter); err != nil {
|
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "invalid filter: %v", err)
|
|
|
|
|
}
|
|
|
|
|
findAttachment.Filters = append(findAttachment.Filters, request.Filter)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attachments, err := s.Store.ListAttachments(ctx, findAttachment)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to list attachments: %v", err)
|
|
|
|
|
@ -472,3 +481,29 @@ func isValidMimeType(mimeType string) bool {
|
|
|
|
|
matched, _ := regexp.MatchString(`^[a-zA-Z0-9][a-zA-Z0-9!#$&^_.+-]{0,126}/[a-zA-Z0-9][a-zA-Z0-9!#$&^_.+-]{0,126}$`, mimeType)
|
|
|
|
|
return matched
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *APIV1Service) validateAttachmentFilter(ctx context.Context, filterStr string) error {
|
|
|
|
|
if filterStr == "" {
|
|
|
|
|
return errors.New("filter cannot be empty")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
engine, err := filter.DefaultAttachmentEngine()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var dialect filter.DialectName
|
|
|
|
|
switch s.Profile.Driver {
|
|
|
|
|
case "mysql":
|
|
|
|
|
dialect = filter.DialectMySQL
|
|
|
|
|
case "postgres":
|
|
|
|
|
dialect = filter.DialectPostgres
|
|
|
|
|
default:
|
|
|
|
|
dialect = filter.DialectSQLite
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err := engine.CompileToStatement(ctx, filterStr, filter.RenderOptions{Dialect: dialect}); err != nil {
|
|
|
|
|
return errors.Wrap(err, "failed to compile filter")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|