aboutsummaryrefslogtreecommitdiff
path: root/utils/format/files.go
blob: dcdcaf7cfc7b0a4d977a454400e32a1b2ca1440c (plain)
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
package format

import (
	"bytes"
	"fmt"
	"image"
	_ "image/gif"
	"image/jpeg"
	"image/png"
	"strings"

	"golang.org/x/image/webp"
)

func init() {
	image.RegisterFormat("webp", "RIFF????WEBP", webp.Decode, webp.DecodeConfig)
}

func FileSize(size int64) string {
	const unit = 1024
	if size < unit {
		return fmt.Sprintf("%d B", size)
	}
	div, exp := int64(unit), 0
	for n := size / unit; n >= unit; n /= unit {
		div *= unit
		exp++
	}

	val := float64(size) / float64(div)
	unitStr := "KMGTPE"[exp : exp+1]
	if val == float64(int64(val)) {
		return fmt.Sprintf("%d %sB", int64(val), unitStr)
	}
	return fmt.Sprintf("%.2f %sB", val, unitStr)
}

func RemoveExtension(fileName string) string {
	if fileName == "" {
		return fileName
	}
	parts := strings.Split(fileName, ".")
	if len(parts) <= 1 {
		return fileName
	}
	return strings.Join(parts[:len(parts)-1], ".")
}

func DecodeImage(imgData []byte) (image.Image, string, error) {
	img, formatName, err := image.Decode(bytes.NewReader(imgData))
	return img, formatName, err
}

func GetImageSizeAndData(img image.Image) (int64, []byte, error) {
	var buf bytes.Buffer
	switch img.(type) {
	case *image.NRGBA, *image.RGBA, *image.YCbCr:
		err := png.Encode(&buf, img)
		if err != nil {
			return 0, nil, err
		}
	default:
		err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: 85})
		if err != nil {
			return 0, nil, err
		}
	}
	return int64(buf.Len()), buf.Bytes(), nil
}