This repository was archived by the owner on Nov 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupload.go
More file actions
159 lines (146 loc) · 3.08 KB
/
upload.go
File metadata and controls
159 lines (146 loc) · 3.08 KB
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
package api
import (
"crypto/sha1"
"encoding/hex"
"io"
"log"
"code.google.com/p/go-uuid/uuid"
)
func Upload(id, collection, tag string, r io.Reader, c Context) (string, error) {
hashReader, hashWriter := io.Pipe()
uploadReader, uploadWriter := io.Pipe()
m := io.MultiWriter(hashWriter, uploadWriter)
hashChan := make(chan string)
hashError := make(chan error)
hashDone := make(chan struct{})
cpDone := make(chan struct{})
cpErr := make(chan error)
uploadDone := make(chan struct{})
uploadErr := make(chan error)
n := make(chan int64)
var bytesWritten int64
tmp := uuid.NewRandom().String()
tmp = "tmp/" + tmp
go hash(hashReader, hashChan, hashError, hashDone)
go asyncCopy(m, r, n, cpErr, cpDone)
if c.Storage != nil {
go c.Storage.Upload(c.Bucket, tmp, uploadReader, c, uploadErr, uploadDone)
}
select {
case err := <-hashError:
if err != nil {
hashWriter.CloseWithError(err)
uploadWriter.CloseWithError(err)
return "", err
} else {
hashWriter.Close()
uploadWriter.Close()
}
case err := <-cpErr:
if err != nil {
hashWriter.CloseWithError(err)
uploadWriter.CloseWithError(err)
return "", err
} else {
hashWriter.Close()
uploadWriter.Close()
}
case err := <-uploadErr:
if err != nil {
hashWriter.CloseWithError(err)
uploadWriter.CloseWithError(err)
return "", err
} else {
hashWriter.Close()
uploadWriter.Close()
}
case <-cpDone:
hashWriter.Close()
uploadWriter.Close()
}
<-cpDone
<-hashDone
if c.Storage != nil {
<-uploadDone
}
bytesWritten = <-n
finalLocation := <-hashChan
if c.Storage != nil {
err := c.Storage.Move(c.Bucket, tmp, c.Bucket, finalLocation, c)
if err != nil {
return "", err
}
}
if c.Datastore != nil {
err := c.Datastore.AddItemToCollection(collection, Item{
Blob: finalLocation,
Bucket: c.Bucket,
Tag: tag,
})
if err != nil {
return "", err
}
}
uBytes, uRequests := c.UsageTracker.TrackUploads(id)
uBytes <- bytesWritten
uRequests <- 1
return finalLocation, nil
}
func hash(r io.Reader, resp chan string, errs chan error, done chan struct{}) {
if resp != nil {
defer close(resp)
}
h := sha1.New()
go asyncCopy(h, r, nil, errs, done)
<-done
resp <- hex.EncodeToString(h.Sum(nil))
}
func del(bucket, tmp string, c Context) {
err := c.Storage.Delete(bucket, tmp)
if err != nil {
log.Printf("Error deleting temporary upload %s in %s: %s\n", tmp, bucket, err)
}
}
func asyncCopy(dst io.Writer, src io.Reader, n chan int64, errs chan error, done chan struct{}) {
if errs != nil {
defer close(errs)
}
if done != nil {
defer close(done)
}
buf := make([]byte, 32*1024)
var written int64
var err error
for {
nr, er := src.Read(buf)
if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
if nw > 0 {
written += int64(nw)
}
if ew != nil {
err = ew
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
}
if er == io.EOF {
break
}
if er != nil {
err = er
break
}
}
if errs != nil && err != nil {
errs <- err
}
if n != nil {
go func(n chan int64, written int64) {
n <- written
}(n, written)
}
}