-
Notifications
You must be signed in to change notification settings - Fork 31
/
artifact.go
99 lines (80 loc) · 1.76 KB
/
artifact.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
package artifact
import (
"bytes"
"io"
"mime"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/mitchellh/goamz/s3"
)
const (
defaultCtype = "application/octet-stream"
)
// Artifact is the thing that gets uploaded or whatever
type Artifact struct {
RepoSlug string
BuildNumber string
BuildID string
JobNumber string
JobID string
Source string
Dest string
Prefix string
Perm s3.ACL
UploadResult *Result
}
// New creates a new *Artifact
func New(prefix, source, dest string, opts *Options) *Artifact {
return &Artifact{
Prefix: prefix,
Source: source,
Dest: dest,
RepoSlug: opts.RepoSlug,
BuildNumber: opts.BuildNumber,
BuildID: opts.BuildID,
JobNumber: opts.JobNumber,
JobID: opts.JobID,
Perm: opts.Perm,
UploadResult: &Result{},
}
}
// ContentType makes it easier to find the perfect match
func (a *Artifact) ContentType() string {
ctype := mime.TypeByExtension(path.Ext(a.Source))
if ctype != "" {
return ctype
}
f, err := os.Open(a.Source)
if err != nil {
return defaultCtype
}
var buf bytes.Buffer
_, err = io.CopyN(&buf, f, int64(512))
if err != nil && err != io.EOF {
return defaultCtype
}
return http.DetectContentType(buf.Bytes())
}
// Reader makes an io.Reader out of the filepath
func (a *Artifact) Reader() (io.Reader, error) {
f, err := os.Open(a.Source)
if err != nil {
return nil, err
}
return f, nil
}
// Size reports the size of the artifact
func (a *Artifact) Size() (uint64, error) {
fi, err := os.Stat(a.Source)
if err != nil {
return uint64(0), nil
}
return uint64(fi.Size()), nil
}
// FullDest calculates the full remote destination path
func (a *Artifact) FullDest() string {
return strings.TrimLeft(filepath.Join(a.Prefix, a.Dest), "/")
}