mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: add memo content length limit setting
This commit is contained in:
@ -27,9 +27,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultPageSize = 10
|
||||
MaxContentLength = 8 * 1024
|
||||
ChunkSize = 64 * 1024 // 64 KiB
|
||||
DefaultPageSize = 10
|
||||
)
|
||||
|
||||
func (s *APIV1Service) CreateMemo(ctx context.Context, request *v1pb.CreateMemoRequest) (*v1pb.Memo, error) {
|
||||
@ -40,9 +38,6 @@ func (s *APIV1Service) CreateMemo(ctx context.Context, request *v1pb.CreateMemoR
|
||||
if user == nil {
|
||||
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
||||
}
|
||||
if len(request.Content) > MaxContentLength {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "content too long")
|
||||
}
|
||||
|
||||
create := &store.Memo{
|
||||
UID: shortuuid.New(),
|
||||
@ -57,6 +52,13 @@ func (s *APIV1Service) CreateMemo(ctx context.Context, request *v1pb.CreateMemoR
|
||||
if workspaceMemoRelatedSetting.DisallowPublicVisible && create.Visibility == store.Public {
|
||||
return nil, status.Errorf(codes.PermissionDenied, "disable public memos system setting is enabled")
|
||||
}
|
||||
contentLengthLimit, err := s.getContentLengthLimit(ctx)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "failed to get content length limit")
|
||||
}
|
||||
if len(create.Content) > contentLengthLimit {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "content too long (max %d characters)", contentLengthLimit)
|
||||
}
|
||||
|
||||
memo, err := s.Store.CreateMemo(ctx, create)
|
||||
if err != nil {
|
||||
@ -257,8 +259,12 @@ func (s *APIV1Service) UpdateMemo(ctx context.Context, request *v1pb.UpdateMemoR
|
||||
}
|
||||
}
|
||||
}
|
||||
if update.Content != nil && len(*update.Content) > MaxContentLength {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "content too long")
|
||||
contentLengthLimit, err := s.getContentLengthLimit(ctx)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "failed to get content length limit")
|
||||
}
|
||||
if update.Content != nil && len(*update.Content) > contentLengthLimit {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "content too long (max %d characters)", contentLengthLimit)
|
||||
}
|
||||
|
||||
if err = s.Store.UpdateMemo(ctx, update); err != nil {
|
||||
@ -702,6 +708,14 @@ func (s *APIV1Service) buildMemoFindWithFilter(ctx context.Context, find *store.
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *APIV1Service) getContentLengthLimit(ctx context.Context) (int, error) {
|
||||
workspaceMemoRelatedSetting, err := s.Store.GetWorkspaceMemoRelatedSetting(ctx)
|
||||
if err != nil {
|
||||
return 0, status.Errorf(codes.Internal, "failed to get workspace memo related setting")
|
||||
}
|
||||
return int(workspaceMemoRelatedSetting.ContentLengthLimit), nil
|
||||
}
|
||||
|
||||
// SearchMemosFilterCELAttributes are the CEL attributes.
|
||||
var SearchMemosFilterCELAttributes = []cel.EnvOption{
|
||||
cel.Variable("content_search", cel.ListType(cel.StringType)),
|
||||
|
Reference in New Issue
Block a user