-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathimage.go
More file actions
432 lines (397 loc) · 10.3 KB
/
Copy pathimage.go
File metadata and controls
432 lines (397 loc) · 10.3 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package deck
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"hash/crc32"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io"
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
"github.com/corona10/goimagehash"
"github.com/k1LoW/errors"
"golang.org/x/net/publicsuffix"
)
type MIMEType string
const (
MIMETypeImagePNG MIMEType = "image/png"
MIMETypeImageJPEG MIMEType = "image/jpeg"
MIMETypeImageGIF MIMEType = "image/gif"
)
type Image struct {
i image.Image
b []byte // Raw image data
mimeType MIMEType
url string // URL if the image was fetched from a URL
fromMarkdown bool
checksum uint32 // Checksum for the image data
pHash *goimagehash.ImageHash // Perceptual hash for JPEG images
modTime time.Time // Modification time of the image file, if applicable
link string // External link associated with the image
// Upload state management
uploadMutex sync.RWMutex
uploadState uploadState
webContentLink string
uploadError error
}
type uploadState int
const (
uploadStateNotStarted uploadState = iota
uploadStateInProgress
uploadStateCompleted
uploadStateFailed
)
func NewImage(pathOrURL string) (_ *Image, err error) {
defer func() {
err = errors.WithStack(err)
}()
var b io.Reader
var modTime time.Time
if strings.HasPrefix(pathOrURL, "http://") || strings.HasPrefix(pathOrURL, "https://") {
i, ok := LoadImageCache(pathOrURL)
if ok {
return i, nil
}
if _, err := url.Parse(pathOrURL); err != nil {
return nil, fmt.Errorf("invalid URL %s: %w", pathOrURL, err)
}
client := &http.Client{
Timeout: 30 * time.Second,
}
req, err := http.NewRequest("GET", pathOrURL, nil)
if err != nil {
return nil, fmt.Errorf("failed to fetch image from URL %s: %w", pathOrURL, err)
}
req.Header.Set("User-Agent", userAgent)
res, err := client.Do(req) //nolint:gosec // The URL is provided by the user via Markdown content, not from an untrusted external source.
if err != nil {
return nil, fmt.Errorf("failed to fetch image from URL %s: %w", pathOrURL, err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to fetch image from URL %s: status code %d", pathOrURL, res.StatusCode)
}
b = res.Body
} else {
fi, err := os.Stat(pathOrURL)
if err != nil {
return nil, fmt.Errorf("failed to stat image file %s: %w", pathOrURL, err)
}
modTime = fi.ModTime()
i, ok := LoadImageCache(pathOrURL)
if ok {
if modTime.Equal(i.modTime) {
return i, nil
}
}
file, err := os.Open(pathOrURL)
if err != nil {
return nil, fmt.Errorf("failed to open image file %s: %w", pathOrURL, err)
}
defer file.Close()
b = file
}
i, err := newImageFromBuffer(b)
if err != nil {
return nil, fmt.Errorf("failed to create image from buffer: %w", err)
}
i.url = pathOrURL
if isPublicURL(pathOrURL) {
// If the URL appears to be OK for direct access, `deck` will not upload a temporary image to Google Drive
// but will instead specify that URL directly in the CreateImageRequest.
i.webContentLink = pathOrURL
}
i.modTime = modTime
StoreImageCache(pathOrURL, i)
return i, nil
}
func NewImageFromMarkdown(pathOrURL string) (_ *Image, err error) {
defer func() {
err = errors.WithStack(err)
}()
i, err := NewImage(pathOrURL)
if err != nil {
return nil, fmt.Errorf("failed to create image from path or URL: %w", err)
}
i.fromMarkdown = true
return i, nil
}
func NewImageFromCodeBlock(r io.Reader) (_ *Image, err error) {
defer func() {
err = errors.WithStack(err)
}()
i, err := newImageFromBuffer(r)
if err != nil {
return nil, fmt.Errorf("failed to create image from code block: %w", err)
}
i.fromMarkdown = true
return i, nil
}
func newImageFromBuffer(r io.Reader) (_ *Image, err error) {
defer func() {
err = errors.WithStack(err)
}()
b, err := io.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("failed to read image data: %w", err)
}
_, mimeType, err := image.DecodeConfig(bytes.NewReader(b))
if err != nil {
return nil, fmt.Errorf("failed to decode image: %w", err)
}
var mt MIMEType
switch mimeType {
case "png":
mt = MIMETypeImagePNG
case "jpeg":
mt = MIMETypeImageJPEG
case "gif":
mt = MIMETypeImageGIF
default:
return nil, fmt.Errorf("unsupported image MIME type: %s", mimeType)
}
return &Image{
b: b,
mimeType: mt,
}, nil
}
func (i *Image) SetLink(link string) {
i.link = link
}
func (i *Image) Equivalent(ii *Image) bool {
if i == nil || ii == nil {
return false
}
if i.mimeType != ii.mimeType {
return false
}
if i.link != ii.link {
return false
}
if i.Checksum() == ii.Checksum() {
return true
}
// Images are compressed on the Google Slides side (especially JPEG, large images),
// so we use Perceptual Hashing for comparison
aHash, err := i.PHash()
if err != nil {
return false
}
bHash, err := ii.PHash()
if err != nil {
return false
}
distance, err := aHash.Distance(bHash)
if err != nil {
return false
}
if distance < 5 { // threshold for similarity
return true
}
return false
}
func (i *Image) Checksum() uint32 {
if i == nil {
return 0
}
if i.checksum == 0 {
i.checksum = crc32.ChecksumIEEE(i.b)
}
return i.checksum
}
func (i *Image) Image() (image.Image, error) {
if i == nil {
return nil, fmt.Errorf("image is nil")
}
if i.i == nil {
img, _, err := image.Decode(bytes.NewReader(i.b))
if err != nil {
return nil, fmt.Errorf("failed to decode image: %w", err)
}
i.i = img
}
return i.i, nil
}
func (i *Image) PHash() (_ *goimagehash.ImageHash, err error) {
defer func() {
err = errors.WithStack(err)
}()
if i == nil {
return nil, fmt.Errorf("image is nil")
}
if i.i == nil {
if _, err := i.Image(); err != nil {
return nil, err
}
}
if i.pHash == nil {
pHash, err := goimagehash.PerceptionHash(i.i)
if err != nil {
return nil, fmt.Errorf("failed to compute perceptual hash: %w", err)
}
i.pHash = pHash
}
return i.pHash, nil
}
func (i *Image) String() string {
if i == nil {
return ""
}
encoded := base64.StdEncoding.EncodeToString(i.b)
return fmt.Sprintf("data:%s;base64,%s", i.mimeType, encoded)
}
func (i *Image) Bytes() []byte {
if i == nil {
return nil
}
return i.b
}
// internalImage is a subset of `Image` that excludes state and other elements, containing the minimum
// data required to reproduce the `Image`. It is used for `json.Marshal/Unmarshal` and caching purposes.
type internalImage struct {
Data string
URL string
FromMarkdown bool
ModTime time.Time
Link string
}
// MarshalJSON and UnmarshalJSON are defined for cloning data and for similarity comparisons of `slide` structures.
func (i *Image) MarshalJSON() (_ []byte, err error) {
return json.Marshal(i.toInternal())
}
func (i *Image) UnmarshalJSON(data []byte) (err error) {
defer func() {
err = errors.WithStack(err)
}()
var iimg internalImage
if err := json.Unmarshal(data, &iimg); err != nil {
return fmt.Errorf("failed to unmarshal image data: %w", err)
}
return iimg.toImage(i)
}
// StartUpload marks the image as upload in progress.
func (i *Image) StartUpload() {
i.uploadMutex.Lock()
defer i.uploadMutex.Unlock()
i.uploadState = uploadStateInProgress
}
// SetUploadResult sets the upload result (success or failure).
func (i *Image) SetUploadResult(webContentLink string, err error) {
i.uploadMutex.Lock()
defer i.uploadMutex.Unlock()
if err != nil {
i.uploadState = uploadStateFailed
i.uploadError = err
} else {
i.uploadState = uploadStateCompleted
i.webContentLink = webContentLink
i.uploadError = nil
}
}
type uploadInfo struct {
url string
link string
codeBlock bool
}
// UploadInfo waits for the upload to complete and returns the webContentLink.
func (i *Image) UploadInfo(ctx context.Context) (*uploadInfo, error) {
for {
i.uploadMutex.RLock()
state := i.uploadState
link := i.webContentLink
uploadErr := i.uploadError
i.uploadMutex.RUnlock()
switch state {
case uploadStateNotStarted, uploadStateCompleted:
return &uploadInfo{
url: link,
link: i.link,
codeBlock: i.codeBlock(),
}, nil
case uploadStateFailed:
return nil, uploadErr
case uploadStateInProgress:
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(10 * time.Millisecond):
// Continue waiting
}
}
}
}
// IsUploadNeeded returns true if the image needs to be uploaded.
func (i *Image) IsUploadNeeded() bool {
i.uploadMutex.RLock()
defer i.uploadMutex.RUnlock()
return i.uploadState == uploadStateNotStarted && i.webContentLink == ""
}
func (i *Image) codeBlock() bool {
return i.url == "" && i.fromMarkdown
}
func (i *Image) toInternal() *internalImage {
return &internalImage{
Data: i.String(),
URL: i.url,
FromMarkdown: i.fromMarkdown,
ModTime: i.modTime,
Link: i.link,
}
}
func (iimg *internalImage) toImage(i *Image) error {
i.url = iimg.URL
i.fromMarkdown = iimg.FromMarkdown
i.modTime = iimg.ModTime
i.link = iimg.Link
data := []byte(iimg.Data)
if !bytes.HasPrefix(data, []byte(`data:`)) {
return fmt.Errorf("invalid image data: %s", data)
}
splitted := bytes.Split(bytes.TrimPrefix(data, []byte(`data:`)), []byte(";base64,"))
if len(splitted) != 2 {
return fmt.Errorf("invalid image data: %s", data)
}
i.mimeType = MIMEType(splitted[0])
decoded, err := base64.StdEncoding.DecodeString(string(splitted[1]))
if err != nil {
return fmt.Errorf("failed to decode base64 image data: %w", err)
}
_, mimeType, err := image.DecodeConfig(bytes.NewReader(decoded))
if err != nil {
return fmt.Errorf("failed to decode image: %w", err)
}
if string(i.mimeType) != fmt.Sprintf("image/%s", mimeType) {
return fmt.Errorf("image MIME type mismatch: expected %s, got %s", i.mimeType, mimeType)
}
i.b = decoded
return nil
}
// isPublicURL checks whether a URL string is OK for direct public access.
// Since we only need to identify what appear to be public URLs, false negatives are acceptable.
func isPublicURL(rawURL string) bool {
u, err := url.Parse(rawURL)
if err != nil {
return false
}
if u.Scheme != "http" && u.Scheme != "https" {
return false
}
if u.User != nil || u.Port() != "" {
return false
}
if ip := net.ParseIP(u.Host); ip != nil {
return false
}
_, icann := publicsuffix.PublicSuffix(u.Host)
return icann
}