This repository was archived by the owner on Mar 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.go
109 lines (86 loc) · 2 KB
/
func.go
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
package main
import (
"bytes"
"encoding/base64"
"image"
"image/png"
"net/http"
"os"
"path/filepath"
"slices"
"strings"
"golang.design/x/clipboard"
// "github.com/srwiley/rasterx"
)
func toBase64(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
}
func GetBase64FromFile(path string) string {
// Read the entire file into a byte slice
bytes, err := os.ReadFile(path)
if err != nil {
panic(err)
}
var base64Encoding string
mimeType := http.DetectContentType(bytes)
switch mimeType {
case "image/jpeg":
base64Encoding += "data:image/jpeg;base64,"
case "image/png":
base64Encoding += "data:image/png;base64,"
}
base64Encoding += toBase64(bytes)
return base64Encoding
}
// Greet returns a greeting for the given name
func GetCurrentAppDir() string {
ex, err := os.Executable()
if err != nil {
panic(err)
}
path := filepath.Dir(ex)
return path
}
func GetImages(directory string) []ImageObj {
// imagePath := a.GetCurrentAppDir() + "/images"
imagePath := directory
files, err := os.ReadDir(imagePath)
if err != nil {
var imageList []ImageObj
return imageList
} else {
var imageList []ImageObj
allowList := []string{"png", "jpg", "jpeg"}
for _, file := range files {
if !file.IsDir() {
splits := strings.Split(file.Name(), ".")
ext := splits[len(splits)-1]
if slices.Contains(allowList, ext) {
base64 := GetBase64FromFile(imagePath + "/" + file.Name())
newImageObj := ImageObj{Path: imagePath + "/" + file.Name(), Name: file.Name(), Base64: base64}
imageList = append(imageList, newImageObj)
}
}
}
return imageList
}
}
func CopyImageToClipboard(filePath string) {
f, err := os.Open(filePath)
// splits := strings.Split(filePath, ".")
// ext := splits[len(splits)-1]
if err != nil {
panic(err)
}
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
panic(err)
}
buf := new(bytes.Buffer)
err = png.Encode(buf, img)
if err != nil {
panic(err)
}
clipboard.Write(clipboard.FmtImage, buf.Bytes())
}