|  |  | package main
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | import (
 | 
						
						
						
							|  |  | 	"context"
 | 
						
						
						
							|  |  | 	"encoding/json"
 | 
						
						
						
							|  |  | 	"fmt"
 | 
						
						
						
							|  |  | 	"io/fs"
 | 
						
						
						
							|  |  | 	"os"
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | 	"path/filepath"
 | 
						
						
						
							|  |  | 	//"time"
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | 	"github.com/estebangarcia21/subprocess"
 | 
						
						
						
							|  |  | 	"github.com/wailsapp/wails/v2/pkg/runtime"
 | 
						
						
						
							|  |  | 	"golang.org/x/sys/windows"
 | 
						
						
						
							|  |  | )
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | type Server struct {
 | 
						
						
						
							|  |  | 	Name string `json:"name"`
 | 
						
						
						
							|  |  | 	Dir  string `json:"dir"`
 | 
						
						
						
							|  |  | 	Cmd  string `json:"cmd"`
 | 
						
						
						
							|  |  | 	Url  string `json:"url"`
 | 
						
						
						
							|  |  | 	Port string `json:"port"`
 | 
						
						
						
							|  |  | 	Run  string `json:"run"`
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | // Запускает web-сервер (локальный)
 | 
						
						
						
							|  |  | func (s *Server) Start() {
 | 
						
						
						
							|  |  | 	os.Chdir(s.Dir)
 | 
						
						
						
							|  |  | 	//s := subprocess.New(".\\"+name+".exe")
 | 
						
						
						
							|  |  | 	p := subprocess.New(s.Cmd)
 | 
						
						
						
							|  |  | 	p.ExecAsync()
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | // App struct
 | 
						
						
						
							|  |  | type App struct {
 | 
						
						
						
							|  |  | 	ctx context.Context
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | // NewApp создает новый экземпляр App
 | 
						
						
						
							|  |  | func NewApp() *App {
 | 
						
						
						
							|  |  | 	app := &App{}
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | 	return app
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | // startup вызывается при запуске приложения
 | 
						
						
						
							|  |  | func (a *App) startup(ctx context.Context) {
 | 
						
						
						
							|  |  | 	a.ctx = ctx
 | 
						
						
						
							|  |  | 	runtime.WindowSetTitle(ctx, "v1.0.2")
 | 
						
						
						
							|  |  | 	runtime.WindowSetPosition(ctx, 1500, 80)
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | // GetProjects возвращает список последних проектов
 | 
						
						
						
							|  |  | func (a *App) GetProjects() []Project {
 | 
						
						
						
							|  |  | 	projects := getLastProjects()
 | 
						
						
						
							|  |  | 	runtime.LogInfo(a.ctx, fmt.Sprintf("projects: %v", projects))
 | 
						
						
						
							|  |  | 	return projects
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | type Disk struct {
 | 
						
						
						
							|  |  | 	Name string
 | 
						
						
						
							|  |  | 	Free uint64
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | func getFileCountAndSize(dir string) (int, int64, error) {
 | 
						
						
						
							|  |  | 	var fileCount int
 | 
						
						
						
							|  |  | 	var totalSize int64
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | 	err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
 | 
						
						
						
							|  |  | 		if err != nil {
 | 
						
						
						
							|  |  | 			return err
 | 
						
						
						
							|  |  | 		}
 | 
						
						
						
							|  |  | 		if !info.IsDir() { // Проверяем, что это не каталог
 | 
						
						
						
							|  |  | 			fileCount++
 | 
						
						
						
							|  |  | 			totalSize += info.Size() // Добавляем размер файла
 | 
						
						
						
							|  |  | 		}
 | 
						
						
						
							|  |  | 		return nil
 | 
						
						
						
							|  |  | 	})
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | 	if err != nil {
 | 
						
						
						
							|  |  | 		return 0, 0, err
 | 
						
						
						
							|  |  | 	}
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | 	return fileCount, totalSize, nil
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | func (a *App) GetDownloads() string {
 | 
						
						
						
							|  |  | 	cnt, size, err := getFileCountAndSize("C:\\Users\\admin\\Downloads")
 | 
						
						
						
							|  |  | 	checkError("getDownloads", err)
 | 
						
						
						
							|  |  | 	info := fmt.Sprintf("%d (%d Mb)", cnt, size/1024/1024)
 | 
						
						
						
							|  |  | 	return info
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | // StartFarDownloads - запускает far
 | 
						
						
						
							|  |  | func (a *App) StartFarDownloads() string {
 | 
						
						
						
							|  |  | 	cmd := `far "D:\soft" "C:\Users\admin\Downloads"`
 | 
						
						
						
							|  |  | 	far := subprocess.New(cmd)
 | 
						
						
						
							|  |  | 	far.ExecAsync()
 | 
						
						
						
							|  |  | 	// runtime.LogInfo(a.ctx, "команда выполнена успешно")
 | 
						
						
						
							|  |  | 	return "ok"
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | // StartFarBooks - запускает far для fb2
 | 
						
						
						
							|  |  | func (a *App) StartFarBooks() string {
 | 
						
						
						
							|  |  | 	cmd := `far "D:\books\2\fb2" "D:\books\2\fb2\images"`
 | 
						
						
						
							|  |  | 	far := subprocess.New(cmd)
 | 
						
						
						
							|  |  | 	far.ExecAsync()
 | 
						
						
						
							|  |  | 	alacritty := subprocess.New("alacritty --working-directory D:/books/2/fb2")
 | 
						
						
						
							|  |  | 	alacritty.ExecAsync()
 | 
						
						
						
							|  |  | 	// runtime.LogInfo(a.ctx, "команда выполнена успешно")
 | 
						
						
						
							|  |  | 	return "ok"
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | // StartFarDisks - запускает far для работы с дисками
 | 
						
						
						
							|  |  | func (a *App) StartFarDisks() string {
 | 
						
						
						
							|  |  | 	cmd := `far "D:\" "C:\"`
 | 
						
						
						
							|  |  | 	far := subprocess.New(cmd)
 | 
						
						
						
							|  |  | 	far.ExecAsync()
 | 
						
						
						
							|  |  | 	// runtime.LogInfo(a.ctx, "команда выполнена успешно")
 | 
						
						
						
							|  |  | 	return "ok"
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | func (a *App) GetDisks() []Disk {
 | 
						
						
						
							|  |  | 	var disks = []Disk{}
 | 
						
						
						
							|  |  | 	var freeBytes, totalBytes, totalFreeBytes uint64
 | 
						
						
						
							|  |  | 	err := windows.GetDiskFreeSpaceEx(windows.StringToUTF16Ptr("C:\\"), &freeBytes, &totalBytes, &totalFreeBytes)
 | 
						
						
						
							|  |  | 	checkError("free space c:", err)
 | 
						
						
						
							|  |  | 	free_C := freeBytes / 1024 / 1024 / 1024
 | 
						
						
						
							|  |  | 	disks = append(disks, Disk{Name: "C:", Free: free_C})
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | 	err = windows.GetDiskFreeSpaceEx(windows.StringToUTF16Ptr("D:\\"), &freeBytes, &totalBytes, &totalFreeBytes)
 | 
						
						
						
							|  |  | 	checkError("free space d:", err)
 | 
						
						
						
							|  |  | 	free_D := freeBytes / 1024 / 1024 / 1024
 | 
						
						
						
							|  |  | 	disks = append(disks, Disk{Name: "D:", Free: free_D})
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | 	return disks
 | 
						
						
						
							|  |  | 	//fmt.Printf("Свободное место на диске: %dGB\n", freeGB)
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | // Greet returns a greeting for the given name
 | 
						
						
						
							|  |  | func (a *App) Greet(name string) string {
 | 
						
						
						
							|  |  | 	return fmt.Sprintf("Hello %s, It's show time!", name)
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | func startLocalServer(name, dir, cmd string) {
 | 
						
						
						
							|  |  | 	//os.Chdir("d:\\projects\\" + name);
 | 
						
						
						
							|  |  | 	os.Chdir(dir)
 | 
						
						
						
							|  |  | 	//s := subprocess.New(".\\"+name+".exe")
 | 
						
						
						
							|  |  | 	s := subprocess.New(cmd)
 | 
						
						
						
							|  |  | 	s.ExecAsync()
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | // StartServer - запускает сервер на выполнение
 | 
						
						
						
							|  |  | func (a *App) StartServer(s string) string {
 | 
						
						
						
							|  |  | 	runtime.LogInfo(a.ctx, s)
 | 
						
						
						
							|  |  | 	var server Server
 | 
						
						
						
							|  |  | 	if err := json.Unmarshal([]byte(s), &server); err != nil {
 | 
						
						
						
							|  |  | 		runtime.LogError(a.ctx, err.Error())
 | 
						
						
						
							|  |  | 	}
 | 
						
						
						
							|  |  | 	runtime.LogInfo(a.ctx, fmt.Sprintf("server: %v", server))
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | 	server.Url = fmt.Sprintf("http://localhost:%s", server.Port)
 | 
						
						
						
							|  |  | 	server.Start()
 | 
						
						
						
							|  |  | 	runtime.BrowserOpenURL(a.ctx, server.Url)
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | 	return "starting server"
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | // StartFar - запускает far
 | 
						
						
						
							|  |  | func (a *App) StartFar(s string) string {
 | 
						
						
						
							|  |  | 	runtime.LogInfo(a.ctx, s)
 | 
						
						
						
							|  |  | 	cmd := fmt.Sprintf("far \"%s\" \"%s\"", s, s)
 | 
						
						
						
							|  |  | 	far := subprocess.New(cmd)
 | 
						
						
						
							|  |  | 	err := far.Exec()
 | 
						
						
						
							|  |  | 	if err != nil {
 | 
						
						
						
							|  |  | 		runtime.LogError(a.ctx, err.Error())
 | 
						
						
						
							|  |  | 	}
 | 
						
						
						
							|  |  | 	runtime.LogInfo(a.ctx, fmt.Sprintf("команда выполнена успешно"))
 | 
						
						
						
							|  |  | 	return "ok"
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | func (a *App) Weed() {
 | 
						
						
						
							|  |  | 	os.Chdir("d:\\projects\\nano\\sw4-nano\\util")
 | 
						
						
						
							|  |  | 	//s := subprocess.New(".\\"+name+".exe")
 | 
						
						
						
							|  |  | 	cmd := "py scanpics.py"
 | 
						
						
						
							|  |  | 	s := subprocess.New(cmd)
 | 
						
						
						
							|  |  | 	err := s.Exec()
 | 
						
						
						
							|  |  | 	if err != nil {
 | 
						
						
						
							|  |  | 		runtime.LogError(a.ctx, err.Error())
 | 
						
						
						
							|  |  | 	}
 | 
						
						
						
							|  |  | 	runtime.LogInfo(a.ctx, "команда выполнена успешно")
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | func getCountPics(dir string) int {
 | 
						
						
						
							|  |  | 	pics, err := os.ReadDir(dir)
 | 
						
						
						
							|  |  | 	if err != nil {
 | 
						
						
						
							|  |  | 		return -1
 | 
						
						
						
							|  |  | 	}
 | 
						
						
						
							|  |  | 	return len(pics)
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | // The `getCountReady` function is counting the number of files (excluding directories) in a
 | 
						
						
						
							|  |  | // specified directory. It uses the `fs.WalkDir` function to walk through the file tree starting
 | 
						
						
						
							|  |  | // from the given directory within a file system. For each file encountered during the walk, it
 | 
						
						
						
							|  |  | // increments a counter if the file is not a directory. Finally, it returns the total count of
 | 
						
						
						
							|  |  | // files found in the directory.
 | 
						
						
						
							|  |  | func getCountReady(ctx context.Context, dir string) int {
 | 
						
						
						
							|  |  | 	root := dir
 | 
						
						
						
							|  |  | 	fileSystem := os.DirFS(root)
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | 	cnt := 0
 | 
						
						
						
							|  |  | 	fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
 | 
						
						
						
							|  |  | 		if err != nil {
 | 
						
						
						
							|  |  | 			runtime.LogError(ctx, err.Error())
 | 
						
						
						
							|  |  | 		}
 | 
						
						
						
							|  |  | 		if !d.IsDir() {
 | 
						
						
						
							|  |  | 			cnt += 1
 | 
						
						
						
							|  |  | 		}
 | 
						
						
						
							|  |  | 		return nil
 | 
						
						
						
							|  |  | 	})
 | 
						
						
						
							|  |  | 	return cnt
 | 
						
						
						
							|  |  | }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | func (a *App) GetStatus() string {
 | 
						
						
						
							|  |  | 	pics := getCountPics("d:\\pics")
 | 
						
						
						
							|  |  | 	ready := getCountReady(a.ctx, "d:\\pics-move")
 | 
						
						
						
							|  |  | 	return fmt.Sprintf("%d / %d", pics, ready)
 | 
						
						
						
							|  |  | }
 |