-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thumbnails_unix.go
290 lines (265 loc) · 7.2 KB
/
thumbnails_unix.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//go:build unix && !android && !ios
package thumbnails
import (
"bytes"
"crypto/md5"
"errors"
"fmt"
"image"
"image/png"
"io"
"net/url"
"os"
"path/filepath"
"strings"
"golang.org/x/exp/slices"
pngstructure "github.com/dsoprea/go-png-image-structure/v2"
"github.com/nfnt/resize"
)
const (
software = `go-icon-setter`
fileURLScheme = `file://`
)
var (
ps string = string(os.PathSeparator)
scales = []string{`xx-large`, `x-large`, `large`, `normal`}
)
func OpenThumbnail(filename string, size image.Point, runThumbnailer bool) (image.Image, error) {
portable := false
img, _, err := openThumbnail(filename, size, runThumbnailer, portable)
return img, err
}
func openThumbnail(filename string, size image.Point, runThumbnailer, portable bool) (image.Image, string, error) {
f, err := os.Stat(filename)
if err != nil {
return nil, ``, err
}
if (size.X > 0 && size.Y > 0) || (size.X < 1 && size.Y < 1) {
return nil, ``, errors.New(`either size.X or size.Y has to be 0`)
}
mTime := fmt.Sprintf(`%d`, f.ModTime().Unix())
md5Sum, fileURL := hashFilename(filename, portable)
var thumbnailDirBase string
if portable {
thumbnailDirBase = filepath.Join(filepath.Dir(filename), `.sh_thumbnails`)
} else {
if xch := os.Getenv(`XDG_CACHE_HOME`); len(xch) > 0 {
thumbnailDirBase = xch
} else if home := os.Getenv(`HOME`); len(home) > 0 {
thumbnailDirBase = filepath.Join(home, `.cache`)
} else {
return nil, ``, errors.New(`unable to determine cache directory`)
}
thumbnailDirBase = filepath.Join(thumbnailDirBase, `thumbnails`)
}
var scale string
switch {
case size.X >= 512 || size.Y >= 512:
scale = `xx-large`
case size.X >= 256 || size.Y >= 256:
scale = `x-large`
case size.X >= 128 || size.Y >= 128:
scale = `large`
default:
scale = `normal`
}
sc := scale
thumbnailDir := filepath.Join(thumbnailDirBase, sc)
thumbnailFilename := filepath.Join(thumbnailDir, md5Sum+`.png`)
var img image.Image
if _, err := os.Stat(thumbnailFilename); os.IsNotExist(err) {
// write thumbnail
m, err := writeImage(filename, fileURL, thumbnailDirBase, md5Sum, sc, mTime, runThumbnailer)
if err != nil {
return nil, ``, err
}
if m != nil {
img = m
goto resize
}
} else {
t, err := newThumbnailFile(thumbnailFilename)
if err != nil {
return nil, ``, errors.New(`unable to open thumbnail image`)
}
mTimeThumbnail, err := t.mTimeStr()
if err != nil {
return nil, ``, errors.New(`unable to get modification time of thumbnail image`)
}
if mTime != mTimeThumbnail {
// rewrite thumbnail
m, err := writeImage(filename, fileURL, thumbnailDirBase, md5Sum, sc, mTime, runThumbnailer)
if err != nil {
return nil, ``, err
}
if m != nil {
img = m
goto resize
}
} else {
// file already exists
fi, err := os.Open(thumbnailFilename)
if err != nil {
return nil, ``, err
}
defer fi.Close()
m, _, err := image.Decode(fi)
if err != nil {
return nil, ``, err
}
if m != nil {
img = m
goto resize
}
}
}
if img == nil {
return nil, ``, errors.New(`unable to create thumbnail`)
}
resize:
if size.X > 1 {
img = resize.Resize(uint(size.X), 0, img, resize.Lanczos3)
} else {
img = resize.Resize(0, uint(size.Y), img, resize.Lanczos3)
}
return img, thumbnailFilename, nil
}
func hashFilename(filename string, portable bool) (md5hash, thumbnailFileURL string) {
fileURL := fileURLScheme
if portable {
// this makes the file path relative and the created file uri might be invalid(?)
// see https://specifications.freedesktop.org/thumbnail-spec/thumbnail-spec-0.8.0.html#SHARED // TODO rm
// see https://specifications.freedesktop.org/thumbnail-spec/thumbnail-spec-latest.html#SHARED
filename = filepath.Base(filename)
}
for _, r := range filename {
switch {
// needs checking!
case (r >= 'a' && r <= 'z') || (r >= '@' && r <= 'Z') || (r >= '&' && r <= ':') || r == '!' || r == '$' || r == '=' || r == '_' || r == '~':
fileURL += string(r)
case r == ' ':
fileURL += `%20` // url.QueryEscape would escape ' ' as '+'
default:
fileURL += url.QueryEscape(string(r))
}
}
h := md5.New()
io.WriteString(h, fileURL)
md5sum := fmt.Sprintf(`%x`, h.Sum(nil))
return md5sum, fileURL
}
func writeImage(filename, fileURL, thumbnailDirBase, md5Sum, scale, mTime string, runThumbnailer bool) (image.Image, error) {
// TODO look if larger thumbnails exist before creating one
var softwareStr string
thumbnailFilename := filepath.Join(thumbnailDirBase, scale, md5Sum+`.png`)
thumbnailDir := filepath.Join(thumbnailDirBase, scale)
{
o := strings.Split(os.Args[0], ps)
softwareStr = software + `::` + o[len(o)-1]
}
// var alpha uint8 = 0xff // enforced
if err := os.MkdirAll(thumbnailDir, 0700); err != nil {
return nil, err
}
var img image.Image
var buf bytes.Buffer
if runThumbnailer {
sz := 1 << (10 - slices.Index(scales, scale)) // 128, 256, 512, 1024
mimeType, err := runNativeThumbnailer(filename, fileURL, thumbnailFilename, uint(sz))
if err != nil {
switch mimeType {
case `image/png`,
`image/jpeg`,
`image/bmp`,
`image/gif`,
`image/tiff`,
`image/webp`:
default:
return nil, err
}
} else {
ft, err := os.Open(thumbnailFilename)
if err != nil {
return nil, err
}
defer ft.Close()
if _, err := io.Copy(&buf, ft); err != nil {
return nil, err
}
if _, err := ft.Seek(0, io.SeekStart); err != nil {
return nil, err
}
img, _, err = image.Decode(ft)
if err != nil {
return nil, err
}
_ = ft.Close()
// TODO check if correctly sized
}
}
// add exif tags if natively created
if img == nil {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
m, _, err := image.Decode(f)
if err != nil {
return nil, err
}
_ = f.Close()
img = m
bounds := img.Bounds()
sz := 1 << (10 - slices.Index(scales, scale)) // 128, 256, 512, 1024
if bounds.Dx() >= bounds.Dy() {
img = resize.Resize(uint(sz), 0, img, resize.Lanczos3)
} else {
img = resize.Resize(0, uint(sz), img, resize.Lanczos3)
}
if err := png.Encode(&buf, img); err != nil {
return nil, err
}
}
pmp := pngstructure.NewPngMediaParser()
csTmp, err := pmp.ParseBytes(buf.Bytes())
if err != nil {
return nil, err
}
cs, okCS := csTmp.(*pngstructure.ChunkSlice)
if !okCS {
return nil, errors.New(`type not *pngstructure.ChunkSlice`)
}
chunks := cs.Chunks()
textChunks := []*pngstructure.Chunk{
// TODO set global constants for chunk names
{Type: `tEXt`, Data: []uint8("Thumb::URI\x00" + fileURL)},
{Type: `tEXt`, Data: []uint8("Thumb::MTime\x00" + mTime)},
{Type: `tEXt`, Data: []uint8("Software\x00" + softwareStr)},
}
for _, textChunk := range textChunks {
textChunk.Length = uint32(len(textChunk.Data))
textChunk.UpdateCrc32()
}
// insert tEXt chunks after IHDR
chunks = append(
chunks[:1],
append(
textChunks,
chunks[1:]...,
)...,
)
cs = pngstructure.NewChunkSlice(chunks)
ft, err := os.OpenFile(thumbnailFilename+`.tmp`, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return nil, err
}
defer ft.Close()
if err := cs.WriteTo(ft); err != nil {
return nil, err
}
if err := os.Rename(thumbnailFilename+`.tmp`, thumbnailFilename); err != nil {
return nil, err
}
return img, nil
}