-
Notifications
You must be signed in to change notification settings - Fork 3
/
profile.go
530 lines (479 loc) · 22.4 KB
/
profile.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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
package main
import (
"database/sql"
"encoding/binary"
// "encoding/json"
"errors"
"fmt"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
"github.com/peterbourgon/diskv/v3"
"gopkg.in/gographics/imagick.v3/imagick"
"html/template"
"io"
// jsoniter "github.com/json-iterator/go"
"log"
"net/http"
// "os"
// "os/exec"
"path"
"path/filepath"
"strings"
// "time"
)
type UserProfile struct {
UserUUID string `form:"UserUUID" json:"useruuid"`
ProfilePartner string `form:"ProfilePartner" json:"profilePartner"`
ProfileAllowPublish bool `form:"ProfileAllowPublish" json:"profileAllowPublish"` // inside the database, this is binary(1)
ProfileMaturePublish bool `form:"ProfileMaturePublish" json:"profileMaturePublish"`
ProfilePublish []string `form:"ProfilePublish" json:"profilePublish"` // seems to be needed; values are Allow and Mature
ProfileURL string `form:"ProfileURL" json:"profileURL"`
ProfileWantToMask int `form:"ProfileWantToMask" json:"profileWantToMask"`
ProfileWantTo []string `form:"ProfileWantTo[]"`
ProfileWantToText string `form:"ProfileWantToText" json:"profileWantToText"`
ProfileSkillsMask int `form:"ProfileSkillsMask" json:"profileSkillsMask"`
ProfileSkills []string `form:"ProfileSkills[]"`
ProfileSkillsText string `form:"ProfileSkillsText" json:"profileSkillsText"`
ProfileLanguages string `form:"ProfileLanguages" json:"profileLanguages"`
ProfileImage string `form:"ProfileImage" json:"profileImage"`
ProfileAboutText string `form:"ProfileAboutText" json:"profileAboutText"`
ProfileFirstImage string `form:"ProfileFirstImage" json:"profileFirstImage"`
ProfileFirstText string `form:"ProfileFirstText" json:"profileFirstText"`
}
// GetProfile connects to the database, does its magic, and spews out a profile. That's the theory at least.
func GetProfile(c *gin.Context) {
session := sessions.Default(c)
username := session.Get("Username")
uuid := session.Get("UUID")
// open database connection
if *config["dsn"] == "" {
log.Fatal("Please configure the DSN for accessing your OpenSimulator database; this application won't work without that")
}
db, err := sql.Open("mysql", *config["dsn"]) // presumes mysql for now
checkErrFatal(err)
defer db.Close()
var (
profileData UserProfile
// avatarProfileImage string // constructed URL for the profile image (gwyneth 20200719) Note: not used any longer (gwyneth 20200728)
// allowPublish, maturePublish []byte // it has to be this way to get around a bug in the mySQL driver which is impossible to fix
allowPublishInt, maturePublishInt uint64 // intermediary things
unsafeProfileURL, unsafeProfileWantToText, unsafeProfileLanguages, unsafeProfileAboutText, unsafeProfileFirstText string // user-provided data requiring strict sanitising (gwyneth 20200815)
)
// Allegedly, this is the only way to extract a binary(n) type into a variable;
// we need these so-called '[]byte buffers' to temporarily store conversion results (gwyneth 20210118)
allowPublish := make([]byte, binary.MaxVarintLen64)
maturePublish := make([]byte, binary.MaxVarintLen64)
err = db.QueryRow("SELECT useruuid, profilePartner, profileAllowPublish, profileMaturePublish, profileURL, profileWantToMask, profileWantToText, profileSkillsMask, profileSkillsText, profileLanguages, profileImage, profileAboutText, profileFirstImage, profileFirstText FROM userprofile WHERE useruuid = ?", uuid).Scan(
&profileData.UserUUID,
&profileData.ProfilePartner,
&allowPublish, // &profileData.ProfileAllowPublish,
&maturePublish, // &profileData.ProfileMaturePublish,
&unsafeProfileURL,
&profileData.ProfileWantToMask,
&unsafeProfileWantToText,
&profileData.ProfileSkillsMask,
&profileData.ProfileSkillsText,
&unsafeProfileLanguages,
&profileData.ProfileImage,
&unsafeProfileAboutText,
&profileData.ProfileFirstImage,
&unsafeProfileFirstText,
)
profileData.ProfileURL = bluemondaySafeHTML.Sanitize(unsafeProfileURL)
profileData.ProfileWantToText = bluemondaySafeHTML.Sanitize(unsafeProfileWantToText)
profileData.ProfileLanguages = bluemondaySafeHTML.Sanitize(unsafeProfileLanguages)
profileData.ProfileAboutText = bluemondaySafeHTML.Sanitize(unsafeProfileAboutText)
profileData.ProfileFirstText = bluemondaySafeHTML.Sanitize(unsafeProfileFirstText)
// Since we get a int(1) = byte, I'll stick with a byte... (gwyneth 20210117)
// That approach doesn't work, so we'll try using encode/binary instead
allowPublishInt, _ = binary.Uvarint(allowPublish)
profileData.ProfileAllowPublish = allowPublishInt > 0 // hm!
maturePublishInt, _ = binary.Uvarint(maturePublish)
profileData.ProfileMaturePublish = maturePublishInt > 0
if err != nil { // db.QueryRow() will return ErrNoRows, which will be passed to Scan()
if *config["ginMode"] == "debug" {
log.Printf("[ERROR]: retrieving profile from user %q (%s) failed; database error was %v\n", username, uuid, err)
}
} else {
if *config["ginMode"] == "debug" {
log.Printf("[DEBUG]: while retrieving profile, allowPublish is %v while maturePublish is %v\n", allowPublish, maturePublish)
log.Printf("[DEBUG]: after some magic, allowPublishInt is %d while maturePublishInt is %d\n", allowPublishInt, maturePublishInt)
log.Printf("[DEBUG]: retrieving profile from user %q (%s): %+v\n", username, uuid, profileData)
}
}
// see if we have this image already
// Note: in the future, we might simplify the call by just using the UUID + file extension... (gwyneth 20200727)
// Note 2: We *also* retrieve a Retina image and store it in the cache, but we do not specifically check for it. The idea is that proper HTML will deal with selecting the 'correct' image, we only need to check for one of them. Also, if the conversion to Retina fails for some reason, that's not a problem, we'll fall back to whatever has been downloaded so far...
profileImage := filepath.Join( /* PathToStaticFiles, */ *config["cache"], profileData.ProfileImage+*config["convertExt"])
profileRetinaImage := filepath.Join( /* PathToStaticFiles, */ *config["cache"], profileData.ProfileImage+"@2x"+*config["convertExt"]) // we need the path, but we won't check for it directly
/*
if profileImage[0] != '/' {
profileImage = "/" + profileImage
}
*/
// either this URL exists and is in the cache, or not, and we need to get the image from
// OpenSimulator and attempt to convert it... we won't change the URL in the process.
// Note: Other usages of the diskv cache might not be so obvious... or maybe they all are? (gwyneth 20200727)
if !imageCache.Has(profileImage) { // this URL is not in the cache yet!
if *config["ginMode"] == "debug" {
log.Println("[INFO] Cache miss on profileImage:", profileImage, " - attempting to download it...")
}
// get it!
profileImageAssetURL := *config["assetServer"] + path.Join("/assets/", profileData.ProfileImage, "/data")
resp, err := http.Get(profileImageAssetURL)
if err != nil {
// handle error
log.Println("[ERROR] Oops — OpenSimulator cannot find", profileImageAssetURL, "error was:", err)
}
defer resp.Body.Close()
newImage, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("[ERROR] Oops — could not get contents of", profileImageAssetURL, "from OpenSimulator, error was:", err)
}
if len(newImage) == 0 {
log.Println("[ERROR] Image retrieved from OpenSimulator", profileImageAssetURL, "has zero bytes.")
// we might have to get out of here
} else {
if *config["ginMode"] == "debug" {
log.Println("[INFO] Image retrieved from OpenSimulator", profileImageAssetURL, "has", len(newImage), "bytes.")
}
}
// Now use ImageMagick to convert this image!
// Note: I've avoided using ImageMagick because it's compiled with Cgo, but I can't do better
// than this. See also https://stackoverflow.com/questions/38950909/c-style-conditional-compilation-in-golang for a way to prevent ImageMagick to be used.
convertedImage, retinaImage, err := ImageConvert(newImage, 256, 256, 100)
if err != nil {
log.Println("[ERROR] Could not convert", profileImageAssetURL, " - error was:", err)
}
if /* convertedImage == nil || */ len(convertedImage) == 0 {
log.Println("[ERROR] Converted image is empty")
}
if /* retinaImage == nil || */ len(retinaImage) == 0 {
log.Println("[ERROR] Converted Retina image is empty")
}
if *config["ginMode"] == "debug" {
log.Println("[INFO] Regular image from", profileImageAssetURL, "has", len(convertedImage), "bytes; retina image has", len(retinaImage), "bytes.")
}
// put it into KV cache:
if err := imageCache.Write(profileImage, convertedImage); err != nil {
log.Println("[ERROR] Could not store converted", profileImage, "in the cache, error was:", err)
}
// put Retina image into KV cache as well:
if err := imageCache.Write(profileRetinaImage, retinaImage); err != nil {
log.Println("[ERROR] Could not store retina image", string(retinaImage), "in the cache, error was:", err)
}
}
// note that the code will now assume that profileImage does, indeed, have a valid
// image URL, and will fail with a broken image (404 error on browser) if it doesn't; thus:
// TODO(gwyneth): get some sort of default image for when all of the above fails
// An idea would be just to get a Libravatar! We have it, after all...
// Do the same for the profile image for First (=Real) Life. Comments as above!
profileFirstImage := filepath.Join( /*PathToStaticFiles, */ *config["cache"], profileData.ProfileFirstImage+*config["convertExt"])
profileRetinaFirstImage := filepath.Join( /* PathToStaticFiles, */ *config["cache"], profileData.ProfileFirstImage+"@2x"+*config["convertExt"])
if !imageCache.Has(profileFirstImage) {
if *config["ginMode"] == "debug" {
log.Println("[INFO] Cache miss on profileFirstImage:", profileFirstImage, " - attempting to download it...")
}
profileFirstImageAssetURL := *config["assetServer"] + path.Join("/assets/", profileData.ProfileFirstImage, "/data")
resp, err := http.Get(profileFirstImageAssetURL)
if err != nil {
log.Println("[ERROR] Oops — OpenSimulator cannot find", profileFirstImageAssetURL, "error was:", err)
}
defer resp.Body.Close()
newImage, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("[ERROR] Oops — could not get contents of", profileFirstImageAssetURL, "from OpenSimulator, error was:", err)
}
if len(newImage) == 0 {
log.Println("[ERROR] Image retrieved from OpenSimulator", profileFirstImageAssetURL, "has zero bytes.")
} else {
if *config["ginMode"] == "debug" {
log.Println("[INFO] Image retrieved from OpenSimulator", profileFirstImageAssetURL, "has", len(newImage), "bytes.")
}
}
convertedImage, retinaImage, err := ImageConvert(newImage, 128, 128, 100)
if err != nil {
log.Println("[ERROR] Could not convert", profileFirstImageAssetURL, " - error was:", err)
}
if /* convertedImage == nil || */ len(convertedImage) == 0 {
log.Println("[ERROR] Converted image is empty")
}
if /* retinaImage == nil || */ len(retinaImage) == 0 {
log.Println("[ERROR] Converted Retina image is empty")
}
if *config["ginMode"] == "debug" {
log.Println("[INFO] Image from", profileFirstImageAssetURL, "has", len(convertedImage), "bytes; retina image has", len(retinaImage), "bytes.")
}
if err := imageCache.Write(profileFirstImage, convertedImage); err != nil {
log.Println("[ERROR] Could not store converted", profileFirstImage, "in the cache, error was:", err)
}
// put Retina image into KV cache as well:
if err := imageCache.Write(profileRetinaFirstImage, retinaImage); err != nil {
log.Println("[ERROR] Could not store retina image", string(retinaImage), "in the cache, error was:", err)
}
}
c.HTML(http.StatusOK, "profile.tpl", environment(c, gin.H{
"needsTables": false,
"needsMap": false,
"moreValidation": true,
"Debug": false, // we will probably need two versions of 'debug mode'... (gwyneth 20200622)
"titleCommon": *config["titleCommon"] + profileData.UserUUID + " Profile",
"ProfileData": fmt.Sprintf("%+v", profileData),
"ProfileURL": template.HTML(profileData.ProfileURL),
"UserUUID": profileData.UserUUID,
"ProfilePartner": profileData.ProfilePartner,
"ProfileAllowPublish": profileData.ProfileAllowPublish,
"ProfileMaturePublish": profileData.ProfileMaturePublish,
"ProfileWantToMask": profileData.ProfileWantToMask,
"ProfileWantToText": template.HTML(profileData.ProfileWantToText),
"ProfileSkillsMask": profileData.ProfileSkillsMask,
"ProfileSkillsText": template.HTML(profileData.ProfileSkillsText),
"ProfileLanguages": template.HTML(profileData.ProfileLanguages),
"ProfileImage": profileImage, // OpenSimulator/Second Life profile image
"ProfileRetinaImage": profileRetinaImage, // Generated Retina image
"ProfileAboutText": template.HTML(profileData.ProfileAboutText),
"ProfileFirstImage": profileFirstImage, // Real life, i.e. 'First Life' image
"ProfileRetinaFirstImage": profileRetinaFirstImage, // Another generated Retina image
"ProfileFirstText": template.HTML(profileData.ProfileFirstText),
}))
}
// saveProfile is what gets called when someone saves the profile.
func saveProfile(c *gin.Context) {
var oneProfile UserProfile
session := sessions.Default(c)
thisUUID := session.Get("UUID")
if c.Bind(&oneProfile) != nil { // nil means no errors
c.HTML(http.StatusBadRequest, "404.tpl", environment(c, gin.H{
"errorcode": http.StatusBadRequest,
"errortext": "Saving profile failed",
"errorbody": "No form data posted",
"titleCommon": *config["titleCommon"] + " - Profile",
}))
log.Println("[ERROR] No form data posted for saving profile")
return
}
if *config["ginMode"] == "debug" {
log.Printf("[DEBUG] oneProfile is now %+v\n", oneProfile)
}
// check if we really are who we claim to be
if thisUUID != oneProfile.UserUUID {
c.HTML(http.StatusUnauthorized, "404.tpl", environment(c, gin.H{
"errorcode": http.StatusUnauthorized,
"errortext": "No permission",
"errorbody": fmt.Sprintf("You have no permission to change the profile for %q", session.Get("Username")),
"titleCommon": *config["titleCommon"] + " - Profile",
}))
log.Printf("[ERROR] Session UUID %q is not the same as Profile UUID %q - profile data change for %q not allowed\n",
thisUUID, oneProfile.UserUUID, session.Get("Username"))
return
}
// Allegedly we have successfully bound to the form data, so we can proceed to write it to the database.
if *config["dsn"] == "" {
log.Fatal("Please configure the DSN for accessing your OpenSimulator database; this application won't work without that")
}
db, err := sql.Open("mysql", *config["dsn"]) // presumes mysql for now
checkErrFatal(err)
defer db.Close()
// Calculate the masks
var wantToMask, skillsMask int
for _, bitfield := range oneProfile.ProfileWantTo {
switch bitfield {
case "Build":
wantToMask += 1
case "Meet":
wantToMask += 4
case "Group":
wantToMask += 8
case "Sell":
wantToMask += 32
case "Explore":
wantToMask += 2
case "BeHired":
wantToMask += 64
case "Buy":
wantToMask += 16
case "Hire":
wantToMask += 128
}
}
for _, bitfield := range oneProfile.ProfileSkills {
switch bitfield {
case "Textures":
skillsMask += 1
case "Modeling":
skillsMask += 8
case "Scripting":
skillsMask += 16
case "Architecture":
skillsMask += 2
case "EventPlanning":
skillsMask += 4
case "CustomCharacters":
skillsMask += 32
}
}
if *config["ginMode"] == "debug" {
log.Printf("[DEBUG] oneProfile.ProfileWantTo is %v, wantToMask is %d, oneProfile.ProfileSkills is %v, skillsMask is %d\n", oneProfile.ProfileWantTo, wantToMask, oneProfile.ProfileSkills, skillsMask)
}
if *config["ginMode"] == "debug" {
log.Printf("[DEBUG] oneProfile.ProfileAllowPublish is %+v, oneProfile.ProfileMaturePublish is %+v\n", oneProfile.ProfileAllowPublish, oneProfile.ProfileMaturePublish)
}
allowPublish := make([]byte, binary.MaxVarintLen64)
maturePublish := make([]byte, binary.MaxVarintLen64)
_ = binary.PutUvarint(allowPublish, 0)
_ = binary.PutUvarint(maturePublish, 0)
// we always seem to get checkboxes as a group inside an array, so we do something similar as above with the bitmasks
// however
for _, publish := range oneProfile.ProfilePublish {
switch publish {
case "Allow":
// allowPublish = append(allowPublish, 1)
// allowPublish = 1
_ = binary.PutUvarint(allowPublish, 1)
case "Mature":
// maturePublish = append(maturePublish, 1)
// maturePublish = 1
_ = binary.PutUvarint(maturePublish, 1)
}
}
// if len(allowPublish) == 0 {
// allowPublish = append(allowPublish, 0)
// }
// if len(maturePublish) == 0 {
// maturePublish = append(maturePublish, 0)
// }
if *config["ginMode"] == "debug" {
log.Printf("[DEBUG] oneProfile.ProfilePublish is %+v, allowPublish is %+v, maturePublish is %+v\n", oneProfile.ProfilePublish, allowPublish, maturePublish)
}
// Update it on database
result, err := db.Exec("UPDATE userprofile SET profileAllowPublish = ?, profileMaturePublish = ?, profileURL = ?, profileWantToMask = ?, profileWantToText = ?, profileSkillsMask = ?, profileSkillsText = ?, profileLanguages = ?, profileAboutText = ?, profileFirstText = ? WHERE useruuid = ?",
// oneProfile.ProfilePartner,
allowPublish,
maturePublish,
bluemondaySafeHTML.Sanitize(oneProfile.ProfileURL),
wantToMask, // oneProfile.ProfileWantToMask, // images are read-only!
bluemondaySafeHTML.Sanitize(oneProfile.ProfileWantToText),
skillsMask, // oneProfile.ProfileSkillsMask,
bluemondaySafeHTML.Sanitize(oneProfile.ProfileSkillsText),
bluemondaySafeHTML.Sanitize(oneProfile.ProfileLanguages),
bluemondaySafeHTML.Sanitize(oneProfile.ProfileAboutText),
bluemondaySafeHTML.Sanitize(oneProfile.ProfileFirstText),
oneProfile.UserUUID,
)
checkErr(err)
if numRowsAffected, err := result.RowsAffected(); err != nil {
c.HTML(http.StatusOK, "404.tpl", environment(c, gin.H{
"errorcode": http.StatusInternalServerError,
"errortext": "Saving profile failed",
"errorbody": fmt.Sprintf("Database error was: %q [%d row(s) affected]", err, numRowsAffected),
"titleCommon": *config["titleCommon"] + " - Profile",
}))
log.Printf("[ERROR] Updating database with new profile for %q failed, error was %s\n", thisUUID, err)
// TODO(gwyneth): we
return
} else {
if *config["ginMode"] == "debug" {
log.Printf("[INFO] Success updating database with new profile for %q, number of rows affected: %d\n", thisUUID, numRowsAffected)
}
c.Redirect(http.StatusSeeOther, "/user/profile")
}
}
// Transformation functions
// These will probably be moved to cache.go or something similar (gwyneth 20200724)
// TODO(gwyneth): Probably split it further in subdirectories
func imageCacheTransform(key string) *diskv.PathKey {
path := strings.Split(key, "/")
last := len(path) - 1
if *config["ginMode"] == "debug" {
log.Printf("[DEBUG] imageCacheTransform: got from KV store key %q transformed into path %v and filename %q\n",
key, path, path[last])
}
return &diskv.PathKey{
Path: path[:last],
FileName: path[last],
}
}
func imageCacheInverseTransform(pathKey *diskv.PathKey) string {
if *config["ginMode"] == "debug" {
log.Printf("[DEBUG] imageCacheInverseTransform: pathKey %v which will be returned as %q\n",
pathKey, strings.Join(pathKey.Path, "/")+pathKey.FileName) // inefficient but we're just debugging... (gwyneth 20200727)
}
return strings.Join(pathKey.Path, "/") + pathKey.FileName
}
// ImageConvert will take sequence of bytes of an image and convert it into another image with minimal compression, possibly resizing it.
// Parameters are []byte of original image, height, width, compression quality
// Returns []byte of converted image
// See https://golangcode.com/convert-pdf-to-jpg/ (gwyneth 20200726)
// TODO(gwyneth): We might also generate a Retina image; how will it be saved? Through the KV store?
func ImageConvert(aImage []byte, height, width, compression uint) ([]byte, []byte, error) {
// some minor error checking on params
if height == 0 {
height = 256
}
if width == 0 {
width = height
}
if compression == 0 {
compression = 75
}
if /* aImage == nil || */ len(aImage) == 0 {
return nil, nil, errors.New("empty image passed to ImageConvert")
}
// Now that we have checked all parameters, it's time to setup ImageMagick:
mw := imagick.NewMagickWand()
defer mw.Destroy()
// Load the image into imagemagick
if err := mw.ReadImageBlob(aImage); err != nil {
return nil, nil, err
}
if *config["ginMode"] == "debug" {
filename := mw.GetFilename()
format := mw.GetFormat()
resX, resY, _ := mw.GetResolution()
x, y, _ := mw.GetSize()
imageProfile := mw.GetImageProfile("generic")
length, _ := mw.GetImageLength()
log.Printf("[DEBUG] ImageConvert now attempting to convert image with filename %q and format %q and size %d (%.f ppi), %d (%.f ppi), Generic profile: %q, size in bytes: %d\n", filename, format, x, resX, y, resY, imageProfile, length)
}
if err := mw.ResizeImage(height, width, imagick.FILTER_LANCZOS_SHARP); err != nil {
return nil, nil, err
}
// Must be *after* ReadImage
// Flatten image and remove alpha channel, to prevent alpha turning black in jpg
if err := mw.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_OFF); err != nil {
return nil, nil, err
}
// Set any compression (100 = max quality)
if err := mw.SetCompressionQuality(compression); err != nil {
return nil, nil, err
}
// Move to first image
mw.SetIteratorIndex(0)
// Convert into PNG
var formatType = *config["convertExt"]
if *config["ginMode"] == "debug" {
log.Println("[DEBUG] Setting format type to", formatType[1:])
}
if err := mw.SetFormat(formatType[1:]); err != nil {
return nil, nil, err
}
var err error // We need to define this here because of stupid scoping issues...
// Return []byte for this image.
blob, err := mw.GetImageBlob()
if err != nil {
return nil, nil, err
}
// now do the same for the Retina size
if err := mw.ResizeImage(height*2, width*2, imagick.FILTER_LANCZOS_SHARP); err != nil {
// this probably doesn't make sensem but we return the valid image, while
// sending back nil for the Retina image *and* the error about why this didn't work.
// (gwyneth 20240620)
return blob, nil, err
}
blobRetina, err := mw.GetImageBlob()
if err != nil {
return nil, nil, err
}
return blob, blobRetina, nil
}