-
Notifications
You must be signed in to change notification settings - Fork 0
/
dehydrate.go
154 lines (125 loc) · 3.23 KB
/
dehydrate.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package cli
import (
"github.com/beauxarts/fedorov/data"
"github.com/beauxarts/scrinium/litres_integration"
"github.com/boggydigital/issa"
"github.com/boggydigital/kvas"
"github.com/boggydigital/nod"
"image"
"image/color"
_ "image/jpeg"
"net/url"
"os"
"strconv"
"strings"
"time"
)
func DehydrateHandler(u *url.URL) error {
idSet := make(map[string]bool)
if idstr := u.Query().Get("id"); idstr != "" {
for _, id := range strings.Split(idstr, ",") {
idSet[id] = true
}
}
all := u.Query().Has("all")
overwrite := u.Query().Has("overwrite")
return Dehydrate(idSet, all, overwrite)
}
func Dehydrate(idSet map[string]bool, all, overwrite bool) error {
di := nod.Begin("dehydrating images...")
defer di.End()
rdx, err := data.NewReduxWriter(
data.DehydratedListImageProperty,
data.DehydratedListImageModifiedProperty,
data.DehydratedItemImageProperty,
data.DehydratedItemImageModifiedProperty,
data.ArtsHistoryOrderProperty)
if err != nil {
return di.EndWithError(err)
}
if all {
if all, ok := rdx.GetAllValues(data.ArtsHistoryOrderProperty, data.ArtsHistoryOrderProperty); ok {
for _, id := range all {
idSet[id] = true
}
}
}
if err := dehydrateImages(
idSet,
rdx,
data.DehydratedItemImageProperty,
data.DehydratedItemImageModifiedProperty,
data.CoverSizesDesc,
overwrite); err != nil {
return di.EndWithError(err)
}
if err := dehydrateImages(
idSet,
rdx,
data.DehydratedListImageProperty,
data.DehydratedListImageModifiedProperty,
data.CoverSizesAsc,
overwrite); err != nil {
return di.EndWithError(err)
}
return nil
}
func dehydrateImages(
idSet map[string]bool,
rdx kvas.WriteableRedux,
imageProperty, modifiedProperty string,
sizes []litres_integration.CoverSize,
overwrite bool) error {
di := nod.NewProgress(" dehydrating %s...", imageProperty)
defer di.End()
di.TotalInt(len(idSet))
plt := issa.StdPalette()
dehydratedImages := make(map[string][]string)
dehydratedImageModified := make(map[string][]string)
for idStr := range idSet {
if _, ok := rdx.GetFirstVal(imageProperty, idStr); ok && !overwrite {
continue
}
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
nod.Log(err.Error())
continue
}
for _, size := range sizes {
acp, err := data.AbsCoverImagePath(id, size)
if err != nil {
return di.EndWithError(err)
}
if dhi, err := dehydrateImage(acp, plt); err == nil {
dehydratedImages[idStr] = []string{dhi}
dehydratedImageModified[idStr] = []string{strconv.FormatInt(time.Now().Unix(), 10)}
// stop dehydrating at the best quality available
break
} else {
nod.Log(err.Error())
}
}
di.Increment()
}
if err := rdx.BatchReplaceValues(imageProperty, dehydratedImages); err != nil {
return di.EndWithError(err)
}
if err := rdx.BatchReplaceValues(modifiedProperty, dehydratedImageModified); err != nil {
return di.EndWithError(err)
}
return nil
}
func dehydrateImage(absImagePath string, plt color.Palette) (string, error) {
dhi := ""
fi, err := os.Open(absImagePath)
if err != nil {
return dhi, err
}
defer fi.Close()
img, _, err := image.Decode(fi)
if err != nil {
return dhi, err
}
gif := issa.GIFImage(img, plt, issa.DefaultDownSampling)
return issa.Dehydrate(gif)
}