Skip to content

Commit

Permalink
refactor: add limit check (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackHole1 committed Mar 27, 2023
1 parent f65dc7e commit a9b0551
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,35 @@ func FindWithDir(dir string) ([]*SchemaBody, error) {
return nil, err
}

list := make([]string, 0, len(files))
list := make([]*SchemaBody, 0, len(files))
for _, dirEntry := range files {
if !dirEntry.IsDir() && filepath.Ext(dirEntry.Name()) == ".json" {
list = append(list, filepath.Join(abdPath, dirEntry.Name()))
p := filepath.Join(abdPath, dirEntry.Name())
t := validate(p)
if t == nil {
fmt.Printf("[sesmate]: skip %s, because template is invalid.\n", filepath.Base(p))
continue
}

if info, err := os.Stat(p); err != nil {
fmt.Printf("[sesmate]: skip %s, because %s.\n", filepath.Base(p), err.Error())
continue
} else if info.Size() > 500*1024 {
fmt.Printf("[sesmate]: skip %s, because template size exceeds the limit of 500 KB.\n", filepath.Base(p))
continue
}

list = append(list, t)
}
}
if len(list) == 0 {
return nil, errors.New("no json file found")
}

result := make([]*SchemaBody, 0, len(list))

for _, p := range list {
t := validate(p)
if t == nil {
fmt.Printf("[sesmate]: skip %s, because template is invalid.\n", filepath.Base(p))
continue
}
result = append(result, t)
}
if len(result) == 0 {
return nil, errors.New("no template file found")
}
if len(list) > 10000 {
return nil, errors.New("the number of templates exceeds the limit of 10000")
}

return result, err
return list, err
}

func FindWithName(body []*SchemaBody, templateName string) *SchemaBody {
Expand Down

0 comments on commit a9b0551

Please sign in to comment.