-
Notifications
You must be signed in to change notification settings - Fork 16
/
apps.go
42 lines (36 loc) · 936 Bytes
/
apps.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
package apps
import (
"fmt"
"os"
"regexp"
"strings"
)
func hasValidExtension(file string, exts []string) bool {
for _, ext := range exts {
if strings.HasSuffix(file, ext) {
return true
}
}
return false
}
// IsStorageID checks if a link is an entry of app-storage.
func IsStorageID(link string) bool {
re := regexp.MustCompile("^(storage:(//)?)?[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$")
if re.MatchString(link) {
return true
}
return false
}
// Validate validates that the apps is valid (storageID / File / URL).
func Validate(kind, app string, validExt []string, URLAllowed bool) error {
if IsStorageID(app) {
return nil
}
if !hasValidExtension(app, validExt) {
return fmt.Errorf("invalid %s file: %s, make sure extension is one of the following: %s", kind, app, strings.Join(validExt, ", "))
}
if _, err := os.Stat(app); err == nil {
return nil
}
return fmt.Errorf("%s: file not found", app)
}