Skip to content
16 changes: 8 additions & 8 deletions internal/api/script/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ type CreateRequest struct {
mux.Meta `path:"/scripts" method:"POST"`
Content string `form:"content" binding:"required,max=102400" label:"脚本详细描述"`
Code string `form:"code" binding:"required,max=10485760" label:"脚本代码"`
Name string `form:"name" binding:"max=128" label:"库的名字"`
Description string `form:"description" binding:"max=10240" label:"库的描述"`
Name string `form:"name" binding:"max=50" label:"库的名字"`
Description string `form:"description" binding:"max=200" label:"库的描述"`
Definition string `form:"definition" binding:"max=10240" label:"库的定义文件"`
Version string `form:"version" binding:"max=32" label:"库的版本"`
Tags []string `form:"tags" binding:"omitempty,max=64" label:"标签"` // 标签,只有脚本类型为库时才有意义
Tags []string `form:"tags" binding:"omitempty,max=5" label:"标签"` // 标签,只有脚本类型为库时才有意义
CategoryID int64 `form:"category" binding:"omitempty,numeric" label:"分类ID"` // 分类ID
Type script_entity.Type `form:"type" binding:"required,oneof=1 2 3" label:"脚本类型"` // 脚本类型:1 用户脚本 2 订阅脚本(不支持) 3 脚本引用库
Public script_entity.Public `form:"public" binding:"required,oneof=1 2 3" label:"公开类型"` // 公开类型:1 公开 2 半公开 3 私有
Expand Down Expand Up @@ -120,7 +120,7 @@ type UpdateCodeRequest struct {
//Name string `form:"name" binding:"max=128" label:"库的名字"`
//Description string `form:"description" binding:"max=102400" label:"库的描述"`
Version string `binding:"required,max=128" form:"version" label:"库的版本号"`
Tags []string `form:"tags" binding:"omitempty,max=64" label:"标签"` // 标签,只有脚本类型为库时才有意义
Tags []string `form:"tags" binding:"omitempty,max=5" label:"标签"` // 标签,只有脚本类型为库时才有意义
Content string `binding:"required,max=102400" form:"content" label:"脚本详细描述"`
Code string `binding:"required,max=10485760" form:"code" label:"脚本代码"`
Definition string `binding:"max=102400" form:"definition" label:"库的定义文件"`
Expand Down Expand Up @@ -260,8 +260,8 @@ type GetSettingResponse struct {
type UpdateSettingRequest struct {
mux.Meta `path:"/scripts/:id/setting" method:"PUT"`
ID int64 `uri:"id" binding:"required"`
Name string `json:"name" binding:"max=128" label:"库的名字"`
Description string `json:"description" binding:"max=102400" label:"库的描述"`
Name string `json:"name" binding:"max=50" label:"库的名字"`
Description string `json:"description" binding:"max=200" label:"库的描述"`
SyncUrl string `json:"sync_url" binding:"omitempty,url,max=1024" label:"代码同步url"`
ContentUrl string `json:"content_url" binding:"omitempty,url,max=1024" label:"详细描述同步url"`
DefinitionUrl string `json:"definition_url" binding:"omitempty,url,max=1024" label:"定义文件同步url"`
Expand All @@ -276,8 +276,8 @@ type UpdateSettingResponse struct {
// UpdateLibInfoRequest 更新库信息
type UpdateLibInfoRequest struct {
mux.Meta `path:"/scripts/:id/lib-info" method:"PUT"`
Name string `json:"name" binding:"max=128" label:"库的名字"`
Description string `json:"description" binding:"max=102400" label:"库的描述"`
Name string `json:"name" binding:"max=50" label:"库的名字"`
Description string `json:"description" binding:"max=200" label:"库的描述"`
}

type UpdateLibInfoResponse struct {
Expand Down
3 changes: 3 additions & 0 deletions internal/pkg/code/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const (

ScriptDeleteReleaseNotLatest
ScriptCategoryNotFound
ScriptNameTooLong
ScriptDescTooLong
ScriptTagsTooMany
)

// issue
Expand Down
3 changes: 3 additions & 0 deletions internal/pkg/code/zh_cn.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ var zhCN = map[int]string{
WebhookRepositoryNotFound: "仓库不存在",
ScriptDeleteReleaseNotLatest: "删除发布版本失败,没有新的正式版本了",
ScriptCategoryNotFound: "脚本分类不存在",
ScriptNameTooLong: "脚本名称过长,最多50个字符",
ScriptDescTooLong: "脚本描述过长,最多200个字符",
ScriptTagsTooMany: "标签数量过多,最多5个",

IssueLabelNotExist: "标签不存在",
IssueNotFound: "反馈不存在",
Expand Down
21 changes: 21 additions & 0 deletions internal/service/script_svc/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strconv"
"strings"
"time"
"unicode/utf8"

"github.com/scriptscat/scriptlist/internal/repository/issue_repo"
"github.com/scriptscat/scriptlist/internal/repository/report_repo"
Expand Down Expand Up @@ -302,6 +303,20 @@ func (s *scriptSvc) scriptCode(ctx context.Context, script *script_entity.Script
return ret
}

// validateScriptMeta validates the name, description, and tags extracted from script metadata.
func validateScriptMeta(ctx context.Context, name, description string, tags []string) error {
if utf8.RuneCountInString(name) > 50 {
return i18n.NewError(ctx, code.ScriptNameTooLong)
}
if utf8.RuneCountInString(description) > 200 {
return i18n.NewError(ctx, code.ScriptDescTooLong)
}
if len(tags) > 5 {
return i18n.NewError(ctx, code.ScriptTagsTooMany)
}
return nil
}

// Create 创建脚本
func (s *scriptSvc) Create(ctx context.Context, req *api.CreateRequest) (*api.CreateResponse, error) {
script := &script_entity.Script{
Expand Down Expand Up @@ -353,6 +368,9 @@ func (s *scriptSvc) Create(ctx context.Context, req *api.CreateRequest) (*api.Cr
script.Description = metaJson["description"][0]
// 处理tag关联
tags = metaJson["tags"]
if err := validateScriptMeta(ctx, script.Name, script.Description, tags); err != nil {
return err
}
if len(metaJson["background"]) > 0 || len(metaJson["crontab"]) > 0 {
tags = append(tags, "后台脚本")
}
Expand Down Expand Up @@ -502,6 +520,9 @@ func (s *scriptSvc) UpdateCode(ctx context.Context, req *api.UpdateCodeRequest)
script.Name = metaJson["name"][0]
script.Description = metaJson["description"][0]
tags = req.Tags
if err := validateScriptMeta(ctx, script.Name, script.Description, tags); err != nil {
return nil, err
}
if len(metaJson["background"]) > 0 || len(metaJson["crontab"]) > 0 {
tags = append(tags, "后台脚本")
}
Expand Down