-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathutils.go
63 lines (53 loc) · 1.66 KB
/
utils.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
package handler
import (
"strings"
"github.com/content-services/content-sources-backend/pkg/api"
"github.com/content-services/content-sources-backend/pkg/rbac"
"github.com/content-services/content-sources-backend/pkg/utils"
"github.com/labstack/echo/v4"
)
func GetHeader(c echo.Context, key string, defvalues []string) []string {
val, ok := c.Request().Header[key]
if !ok {
return defvalues
}
return val
}
func removeEndSuffix(source string, suffix string) string {
output := source
j := len(source) - 1
for j > 0 && strings.HasSuffix(output, suffix) {
output = strings.TrimSuffix(output, suffix)
}
return output
}
func addRepoRoute(e *echo.Group, method string, path string, h echo.HandlerFunc, verb rbac.Verb, m ...echo.MiddlewareFunc) {
e.Add(method, path, h, m...)
rbac.ServicePermissions.Add(method, path, rbac.ResourceRepositories, verb)
}
func addTemplateRoute(e *echo.Group, method string, path string, h echo.HandlerFunc, verb rbac.Verb, m ...echo.MiddlewareFunc) {
e.Add(method, path, h, m...)
rbac.ServicePermissions.Add(method, path, rbac.ResourceTemplates, verb)
}
func preprocessInput(input *api.ContentUnitSearchRequest) {
if input == nil {
return
}
for i, url := range input.URLs {
input.URLs[i] = removeEndSuffix(url, "/")
}
if input.Limit == nil {
input.Limit = utils.Ptr(api.ContentUnitSearchRequestLimitDefault)
}
if *input.Limit > api.ContentUnitSearchRequestLimitMaximum {
*input.Limit = api.ContentUnitSearchRequestLimitMaximum
}
}
func extractUploadUuid(href string) string {
uuid := strings.TrimSuffix(href, "/")
lastIndex := strings.LastIndex(uuid, "/")
if lastIndex == -1 {
return ""
}
return uuid[lastIndex+1:]
}