-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
75 lines (61 loc) · 1.49 KB
/
cache.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
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
// FileInCache returns if the file is in the cache path and if it is also returns the format of the image
func FileInCache(filename string, cfg *Config) (bool, string, error) {
files, err := ioutil.ReadDir(cfg.CachePath)
if err != nil {
return false, "", err
}
for _, file := range files {
fileParts := strings.Split(file.Name(), ".")
if fileParts[0] == filename {
if cfg.Verbose {
fmt.Println("vermittler: file `" + filename + "." + fileParts[1] + "` exists in cache")
}
return true, fileParts[1], nil
}
}
return false, "", nil
}
// LoadImage loads an image from the cache
func LoadImage(filename string, cfg *Config) (*Image, error) {
var err error
img := &Image{
Width: -1,
Height: -1,
Blur: -1.0,
}
f, err := os.Open(cfg.CachePath + "/" + filename)
if err != nil {
return nil, err
}
defer f.Close()
formatStrings := strings.Split(filename, ".")
img.Format = formatStrings[len(formatStrings)-1]
err = img.readImage(f, false)
if err != nil {
return nil, err
}
if cfg.Verbose {
fmt.Println("vermittler: loading image `" + filename + "` from cache")
}
return img, nil
}
// CacheFile stores the file in the cache path
func CacheFile(file string, img *Image, cfg *Config) {
f, err := os.Create(cfg.CachePath + "/" + file)
if err == nil {
defer f.Close()
img.Write(f)
if cfg.Verbose {
fmt.Println("vermittler: storing file `" + file + "` in cache")
}
} else {
panic(err.Error())
}
}