-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpackage.go
108 lines (96 loc) · 2 KB
/
package.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
package sapt
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
type Package struct {
Path string
Content []byte
Metadata *packageMetadata
}
func UploadPackages(file *os.File, rm bool, conn *S3) {
stat, _ := file.Stat()
path := file.Name()
fileList := []string{}
if stat.IsDir() {
filepath.Walk(path, func(p string, f os.FileInfo, err error) error {
if !f.IsDir() && filepath.Ext(p) == ".deb" {
fileList = append(fileList, p)
}
return nil
})
} else if filepath.Ext(path) == ".deb" {
fileList = append(fileList, path)
}
file.Close()
var wg sync.WaitGroup
wg.Add(len(fileList))
uploadChan := make(chan *Package, 10)
for _, f := range fileList {
go func(f string) {
uploadChan <- NewPackage(f, path)
}(f)
}
for i := 0; i < len(fileList); i++ {
go func() {
for {
pkg, ok := <-uploadChan
if !ok {
break
}
conn.uploadPackage(pkg)
time.Sleep(time.Millisecond * 100)
wg.Done()
}
}()
}
wg.Wait()
}
func NewPackage(path string, basePath string) *Package {
var name string
if path == basePath {
name = basename(path)
} else {
name = strings.Replace(path, basePath, "", 1)
}
// read in file contents
content, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal(err)
}
// retrieve metadata
metadata := metadataFromDeb(path)
metadata.Filename = name
metadata.MD5sum = hash(content, "md5")
metadata.SHA1 = hash(content, "sha1")
metadata.SHA256 = hash(content, "sha256")
return &Package{
Path: name,
Content: content,
Metadata: metadata,
}
}
func hash(content []byte, crypto string) string {
var hash string
if crypto == "md5" {
hash = fmt.Sprintf("%x", md5.Sum(content))
} else if crypto == "sha1" {
hash = fmt.Sprintf("%x", sha1.Sum(content))
} else if crypto == "sha256" {
hash = fmt.Sprintf("%x", sha256.Sum256(content))
}
return hash
}
func basename(path string) string {
i := strings.LastIndex(path, "/")
return path[i:]
}