forked from cshum/imagor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblob.go
66 lines (55 loc) · 1.14 KB
/
blob.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
package imagor
import (
"io/ioutil"
"sync"
)
// Blob abstraction for file path, bytes data and meta attributes
type Blob struct {
path string
buf []byte
once sync.Once
err error
Meta *Meta
}
// Meta image attributes
type Meta struct {
Format string `json:"format"`
ContentType string `json:"content_type"`
Width int `json:"width"`
Height int `json:"height"`
Orientation int `json:"orientation"`
}
func NewBlobFilePath(filepath string) *Blob {
return &Blob{path: filepath}
}
func NewBlobBytes(bytes []byte) *Blob {
return &Blob{buf: bytes}
}
func NewBlobBytesWithMeta(bytes []byte, meta *Meta) *Blob {
return &Blob{buf: bytes, Meta: meta}
}
func (b *Blob) readAllOnce() {
b.once.Do(func() {
if len(b.buf) > 0 {
return
}
if b.path != "" {
b.buf, b.err = ioutil.ReadFile(b.path)
}
if len(b.buf) == 0 && b.err == nil {
b.buf = nil
b.err = ErrNotFound
return
}
})
}
func (b *Blob) IsEmpty() bool {
return b.path == "" && len(b.buf) == 0
}
func (b *Blob) ReadAll() ([]byte, error) {
b.readAllOnce()
return b.buf, b.err
}
func IsBlobEmpty(f *Blob) bool {
return f == nil || f.IsEmpty()
}