-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
458 lines (399 loc) · 11.2 KB
/
Copy pathutils.go
File metadata and controls
458 lines (399 loc) · 11.2 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
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
package pluto
import (
"context"
"encoding/hex"
"errors"
"fmt"
"image"
"image/draw"
"math"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/disintegration/imaging"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5"
"github.com/nfnt/resize"
)
const eps = 1e-5
func ParamInt(gc *gin.Context, key string) (int, bool) {
str := gc.Param(key)
val, err := strconv.Atoi(str)
if err != nil {
return 0, false
}
return val, true
}
func GetQueryInt(gc *gin.Context, key string) (int, bool) {
str, ok := gc.GetQuery(key)
if !ok {
return 0, false
}
val, err := strconv.Atoi(str)
if err != nil {
return 0, false
}
return val, true
}
func GetQueryIntDefault(gc *gin.Context, key string, def int) (int, bool) {
str, ok := gc.GetQuery(key)
if !ok {
return def, true
}
val, err := strconv.Atoi(str)
if err != nil {
return 0, false
}
return val, true
}
func GetQueryBoolDefault(gc *gin.Context, key string, def bool) (bool, bool) {
str, ok := gc.GetQuery(key)
if !ok {
return def, true
}
if str == "" {
return true, true
}
val, err := strconv.ParseBool(str)
if err != nil {
return false, false
}
return val, true
}
func ParseAspectRatio(s string) (float32, error) {
parts := strings.Split(s, ":")
if len(parts) != 2 {
return 0, errors.New("Invalid ratio")
}
w, err1 := strconv.ParseFloat(parts[0], 64)
h, err2 := strconv.ParseFloat(parts[1], 64)
if err1 != nil || err2 != nil || h == 0 {
return 0, errors.New("Invalid ratio")
}
return float32(w) / float32(h), nil
}
func ResizeToWidth(img image.Image, width int, ratio string) image.Image {
// Parse ratio like "7by5"
var height uint
if parts := strings.Split(ratio, "by"); len(parts) == 2 {
rw, _ := strconv.Atoi(parts[0])
rh, _ := strconv.Atoi(parts[1])
if rw > 0 && rh > 0 {
height = uint(float64(width) * float64(rh) / float64(rw))
}
}
if height == 0 {
// Maintain original aspect ratio
return resize.Resize(uint(width), 0, img, resize.Lanczos3)
}
return resize.Resize(uint(width), height, img, resize.Lanczos3)
}
func cropImage(img image.Image, rect image.Rectangle) image.Image {
return img.(interface {
SubImage(r image.Rectangle) image.Image
}).SubImage(rect)
}
// CropWithFocus crops an image to a target aspect ratio and optionally resizes,
// centered on the focus point, while preserving alpha transparency.
func CropWithFocus(
img image.Image,
targetRatio float32, // desired width/height ratio (ignored if targetW/targetH set)
focusX, focusY float32, // normalized [0,1] focus point
targetW, targetH int, // optional final size; 0 to keep crop size
) image.Image {
srcBounds := img.Bounds()
srcW := srcBounds.Dx()
srcH := srcBounds.Dy()
srcRatio := float32(srcW) / float32(srcH)
// Clamp focus values to [0,1]
focusX = clampNormalized(focusX)
focusY = clampNormalized(focusY)
// Compute target ratio if targetW and targetH are set
if targetW > 0 && targetH > 0 {
targetRatio = float32(targetW) / float32(targetH)
} else if targetRatio <= 0.00001 {
targetRatio = srcRatio
}
// Determine crop size (never larger than source)
var cropW, cropH int
if srcRatio > targetRatio {
// Source is wider than target ratio → limit width
cropH = srcH
cropW = int(float32(cropH) * targetRatio)
} else {
// Source is taller than target ratio → limit height
cropW = srcW
cropH = int(float32(cropW) / targetRatio)
}
// Compute top-left of crop rectangle, centered on focus
centerX := int(focusX * float32(srcW))
centerY := int(focusY * float32(srcH))
x0 := clampInt(centerX-cropW/2, 0, srcW-cropW)
y0 := clampInt(centerY-cropH/2, 0, srcH-cropH)
cropRect := image.Rect(x0, y0, x0+cropW, y0+cropH)
// Crop into NRGBA to preserve alpha
var cropped *image.NRGBA
if nrgbaImg, ok := img.(*image.NRGBA); ok {
cropped = imaging.Crop(nrgbaImg, cropRect)
} else {
dst := imaging.New(cropW, cropH, image.Transparent)
draw.Draw(dst, dst.Bounds(), img, cropRect.Min, draw.Over)
cropped = dst
}
// Optional resizing: scale down only, never exceed cropped size
finalW, finalH := cropW, cropH
if targetW > 0 {
scale := float64(targetW) / float64(cropW)
if scale < 1 {
finalW = targetW
finalH = int(float64(cropH) * scale)
}
}
if targetH > 0 {
scale := float64(targetH) / float64(cropH)
if scale < 1 {
finalH = targetH
finalW = int(float64(cropW) * scale)
}
}
// Resize only if needed
if finalW != cropW || finalH != cropH {
cropped = imaging.Resize(cropped, finalW, finalH, imaging.Lanczos)
}
return cropped
}
func CropWithFocusWithoutAlpha(
img image.Image,
targetRatio float32,
focusX, focusY float32,
targetW, targetH int,
) image.Image {
srcBounds := img.Bounds()
srcW := srcBounds.Dx()
srcH := srcBounds.Dy()
srcRatio := float32(srcW) / float32(srcH)
// Clamp focus values to [0,1]
focusX = clampNormalized(focusX)
focusY = clampNormalized(focusY)
// Determine target ratio
if targetW > 0 && targetH > 0 {
targetRatio = float32(targetW) / float32(targetH)
} else if targetRatio <= eps {
targetRatio = srcRatio
}
// Determine crop size based on target aspect ratio
var cropW, cropH int
if srcRatio > targetRatio {
// Source is wider → crop width
cropH = srcH
cropW = int(float32(cropH) * targetRatio)
} else {
// Source is taller → crop height
cropW = srcW
cropH = int(float32(cropW) / targetRatio)
}
// Compute top-left corner so that focus point is centered
centerX := int(focusX * float32(srcW))
centerY := int(focusY * float32(srcH))
x0 := clampInt(centerX-cropW/2, 0, srcW-cropW)
y0 := clampInt(centerY-cropH/2, 0, srcH-cropH)
cropped := imaging.Crop(img, image.Rect(x0, y0, x0+cropW, y0+cropH))
// --- Compute final size ---
finalW, finalH := cropW, cropH
if targetW > 0 {
// Width is driving value
finalW = targetW
finalH = int(float32(finalW) / float32(cropW) * float32(cropH))
}
if targetH > 0 && finalH > targetH {
// Make sure height does not exceed targetH
finalH = targetH
finalW = int(float32(finalH) / float32(cropH) * float32(cropW))
}
// Resize if needed
if finalW != cropW || finalH != cropH {
cropped = imaging.Resize(cropped, finalW, finalH, imaging.Lanczos)
}
return cropped
}
func clampNormalized(v float32) float32 {
if v < 0.0 {
return 0.0
}
if v > 1.0 {
return 1.0
}
return v
}
func clampInt(value, minVal, maxVal int) int {
if value < minVal {
return minVal
}
if value > maxVal {
return maxVal
}
return value
}
// EncodeFloat32ForPath converts a float32 into a safe ASCII filename string.
// Output contains only [0-9a-f] and is exactly 8 characters long.
func EncodeFloat32ForPath(f float32) string {
bits := math.Float32bits(f) // Get IEEE 754 binary representation
buf := make([]byte, 4)
buf[0] = byte(bits >> 24)
buf[1] = byte(bits >> 16)
buf[2] = byte(bits >> 8)
buf[3] = byte(bits)
return hex.EncodeToString(buf) // 4 bytes -> 8 hex chars
}
// DecodeFloat32FromPath reverses the encoding back to float32.
func DecodeFloat32FromPath(s string) (float32, error) {
b, err := hex.DecodeString(s)
if err != nil || len(b) != 4 {
return 0, errors.New("Invalid hex float string")
}
bits := uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3])
return math.Float32frombits(bits), nil
}
type ImageCleanupResult struct {
CacheFilesRemoved int
ImageFileRemoved bool
}
func CleanupPlutoImageFiles(imageUuid string, fileName string) (*ImageCleanupResult, error) {
result := &ImageCleanupResult{}
// Always clean cache (safe even if image doesn't exist)
cacheFilesRemoved, err := CleanupPlutoCache(imageUuid)
if err != nil {
return result, err
}
result.CacheFilesRemoved = cacheFilesRemoved
// Only clean image file if we actually have a filename
if fileName != "" {
imageFileRemoved, err := CleanupPlutoImage(fileName)
if err != nil {
return result, err
}
result.ImageFileRemoved = imageFileRemoved
}
return result, nil
}
// Delete original image file
func CleanupPlutoImage(imageFileName string) (bool, error) {
if imageFileName != "" {
path := filepath.Join(PlutoInstance.Config.PlutoImageDir, imageFileName)
if err := RemoveFile(path); err != nil {
return false, err
}
}
return true, nil
}
// Delete cache files
func CleanupPlutoCache(imageUuid string) (int, error) {
prefix := fmt.Sprintf("%s_", imageUuid)
cacheFilesRemoved, err := DeleteFilesWithPrefix(PlutoInstance.Config.PlutoCacheDir, prefix)
if err != nil {
return 0, err
}
return cacheFilesRemoved, nil
}
// DeleteFilesWithPrefix deletes all files in a directory that start with the given prefix.
// Returns the number of deleted files and an error (if any).
func DeleteFilesWithPrefix(dir string, prefix string) (int, error) {
if len(prefix) < 1 {
return 0, errors.New("Prefix required")
}
entries, err := os.ReadDir(dir)
if err != nil {
return 0, fmt.Errorf("Failed to read directory: %w", err)
}
deletedCount := 0
for _, entry := range entries {
if entry.IsDir() {
continue
}
name := entry.Name()
if strings.HasPrefix(name, prefix) {
fullPath := filepath.Join(dir, name)
if err := os.Remove(fullPath); err != nil {
return deletedCount, fmt.Errorf("Failed to delete %s: %w", fullPath, err)
}
deletedCount++
}
}
return deletedCount, nil
}
// RemoveFile deletes a file at the given path.
// Returns an error if the file cannot be deleted.
func RemoveFile(path string) error {
if path == "" {
return errors.New("File path required")
}
// Check if file exists first (optional)
if _, err := os.Stat(path); os.IsNotExist(err) {
return fmt.Errorf("File does not exist: %s", path)
}
if err := os.Remove(path); err != nil {
return fmt.Errorf("Failed to delete file %s: %w", path, err)
}
return nil
}
// GetImageFocus returns focus_x and focus_y for an image
func GetImageFocusTx(
ctx context.Context,
tx pgx.Tx,
imageUuid string,
) (focusX *float64, focusY *float64, err error) {
query := fmt.Sprintf(
`SELECT focus_x, focus_y FROM %s.pluto_image WHERE uuid = $1::uuid`,
PlutoInstance.DbSchema,
)
var fx, fy *float64
err = tx.QueryRow(ctx, query, imageUuid).Scan(&fx, &fy)
if err != nil {
return nil, nil, err
}
return fx, fy, nil
}
// Deletes image + cache DB entries, returns filename to delete from disk
func DeleteImageTx(
ctx context.Context,
tx pgx.Tx,
imageUuid string,
) (deletedFileName string, cacheRows int64, err error) {
schema := PlutoInstance.DbSchema
// Delete cache rows
cacheRowsAffected, err := DeleteCacheTx(ctx, tx, imageUuid)
if err != nil {
return "", 0, err
}
// Delete image row
imageQuery := fmt.Sprintf(`DELETE FROM %s.pluto_image WHERE uuid = $1::uuid RETURNING gen_file_name`, schema)
err = tx.QueryRow(ctx, imageQuery, imageUuid).Scan(&deletedFileName)
if err != nil {
return "", cacheRowsAffected, err
}
return deletedFileName, cacheRowsAffected, nil
}
// Deletes cache DB entries, return number of affected rows
func DeleteCacheTx(
ctx context.Context,
tx pgx.Tx,
imageUuid string,
) (deletedFilesCount int64, err error) {
schema := PlutoInstance.DbSchema
cacheQuery := fmt.Sprintf(`DELETE FROM %s.pluto_cache WHERE pluto_image_uuid = $1::uuid`, schema)
cmdTag, err := tx.Exec(ctx, cacheQuery, imageUuid)
if err != nil {
return 0, err
}
return cmdTag.RowsAffected(), nil
}
func FloatPtrEqual(a, b *float64) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
return *a == *b
}