1package image
2
3import (
4 "fmt"
5 "image"
6 "image/color"
7 "os"
8 "strings"
9
10 "github.com/charmbracelet/lipgloss/v2"
11 "github.com/disintegration/imaging"
12 "github.com/lucasb-eyer/go-colorful"
13)
14
15func ValidateFileSize(filePath string, sizeLimit int64) (bool, error) {
16 fileInfo, err := os.Stat(filePath)
17 if err != nil {
18 return false, fmt.Errorf("error getting file info: %w", err)
19 }
20
21 if fileInfo.Size() > sizeLimit {
22 return true, nil
23 }
24
25 return false, nil
26}
27
28func ToString(width int, img image.Image) string {
29 img = imaging.Resize(img, width, 0, imaging.Lanczos)
30 b := img.Bounds()
31 imageWidth := b.Max.X
32 h := b.Max.Y
33 str := strings.Builder{}
34
35 for heightCounter := 0; heightCounter < h; heightCounter += 2 {
36 for x := range imageWidth {
37 c1, _ := colorful.MakeColor(img.At(x, heightCounter))
38 color1 := lipgloss.Color(c1.Hex())
39
40 var color2 color.Color
41 if heightCounter+1 < h {
42 c2, _ := colorful.MakeColor(img.At(x, heightCounter+1))
43 color2 = lipgloss.Color(c2.Hex())
44 } else {
45 color2 = color1
46 }
47
48 str.WriteString(lipgloss.NewStyle().Foreground(color1).
49 Background(color2).Render("▀"))
50 }
51
52 str.WriteString("\n")
53 }
54
55 return str.String()
56}
57
58func ImagePreview(width int, filename string) (string, error) {
59 imageContent, err := os.Open(filename)
60 if err != nil {
61 return "", err
62 }
63 defer imageContent.Close()
64
65 img, _, err := image.Decode(imageContent)
66 if err != nil {
67 return "", err
68 }
69
70 imageString := ToString(width, img)
71
72 return imageString, nil
73}