/
file.go
53 lines (43 loc) · 924 Bytes
/
file.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
package imagor
import (
"io/ioutil"
)
type File struct {
Path string
Meta *Meta
buf []byte
}
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 NewFilePath(filepath string) *File {
return &File{Path: filepath}
}
func NewFileBytes(bytes []byte) *File {
return &File{buf: bytes}
}
func NewFileBytesWithMeta(bytes []byte, meta *Meta) *File {
return &File{buf: bytes, Meta: meta}
}
func (f *File) IsEmpty() bool {
return f.Path == "" && len(f.buf) == 0
}
func (f *File) HasPath() bool {
return f.Path != ""
}
func (f *File) Bytes() ([]byte, error) {
if len(f.buf) > 0 {
return f.buf, nil
}
if f.Path != "" {
return ioutil.ReadFile(f.Path)
}
return nil, ErrNotFound
}
func IsFileEmpty(f *File) bool {
return f == nil || f.IsEmpty()
}