-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_files_uploader.go
96 lines (84 loc) · 2.1 KB
/
static_files_uploader.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
package main
import (
"bytes"
"context"
"net/http"
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
func staticSiteUploader(DeploymenClient *DeploymentBackend) {
var (
localPath = "web/public/"
bucket = DeploymenClient.Bucket
)
walker := make(fileWalk)
go func() {
// Gather the files to upload by walking the path recursively
if err := filepath.Walk(localPath, walker.Walk); err != nil {
log.Fatalln("Walk failed:", err)
}
close(walker)
}()
// For each file found walking, upload it to Amazon S3
for path := range walker {
rel, err := filepath.Rel(localPath, path)
if err != nil {
log.Fatalln("Unable to get relative path:", path, err)
}
file, err := os.Open(path)
if err != nil {
log.Println("Failed opening file", path, err)
continue
}
fileInfo, _ := file.Stat()
var fileSize int64 = fileInfo.Size()
buf := make([]byte, fileSize)
_, err = file.Read(buf)
contentType := http.DetectContentType(buf)
ext := filepath.Ext(path)
if ext == ".css" {
contentType = "text/css;"
}
if ext == ".svg" {
contentType = "image/svg+xml;"
}
if ext == ".js" {
contentType = "application/javascript;"
}
if ext == ".xml" {
contentType = "application/xml;"
}
if err != nil {
log.Fatalln("File bytes empty", path, err)
}
client := DeploymenClient.Client
uploader := manager.NewUploader(client)
result, err := uploader.Upload(context.TODO(), &s3.PutObjectInput{
Bucket: &bucket,
Key: aws.String(rel),
Body: bytes.NewReader(buf),
ContentType: aws.String(contentType),
})
log.Debug(contentType)
file.Close()
if err != nil {
log.Fatalln("Failed to upload", path, err)
}
log.Debugln("Uploaded", path, result.Location)
}
log.Infoln("Successfully uploaded built static site to the bucket")
}
type fileWalk chan string
func (f fileWalk) Walk(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
f <- path
}
return nil
}