-
Notifications
You must be signed in to change notification settings - Fork 1
/
disk_backend.go
89 lines (75 loc) · 1.82 KB
/
disk_backend.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
package main
import (
"image"
"io"
"io/ioutil"
"os"
)
type diskBackend struct {
Root string
}
func newDiskBackend(root string) diskBackend {
return diskBackend{Root: root}
}
func (d diskBackend) String() string {
return "Disk"
}
func (d diskBackend) WriteSized(img imageSpecifier, r io.ReadCloser) error {
path := img.baseDir(d.Root)
err := os.MkdirAll(path, 0755)
if err != nil {
return err
}
fullpath := img.sizedPath(d.Root)
f, err := os.OpenFile(fullpath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, r)
return err
}
func (d diskBackend) WriteFull(img imageSpecifier, r io.ReadCloser) error {
path := img.baseDir(d.Root)
err := os.MkdirAll(path, 0755)
if err != nil {
return err
}
fullpath := img.fullSizePath(d.Root)
f, err := os.OpenFile(fullpath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, r)
return err
}
func (d diskBackend) Read(img imageSpecifier) ([]byte, error) {
path := img.sizedPath(d.Root)
return ioutil.ReadFile(path)
}
func (d diskBackend) Exists(img imageSpecifier) bool {
path := img.sizedPath(d.Root)
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}
func (d diskBackend) Delete(img imageSpecifier) error {
path := img.sizedPath(d.Root)
return os.RemoveAll(path)
}
func (d diskBackend) writeLocalType(ri imageSpecifier, outputImage image.Image, encFunc encfunc) {
wFile, err := os.OpenFile(ri.sizedPath(d.Root), os.O_CREATE|os.O_RDWR, 0644)
defer wFile.Close()
if err != nil {
// what do we do if we can't write?
// we still have the resized image, so we can serve the response
// we just can't cache it.
return
}
encFunc(wFile, outputImage)
}
func (d diskBackend) fullPath(ri imageSpecifier) string {
return ri.fullSizePath(d.Root)
}