-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathticket.go
58 lines (52 loc) · 1.18 KB
/
ticket.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
package main
import (
"crypto/sha256"
"fmt"
"hash"
"io"
"net/mail"
"os"
"path/filepath"
)
const digest_length = 20
type ArchiveTicket struct {
hasher hash.Hash
digest [digest_length]byte
filename string
header mail.Header
}
func (arch *ArchiveTicket) Submit() (name string, err error) {
if e := arch.Hash(); e != nil {
panic(e)
}
first_byte := fmt.Sprintf("%02x", arch.digest[0])
rest_bytes := fmt.Sprintf("%02x", arch.digest[1:])
name = filepath.Join(*targetdir, first_byte, rest_bytes)
if e := os.MkdirAll(filepath.Join(*targetdir, first_byte), os.ModePerm); e != nil {
panic(e)
} else if outf, e := os.Create(name); e != nil {
panic(e)
} else if inf, e := os.Open(arch.filename); e != nil {
panic(e)
} else if _, e := io.Copy(outf, inf); e != nil {
panic(e)
} else if e := inf.Close(); e != nil {
panic(e)
} else if e := outf.Close(); e != nil {
panic(e)
}
return
}
// make sure hasher is reset before calling Hash()
func (a *ArchiveTicket) Hash() error {
if a.hasher == nil {
a.hasher = sha256.New()
}
if _, e := WriteHeaders(a.header, a.hasher); e != nil {
return e
} else {
copy(a.digest[:], a.hasher.Sum(nil))
}
a.hasher.Reset()
return nil
}