-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta.go
56 lines (48 loc) · 1.18 KB
/
meta.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
package metadata
import (
"mime/multipart"
"time"
log "github.com/rauwekost/silo/Godeps/_workspace/src/github.com/Sirupsen/logrus"
"github.com/rauwekost/silo/storage"
)
var HeaderKeys = []string{
"Age",
"Content-Type",
"Last-Modified",
"Date",
"Etag",
}
type Metadata struct {
Name string `json:"name"`
CreatedAt time.Time `json:"created_at"`
ModifiedAt time.Time `json:"modified_at"`
Size int `json:"size"`
Headers map[string]string `json:"headers"`
Checksum string `json:"checksum"`
}
func New(f storage.File, t *multipart.FileHeader) *Metadata {
now := time.Now()
m := Metadata{
Name: t.Filename,
CreatedAt: now,
ModifiedAt: now,
Size: int(f.Size()),
Checksum: f.Checksum(),
}
headers := make(map[string]string, 0)
for _, key := range HeaderKeys {
if value, ok := t.Header[key]; ok && len(value) > 0 {
headers[key] = value[0]
}
}
m.Headers = headers
return &m
}
func (m *Metadata) ContentType() string {
typ, ok := m.Headers["Content-Type"]
if !ok {
log.Errorf("could not find content-type for: %s created-at: %s", m.Name, m.CreatedAt)
return ""
}
return typ
}