2024-01-14 16:31:07 +06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-01-18 01:39:20 +06:00
|
|
|
"errors"
|
2024-01-14 16:31:07 +06:00
|
|
|
"ffmpegGui/convertor"
|
|
|
|
myError "ffmpegGui/error"
|
|
|
|
"ffmpegGui/handler"
|
2024-01-18 01:39:20 +06:00
|
|
|
"ffmpegGui/migration"
|
|
|
|
"ffmpegGui/setting"
|
2024-01-14 16:31:07 +06:00
|
|
|
"fyne.io/fyne/v2"
|
|
|
|
"fyne.io/fyne/v2/app"
|
2024-01-18 01:39:20 +06:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"os"
|
2024-01-14 16:31:07 +06:00
|
|
|
)
|
|
|
|
|
2024-01-20 01:53:25 +06:00
|
|
|
//const appVersion string = "0.1.1"
|
2024-01-14 16:31:07 +06:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
a := app.New()
|
2024-01-23 21:33:30 +06:00
|
|
|
iconResource, err := fyne.LoadResourceFromPath("icon.png")
|
|
|
|
if err == nil {
|
|
|
|
a.SetIcon(iconResource)
|
|
|
|
}
|
2024-01-14 16:31:07 +06:00
|
|
|
w := a.NewWindow("GUI FFMpeg!")
|
2024-01-18 20:23:23 +06:00
|
|
|
w.Resize(fyne.Size{Width: 800, Height: 600})
|
2024-01-20 01:53:25 +06:00
|
|
|
w.CenterOnScreen()
|
2024-01-14 16:31:07 +06:00
|
|
|
|
|
|
|
errorView := myError.NewView(w)
|
|
|
|
|
2024-01-18 01:39:20 +06:00
|
|
|
if canCreateFile("data/database") != true {
|
2024-01-18 20:23:23 +06:00
|
|
|
errorView.PanicError(errors.New("не смогли создать файл 'database' в папке 'data'"))
|
2024-01-18 01:39:20 +06:00
|
|
|
w.ShowAndRun()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := gorm.Open(sqlite.Open("data/database"), &gorm.Config{})
|
|
|
|
if err != nil {
|
|
|
|
errorView.PanicError(err)
|
|
|
|
w.ShowAndRun()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-20 14:58:16 +06:00
|
|
|
defer appCloseWithDb(db)
|
2024-01-18 01:39:20 +06:00
|
|
|
|
|
|
|
err = migration.Run(db)
|
|
|
|
if err != nil {
|
|
|
|
errorView.PanicError(err)
|
|
|
|
w.ShowAndRun()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
settingRepository := setting.NewRepository(db)
|
|
|
|
pathFFmpeg, err := settingRepository.GetValue("ffmpeg")
|
|
|
|
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) == false {
|
|
|
|
errorView.PanicError(err)
|
|
|
|
w.ShowAndRun()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pathFFprobe, err := settingRepository.GetValue("ffprobe")
|
|
|
|
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) == false {
|
|
|
|
errorView.PanicError(err)
|
|
|
|
w.ShowAndRun()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ffPathUtilities := convertor.FFPathUtilities{FFmpeg: pathFFmpeg, FFprobe: pathFFprobe}
|
2024-01-14 16:31:07 +06:00
|
|
|
|
|
|
|
convertorView := convertor.NewView(w)
|
2024-01-18 01:39:20 +06:00
|
|
|
settingView := setting.NewView(w)
|
|
|
|
convertorService := convertor.NewService(ffPathUtilities)
|
2024-01-20 14:58:16 +06:00
|
|
|
defer appCloseWithConvert(convertorService)
|
2024-01-18 01:39:20 +06:00
|
|
|
mainHandler := handler.NewConvertorHandler(convertorService, convertorView, settingView, settingRepository)
|
2024-01-14 16:31:07 +06:00
|
|
|
|
|
|
|
mainHandler.GetConvertor()
|
|
|
|
|
|
|
|
w.ShowAndRun()
|
|
|
|
}
|
2024-01-18 01:39:20 +06:00
|
|
|
|
2024-01-20 14:58:16 +06:00
|
|
|
func appCloseWithDb(db *gorm.DB) {
|
2024-01-18 01:39:20 +06:00
|
|
|
sqlDB, err := db.DB()
|
|
|
|
if err == nil {
|
|
|
|
_ = sqlDB.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-20 14:58:16 +06:00
|
|
|
func appCloseWithConvert(convertorService convertor.ServiceContract) {
|
|
|
|
for _, cmd := range convertorService.GetRunningProcesses() {
|
|
|
|
_ = cmd.Process.Kill()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 01:39:20 +06:00
|
|
|
func canCreateFile(path string) bool {
|
|
|
|
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_ = file.Close()
|
|
|
|
return true
|
|
|
|
}
|