/
main.go
162 lines (146 loc) · 3.34 KB
/
main.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"bytes"
"crypto/md5"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"github.com/caiguanhao/ossslim"
)
var (
client ossslim.Client
dryrun bool
nomd5 bool
)
func main() {
var createConfig bool
var configFile string
var extsIgnore list
flag.BoolVar(&createConfig, "C", false, "create config file and exit")
flag.StringVar(&configFile, "c", "oss.config", "config file location")
flag.BoolVar(&dryrun, "n", false, "show only URLs, don't upload")
flag.BoolVar(&nomd5, "nomd5", false, "do not compute md5")
flag.Var(&extsIgnore, "noext", "file extensions to ignore (for example -noext html)")
flag.Parse()
if createConfig {
if err := writeConfig(configFile, &config{
OSSAccessKeyId: "LTAIxxxxxxxxxxxxxxxxxxxx",
OSSAccessKeySecret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
OSSPrefix: "https://xxxxxxxx.oss-cn-hangzhou.aliyuncs.com",
OSSBucket: "xxxxxxxx",
}); err != nil {
log.Fatalln(err)
}
log.Println("created config file:", configFile)
return
}
var currentConfig config
if err := readConfig(configFile, ¤tConfig); err != nil {
log.Fatalln(err)
}
args := flag.Args()
if len(args) != 1 {
log.Fatalln("must provide only one directory")
}
root := args[0]
client = ossslim.Client{
AccessKeyId: currentConfig.OSSAccessKeyId,
AccessKeySecret: currentConfig.OSSAccessKeySecret,
Prefix: currentConfig.OSSPrefix,
Bucket: currentConfig.OSSBucket,
}
jobs := make(chan string)
go func() {
defer close(jobs)
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return nil
}
ext := strings.TrimPrefix(filepath.Ext(path), ".")
if extsIgnore.Has(ext) {
return nil
}
name, err := filepath.Rel(root, path)
if err != nil {
return err
}
jobs <- name
return nil
})
if err != nil {
log.Fatalln(err)
}
}()
concurrency := runtime.NumCPU()
var wg sync.WaitGroup
wg.Add(concurrency)
for i := 0; i < concurrency; i++ {
go func() {
defer wg.Done()
for path := range jobs {
upload(root, path)
}
}()
}
wg.Wait()
}
func upload(root, path string) {
contentType := contentTypeForExtension(filepath.Ext(path))
if dryrun {
fmt.Printf("%s (%s)\n", client.URL(path), contentType)
return
}
var buffer bytes.Buffer
file, err := os.Open(filepath.Join(root, path))
if err != nil {
log.Fatalln(err)
return
}
defer file.Close()
if nomd5 == false {
md5sum := md5.New()
n, err := io.Copy(io.MultiWriter(&buffer, md5sum), file)
if err != nil {
log.Fatalln(err)
return
}
req, err := client.Upload(path, &buffer, md5sum.Sum(nil), contentType)
if err != nil {
log.Fatalln("failed to upload to", req.URL(), err)
return
}
log.Printf("uploaded to %s (%d bytes)\n", req.URL(), n)
} else {
req, err := client.Upload(path, file, nil, contentType)
if err != nil {
log.Fatalln("failed to upload to", req.URL(), err)
return
}
log.Printf("uploaded to %s\n", req.URL())
}
}
type list []string
func (s list) String() string {
return strings.Join(s, ", ")
}
func (s *list) Set(value string) error {
*s = append(*s, value)
return nil
}
func (s list) Has(str string) bool {
for _, element := range s {
if element == str {
return true
}
}
return false
}