-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
submission.go
148 lines (120 loc) · 3.97 KB
/
submission.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package experimental
import (
"bytes"
"encoding/json"
"errors"
"image"
_ "image/png"
"regexp"
"strings"
"github.com/hashicorp/go-version"
"github.com/the-egg-corp/thundergo/util"
)
type PackageSubmissionMetadata struct {
UUID string `json:"upload_uuid"`
Author string `json:"author_name"`
Communities []string `json:"communities"`
CommunityCategories []string `json:"community_categories"`
Categories []string `json:"categories"`
HasNsfwContent bool `json:"has_nsfw_content"`
}
type ManifestMetadata struct {
Name string `json:"name"`
VersionNumber string `json:"version_number"`
WebsiteURL *string `json:"website_url"`
Description string `json:"description"`
Dependencies []string `json:"dependencies"`
}
type IconValidatorParams struct {
FileName string
ImageData []byte
}
func NewErr(msg string) error {
return errors.New(msg)
}
// TODO: Implement this
func SubmitPackage(data []byte) (bool, error) {
return false, nil
}
// TODO: Implement this
func ValidateReadme(data []byte) (bool, error) {
return false, nil
}
func ValidateManifest(author string, data []byte) (bool, []string, error) {
var manifest ManifestMetadata
var errors []string
err := json.Unmarshal(data, &manifest)
if err != nil {
errors = append(errors, err.Error())
}
pkg, _ := GetPackage(author, manifest.Name)
if pkg == nil {
return false, nil, NewErr("package not found under the specified author")
}
AddIfEmpty(&errors, &manifest.Name, "required property 'name' is empty or unspecified")
AddIfInvalid(&errors, &manifest.Name, "property 'name' must contain only valid characters (a-z A-Z 0-9 _)")
AddIfEmpty(&errors, &manifest.Description, "required property 'description' is empty or unspecified")
verEmpty := AddIfEmpty(&errors, &manifest.VersionNumber, "required property 'version_number' is empty or unspecified")
if !verEmpty {
sv, _ := util.CheckSemVer(manifest.VersionNumber)
AddIfFalse(&errors, &sv, "property 'version_number' does not follow semantic versioning (major.minor.patch)")
if !sv {
return false, errors, nil
}
verA, _ := version.NewSemver(manifest.VersionNumber)
verB, _ := version.NewSemver(pkg.Latest.VersionNumber)
if verA.LessThanOrEqual(verB) {
errors = append(errors, "property 'version_number' must be higher than the latest")
}
}
if manifest.WebsiteURL == nil {
errors = append(errors, "required property 'website_url' is unspecified")
} else {
url := strings.ToLower(*manifest.WebsiteURL)
if !(strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://")) {
errors = append(errors, "property 'website_url' must be a valid URL")
}
}
if manifest.Dependencies == nil {
errors = append(errors, "manifest property 'dependencies' is required")
}
return len(errors) < 1, errors, nil
}
func AddIfEmpty(arr *[]string, str *string, errStr string) bool {
empty := *str == "" || str == nil
if empty {
*arr = append(*arr, errStr)
}
return empty
}
func AddIfFalse(arr *[]string, val *bool, errStr string) {
if !*val {
*arr = append(*arr, errStr)
}
}
func AddIfInvalid(arr *[]string, str *string, errStr string) {
matched, _ := regexp.MatchString(`^[a-zA-Z0-9_]+$`, *str)
if !matched {
*arr = append(*arr, errStr)
}
}
// Decodes image data and validates that the image is a PNG and the dimensions are 256x256.
//
// Additionally, if the file name is specified, it will validate that it is named correctly.
func ValidateIcon(params IconValidatorParams) (bool, error) {
if params.FileName != "" && params.FileName != "icon.png" {
return false, errors.New("image name provided did not match: icon.png")
}
// Decode data into the Image type.
img, _, err := image.Decode(bytes.NewReader(params.ImageData))
if err != nil {
return false, err
}
bounds := img.Bounds()
width, height := bounds.Dx(), bounds.Dy()
// Verify dimensions
if width == 256 && height == 256 {
return true, nil
}
return false, errors.New("image dimensions did not match: 256x256")
}