mirror of https://github.com/cloudreve/Cloudreve
feat: backend share link password check
parent
17fa44c290
commit
22c7bb0b35
|
@ -2,6 +2,7 @@ package share
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/cloudreve/Cloudreve/v4/application/dependency"
|
"github.com/cloudreve/Cloudreve/v4/application/dependency"
|
||||||
|
@ -28,6 +29,14 @@ type (
|
||||||
ShareCreateParamCtx struct{}
|
ShareCreateParamCtx struct{}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
PasswordMaxLength = 32 // 分享密码最大长度
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
PasswordRegexp = regexp.MustCompile("^[a-zA-Z0-9]*$")
|
||||||
|
)
|
||||||
|
|
||||||
// Upsert 创建或更新分享
|
// Upsert 创建或更新分享
|
||||||
func (service *ShareCreateService) Upsert(c *gin.Context, existed int) (string, error) {
|
func (service *ShareCreateService) Upsert(c *gin.Context, existed int) (string, error) {
|
||||||
dep := dependency.FromContext(c)
|
dep := dependency.FromContext(c)
|
||||||
|
@ -51,6 +60,16 @@ func (service *ShareCreateService) Upsert(c *gin.Context, existed int) (string,
|
||||||
*expires = time.Now().Add(time.Duration(service.Expire) * time.Second)
|
*expires = time.Now().Add(time.Duration(service.Expire) * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate password if provided
|
||||||
|
if service.Password != "" {
|
||||||
|
if len(service.Password) > PasswordMaxLength {
|
||||||
|
return "", serializer.NewError(serializer.CodeParamErr, "Password too long", nil)
|
||||||
|
}
|
||||||
|
if !PasswordRegexp.MatchString(service.Password) {
|
||||||
|
return "", serializer.NewError(serializer.CodeParamErr, "Invalid password format", nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
share, err := m.CreateOrUpdateShare(c, uri, &manager.CreateShareArgs{
|
share, err := m.CreateOrUpdateShare(c, uri, &manager.CreateShareArgs{
|
||||||
IsPrivate: service.IsPrivate,
|
IsPrivate: service.IsPrivate,
|
||||||
Password: service.Password,
|
Password: service.Password,
|
||||||
|
|
Loading…
Reference in New Issue