(bucket, fileName string)
| 38 | } |
| 39 | |
| 40 | func GetFileAndThumbnail(bucket, fileName string) { |
| 41 | if strings.Contains(fileName, "thumbnail") { |
| 42 | return |
| 43 | } |
| 44 | //log.Println("[GetFileAndThumbnail] Start", "bucket", bucket, "fileName", fileName) |
| 45 | //判断是不是图片 |
| 46 | n := strings.Split(fileName, ".") |
| 47 | if !CheckIsImage(n[1]) { |
| 48 | return |
| 49 | } |
| 50 | thumbnailName := fmt.Sprintf("%s_%s.%s", n[0], "thumbnail", "jpg") |
| 51 | if CheckFileEx(bucket, thumbnailName) { |
| 52 | return |
| 53 | } |
| 54 | if len(strings.Split(fileName, ".")) != 2 { |
| 55 | log.Println("[GetFileAndThumbnail] fileName != 2", fileName, "bucket", bucket) |
| 56 | return |
| 57 | } |
| 58 | reader, err := minioClient.GetObject(bucket, fileName, minio.GetObjectOptions{}) |
| 59 | if err != nil { |
| 60 | log.Println("[GetFileAndThumbnail] bucket", bucket, "fileName", fileName, "GetObject", err) |
| 61 | return |
| 62 | } |
| 63 | defer reader.Close() |
| 64 | readerByte, err := jpeg.Decode(reader) |
| 65 | if err != nil { |
| 66 | log.Println("[GetFileAndThumbnail] bucket", bucket, "fileName", fileName, " ReadAll", err) |
| 67 | return |
| 68 | } |
| 69 | m := resize.Thumbnail(maxWidth, maxHeight, readerByte, resize.Lanczos3) |
| 70 | var buf bytes.Buffer |
| 71 | err = jpeg.Encode(&buf, m, nil) |
| 72 | if err != nil { |
| 73 | log.Println("[GetFileAndThumbnail] bucket", bucket, "fileName", fileName, " Encode", err) |
| 74 | return |
| 75 | } |
| 76 | if CheckFileEx(bucket, thumbnailName) { |
| 77 | return |
| 78 | } |
| 79 | _, err = minioClient.PutObject(bucket, thumbnailName, bytes.NewReader(buf.Bytes()), int64(len(buf.Bytes())), minio.PutObjectOptions{ |
| 80 | ContentType: "image/jpg", |
| 81 | }) |
| 82 | if err != nil { |
| 83 | log.Println("[GetFileAndThumbnail] bucket", bucket, "fileName", "fileName", " PutObject", err) |
| 84 | return |
| 85 | } |
| 86 | //log.Println("[GetFileAndThumbnail] End bucket", bucket, "fileName", fileName, "thumbnail", thumbnailName) |
| 87 | } |
| 88 | |
| 89 | func CheckFileEx(bucket, fileName string) bool { |
| 90 | objInfo, err := minioClient.StatObject(bucket, fileName, minio.StatObjectOptions{}) |
no test coverage detected