-
Notifications
You must be signed in to change notification settings - Fork 2
/
spaces.go
76 lines (62 loc) · 1.35 KB
/
spaces.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
package airlock
import (
"fmt"
"math/rand"
"time"
"github.com/goamz/goamz/s3"
)
func randomString(n int) string {
const letterBytes = "abcdefghijklmnopqrstuvwxyz"
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}
func (a *Airlock) MakeSpace() error {
rand.Seed(time.Now().UTC().UnixNano())
cleanName := a.name
preRandLength := SpaceNameMaxLength - SpaceNameRandLength - 1
if len(cleanName) > preRandLength {
cleanName = cleanName[:preRandLength]
}
for {
spaceName := fmt.Sprintf("%s-%s", cleanName, randomString(SpaceNameRandLength))
if a.DryRun {
a.space = &s3.Bucket{
Name: spaceName,
}
return nil
}
space := a.Spaces.Bucket(spaceName)
err := space.PutBucket(s3.Private)
if err != nil {
if serr, ok := err.(*s3.Error); ok && serr.Code == "BucketAlreadyExists" {
continue
} else {
return err
}
} else {
a.space = space
return nil
}
}
}
func (a *Airlock) SpaceName() string {
return a.space.Name
}
func (a *Airlock) uploadFile(f File) error {
// do nothing on dry run
if a.DryRun {
time.Sleep(time.Millisecond * 300)
return nil
}
if f.IsDir {
return nil
}
contentType, err := f.ContentType()
if err != nil {
return err
}
return a.space.PutReader(f.RelPath, f.Reader, f.Size, contentType, s3.PublicRead, s3.Options{})
}