-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
90 lines (75 loc) · 1.92 KB
/
utils.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
package main
import (
"bytes"
"context"
"image"
"image/png"
"log"
"os"
"strings"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type File struct {
Path string `json:"path"`
}
func (a *App) SetOutDir() string {
outDir, err := runtime.OpenDirectoryDialog(a.ctx, runtime.OpenDialogOptions{})
if err != nil {
log.Fatal(err)
return "error"
}
a.ctx = context.WithValue(a.ctx, "outDir", outDir+"/")
return outDir
}
func (a *App) GetOutDir() string {
return a.ctx.Value("outDir").(string)
}
// Converts given files to format
func (a *App) FileConverterDialog() string {
files, err := runtime.OpenMultipleFilesDialog(a.ctx, runtime.OpenDialogOptions{
Filters: []runtime.FileFilter{
{
DisplayName: "Only HEIC file types are allowed.",
Pattern: "*.heic;*.HEIC",
},
},
})
if err != nil {
log.Fatal(err)
return "Error"
}
var outDir string = a.ctx.Value("outDir").(string)
for _, f := range files {
splitPath := strings.Split(f, "/")
filename := strings.Split(splitPath[len(splitPath)-1], ".")[0]
runtime.LogInfof(a.ctx, "Image info %s\n \n%s", filename, f)
file, err := os.Open(f)
if err != nil {
runtime.LogError(a.ctx, "Error opening file: "+err.Error())
return "Error"
}
img, _, err := image.Decode(file)
if err != nil {
file.Close()
runtime.LogInfof(a.ctx, "Error decoding %s", err)
return "Error"
}
// Encode the image to PNG
var out bytes.Buffer
if err := png.Encode(&out, img); err != nil {
file.Close()
runtime.LogErrorf(a.ctx, "could not encode image as PNG: %v", err)
return "Error"
}
filename = outDir + filename + ".png"
// Save the PNG data to a file
if err := os.WriteFile(filename, out.Bytes(), 0644); err != nil {
file.Close()
runtime.LogErrorf(a.ctx, "could not save PNG image as %s: %v", filename, err)
return "Error"
}
runtime.LogInfof(a.ctx, "Image successfully written to %s\n", filename)
file.Close()
}
return "Success"
}