-
Notifications
You must be signed in to change notification settings - Fork 5
/
s3store_part_producer.go
80 lines (71 loc) · 1.57 KB
/
s3store_part_producer.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
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"time"
)
// s3PartProducer converts a stream of bytes from the reader into a stream of files on disk
type s3PartProducer struct {
temporaryDirectory string
files chan<- *os.File
done chan struct{}
err error
r io.Reader
}
func (spp *s3PartProducer) produce(totalSize, partSize int64) {
for {
if totalSize <= 0 {
close(spp.files)
return
}
file, err := spp.nextPart(partSize)
if err != nil {
spp.err = err
close(spp.files)
return
}
/*
if file == nil {
close(spp.files)
return
}
*/
select {
case spp.files <- file:
totalSize = totalSize - partSize
fmt.Println("----aaaa", totalSize)
case <-spp.done:
close(spp.files)
return
}
}
}
func (spp *s3PartProducer) nextPart(size int64) (*os.File, error) {
sm := time.Now()
defer func() {
em := time.Now()
fmt.Println("------------->nextPart, d:", size, em.Sub(sm))
}()
// Create a temporary file to store the part
file, err := ioutil.TempFile(spp.temporaryDirectory, "tusd-s3-tmp-")
if err != nil {
return nil, err
}
limitedReader := io.LimitReader(spp.r, size)
_, err = io.Copy(file, limitedReader)
if err != nil {
return nil, err
}
// If the entire request body is read and no more data is available,
// io.Copy returns 0 since it is unable to read any bytes. In that
// case, we can close the s3PartProducer.
//if n == 0 {
//cleanUpTempFile(file)
// return nil, nil
//}
// Seek to the beginning of the file
file.Seek(0, 0)
return file, nil
}