-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
create.go
153 lines (118 loc) Β· 4.37 KB
/
create.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
package thumb
import (
"errors"
"fmt"
"image"
"image/png"
"path"
"path/filepath"
"github.com/disintegration/imaging"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/fs"
)
// Suffix returns the thumb cache file suffix.
func Suffix(width, height int, opts ...ResampleOption) (result string) {
method, _, format := ResampleOptions(opts...)
result = fmt.Sprintf("%dx%d_%s.%s", width, height, ResampleMethods[method], format)
return result
}
// FileName returns the file name of the thumbnail for the matching size.
func FileName(hash, thumbPath string, width, height int, opts ...ResampleOption) (fileName string, err error) {
if InvalidSize(width) {
return "", fmt.Errorf("thumb: width exceeds limit (%d)", width)
}
if InvalidSize(height) {
return "", fmt.Errorf("thumb: height exceeds limit (%d)", height)
}
if len(hash) < 4 {
return "", fmt.Errorf("thumb: file hash is empty or too short (%s)", clean.Log(hash))
}
if len(thumbPath) == 0 {
return "", errors.New("thumb: folder is empty")
}
suffix := Suffix(width, height, opts...)
p := path.Join(thumbPath, hash[0:1], hash[1:2], hash[2:3])
if err = fs.MkdirAll(p); err != nil {
return "", err
}
fileName = fmt.Sprintf("%s/%s_%s", p, hash, suffix)
return fileName, nil
}
// ResolvedName returns the file name of the thumbnail for the matching size with all symlinks resolved.
func ResolvedName(hash, thumbPath string, width, height int, opts ...ResampleOption) (fileName string, err error) {
if fileName, err = FileName(hash, thumbPath, width, height, opts...); err != nil {
return fileName, err
} else {
return fs.Resolve(fileName)
}
}
// FromCache returns the filename if a thumbnail image with the matching size is in the cache.
func FromCache(imageFilename, hash, thumbPath string, width, height int, opts ...ResampleOption) (fileName string, err error) {
if len(hash) < 4 {
return "", fmt.Errorf("thumb: invalid file hash %s", clean.Log(hash))
}
if len(imageFilename) < 4 {
return "", fmt.Errorf("thumb: invalid file name %s", clean.Log(imageFilename))
}
if fileName, err = FileName(hash, thumbPath, width, height, opts...); err != nil {
log.Debugf("thumb: %s in %s (filename)", err, clean.Log(filepath.Base(imageFilename)))
return "", err
} else if fileName, err = fs.Resolve(fileName); err != nil {
return "", ErrNotCached
} else if fs.FileExistsNotEmpty(fileName) {
return fileName, nil
}
return "", ErrNotCached
}
// FromFile generates a new thumbnail with the requested size, if it does not already exist, and returns its filename.
func FromFile(imageName, hash, thumbPath string, width, height, orientation int, opts ...ResampleOption) (fileName string, err error) {
if fileName, err = FromCache(imageName, hash, thumbPath, width, height, opts...); err == nil {
return fileName, err
} else if !errors.Is(err, ErrNotCached) {
return "", err
}
// Use libvips to generate thumbnails?
if Library == LibVips {
fileName, _, err = Vips(imageName, nil, hash, thumbPath, width, height, opts...)
return fileName, err
}
// Generate thumb cache filename.
fileName, err = FileName(hash, thumbPath, width, height, opts...)
if err != nil {
log.Debugf("thumb: %s in %s (filename)", err, clean.Log(filepath.Base(imageName)))
return "", err
}
// Load image from file.
img, err := Open(imageName, orientation)
if err != nil {
log.Debugf("thumb: %s in %s", err, clean.Log(filepath.Base(imageName)))
return "", err
}
// Create thumb from image.
if _, err = Create(img, fileName, width, height, opts...); err != nil {
return "", err
}
return fileName, nil
}
// Create creates an image thumbnail.
func Create(img image.Image, fileName string, width, height int, opts ...ResampleOption) (result image.Image, err error) {
if InvalidSize(width) {
return img, fmt.Errorf("thumb: width has an invalid value (%d)", width)
}
if InvalidSize(height) {
return img, fmt.Errorf("thumb: height has an invalid value (%d)", height)
}
result = Resample(img, width, height, opts...)
var quality imaging.EncodeOption
if fs.FileType(fileName) == fs.ImagePNG {
quality = imaging.PNGCompressionLevel(png.DefaultCompression)
} else {
quality = JpegQuality(width, height).EncodeOption()
}
err = imaging.Save(result, fileName, quality)
if err != nil {
log.Debugf("thumb: failed to save %s", clean.Log(filepath.Base(fileName)))
return result, err
}
return result, nil
}