/
upload_source.go
93 lines (75 loc) · 1.55 KB
/
upload_source.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
package storage
import (
"errors"
"io"
"os"
)
type UploadSource interface {
Size() int64
Rewindable() bool
Rewind() error
}
func NewUploadSourceReader(reader io.Reader, size int64) (UploadSource, error) {
return &uploadSourceReader{
reader: reader,
size: size,
}, nil
}
type uploadSourceReader struct {
reader io.Reader
size int64
}
func (u *uploadSourceReader) Rewindable() bool {
return false
}
func (u *uploadSourceReader) Rewind() error {
return errors.New("resource not support rewind")
}
func (u *uploadSourceReader) Size() int64 {
return u.size
}
func NewUploadSourceReaderAt(reader io.ReaderAt, size int64) (UploadSource, error) {
if size <= 0 {
return nil, errors.New("source size must be set")
}
return &uploadSourceReaderAt{
reader: reader,
size: size,
}, nil
}
type uploadSourceReaderAt struct {
reader io.ReaderAt
size int64
}
func (u *uploadSourceReaderAt) Rewindable() bool {
return true
}
func (u *uploadSourceReaderAt) Rewind() error {
return nil
}
func (u *uploadSourceReaderAt) Size() int64 {
return u.size
}
func NewUploadSourceFile(filePath string) (UploadSource, error) {
if fileInfo, err := os.Stat(filePath); err != nil {
return nil, err
} else {
return &uploadSourceFile{
fileInfo: fileInfo,
filePath: filePath,
}, nil
}
}
type uploadSourceFile struct {
filePath string
fileInfo os.FileInfo
}
func (u *uploadSourceFile) Rewindable() bool {
return true
}
func (u *uploadSourceFile) Rewind() error {
return nil
}
func (u *uploadSourceFile) Size() int64 {
return u.fileInfo.Size()
}