-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
81 lines (65 loc) · 1.63 KB
/
util.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
package s3
import (
"context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/vcraescu/bucketly"
"strings"
)
func directorize(s string) string {
s = strings.TrimSpace(s)
if s == "." {
return string(pathSeparator)
}
if strings.HasSuffix(s, string(pathSeparator)) {
return s
}
return s + string(pathSeparator)
}
func waitUntilKeyExists(ctx context.Context, svc *s3.S3, bucket, key string) error {
return svc.WaitUntilObjectExistsWithContext(ctx, &s3.HeadObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
}
func waitUntilKeyNotExists(ctx context.Context, svc *s3.S3, bucket, key string) error {
return svc.WaitUntilObjectNotExistsWithContext(ctx, &s3.HeadObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
}
func isNotExists(err error) bool {
err1, ok := err.(awserr.RequestFailure)
return ok && err1.StatusCode() == 404
}
func isDirPath(name string) bool {
name = strings.TrimSpace(name)
if name == "" || name == "." {
name = string(pathSeparator)
}
return strings.HasSuffix(name, string(pathSeparator))
}
func cleanPath(b bucketly.PathSeparable, name string) string {
name = strings.TrimSpace(name)
isDir := isDirPath(name)
name = bucketly.Clean(b, name)
if name == "." {
name = ""
}
if isDir {
name = directorize(name)
}
return name
}
func sanitzePath(b bucketly.PathSeparable, name string) (string, error) {
isDir := isDirPath(name)
name, err := bucketly.Sanitize(b, name)
if err != nil {
return "", err
}
if isDir && name != "/" {
name += string(b.PathSeparator())
}
return name, nil
}