@@ -22,7 +22,6 @@ import (
22
22
"net/http"
23
23
"net/url"
24
24
"path"
25
- "path/filepath"
26
25
"reflect"
27
26
"strconv"
28
27
"strings"
@@ -313,6 +312,16 @@ func (m *Minio) DeleteObject(ctx context.Context, name string) error {
313
312
if err := m .initMinio (ctx ); err != nil {
314
313
return err
315
314
}
315
+ info , err := m .core .Client .StatObject (ctx , m .bucket , name , minio.StatObjectOptions {})
316
+ if err != nil {
317
+ if m .IsNotFound (err ) {
318
+ return nil
319
+ }
320
+ return err
321
+ }
322
+ if err := m .clearObjectImageInfoKey (ctx , name , info .ETag ); err != nil {
323
+ return err
324
+ }
316
325
return m .core .Client .RemoveObject (ctx , m .bucket , name , minio.RemoveObjectOptions {})
317
326
}
318
327
@@ -513,6 +522,64 @@ func (m *Minio) GetImageThumbnailKey(ctx context.Context, name string) (string,
513
522
}
514
523
thumbnailWidth , thumbnailHeight := getThumbnailSize (img )
515
524
516
- cacheKey := filepath .Join (imageThumbnailPath , info .Etag , fmt .Sprintf ("image_w%d_h%d.%s" , thumbnailWidth , thumbnailHeight , info .Format ))
525
+ cacheKey := path .Join (imageThumbnailPath , info .Etag , fmt .Sprintf ("image_w%d_h%d.%s" , thumbnailWidth , thumbnailHeight , info .Format ))
517
526
return cacheKey , nil
518
527
}
528
+
529
+ func (m * Minio ) clearObjectImageInfoKey (ctx context.Context , name string , etag string ) error {
530
+ if err := m .initMinio (ctx ); err != nil {
531
+ return err
532
+ }
533
+ prefix := path .Join (imageThumbnailPath , etag )
534
+ if ! strings .HasSuffix (prefix , "/" ) {
535
+ prefix += "/"
536
+ }
537
+ res := m .core .Client .ListObjects (ctx , m .bucket , minio.ListObjectsOptions {Prefix : prefix })
538
+ var first bool
539
+ for {
540
+ select {
541
+ case <- ctx .Done ():
542
+ return ctx .Err ()
543
+ case val , ok := <- res :
544
+ if ! ok {
545
+ return nil
546
+ }
547
+ if val .Err != nil {
548
+ return val .Err
549
+ }
550
+ if first == false {
551
+ first = true
552
+ // openim/thumbnail/039395be6547fb10724fd0999ea3e834/image_w640_h640.png
553
+ // MINIO:IMAGE:openim/data/hash/f4061e92af6f0215f9e7ef4d4b78f234
554
+ if err := m .cache .DelObjectImageInfoKey (ctx , etag ); err != nil {
555
+ return err
556
+ }
557
+ }
558
+ if err := m .deleteThumbnailCache (ctx , name , val .Key ); err != nil {
559
+ return err
560
+ }
561
+ if err := m .core .Client .RemoveObject (ctx , m .bucket , val .Key , minio.RemoveObjectOptions {}); err != nil {
562
+ return err
563
+ }
564
+ }
565
+ }
566
+ }
567
+
568
+ func (m * Minio ) deleteThumbnailCache (ctx context.Context , name string , key string ) error {
569
+ // openim/thumbnail/ae20fe3d6466fdb11bcf465386b51312/image_w640_h640.jpeg
570
+ // MINIO:THUMBNAIL:jpeg:w640:h640:openim/data/hash/f4061e92af6f0215f9e7ef4d4b78f234
571
+ filename := path .Base (key )
572
+ ext := path .Ext (filename )
573
+ filename = strings .TrimSuffix (filename , ext )
574
+ filename = strings .TrimPrefix (filename , "image_w" )
575
+ var (
576
+ width int
577
+ height int
578
+ )
579
+ if arr := strings .Split (filename , "_h" ); len (arr ) == 2 {
580
+ width , _ = strconv .Atoi (arr [0 ])
581
+ height , _ = strconv .Atoi (arr [1 ])
582
+ }
583
+ format := strings .Trim (ext , "." )
584
+ return m .cache .DelImageThumbnailKey (ctx , name , format , width , height )
585
+ }
0 commit comments