86 lines
1.4 KiB
Go
86 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
_ "image/jpeg"
|
|
_ "image/png"
|
|
"os"
|
|
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
"gitnet.fr/deblan/go-imager/pkg/img"
|
|
)
|
|
|
|
type OpenFileResult struct {
|
|
Path string `json:"path"`
|
|
Type string `json:"type"`
|
|
Base64 string `json:"base64"`
|
|
Error bool `json:"error"`
|
|
}
|
|
|
|
type App struct {
|
|
ctx context.Context
|
|
}
|
|
|
|
func NewApp() *App {
|
|
return &App{}
|
|
}
|
|
|
|
func (a *App) startup(ctx context.Context) {
|
|
a.ctx = ctx
|
|
}
|
|
|
|
func (a *App) OpenImageFile() *img.ImageInfo {
|
|
file, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
|
|
Filters: []runtime.FileFilter{
|
|
runtime.FileFilter{
|
|
Pattern: "*.jpg;*.png;*.jpeg;*.JPG;*.PNG;*.JPEG",
|
|
DisplayName: "Images",
|
|
},
|
|
},
|
|
})
|
|
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
return img.GetImageInfo(file)
|
|
}
|
|
|
|
func (a *App) SaveImageFile(src img.ImageInfo) error {
|
|
file, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
|
|
DefaultFilename: "result." + src.Type,
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
content, err := base64.StdEncoding.DecodeString(src.Base64)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
writer, err := os.Create(file)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer writer.Close()
|
|
|
|
if _, err := writer.Write(content); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := writer.Sync(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (a *App) ApplyEffects(src img.ImageInfo, effects []img.ImageEffect) *img.ImageInfo {
|
|
return img.ApplyFilters(&src, effects)
|
|
}
|