Refactor package "convertor" "Main" method.

Moved the button for selecting a video file into a separate function.
This commit is contained in:
Leonid Nikitin 2024-01-15 20:28:02 +06:00
parent 97dd0f4b32
commit dddbfa65bc
Signed by: kor-elf
GPG Key ID: 7DE8F80C5CEC2C0D
3 changed files with 85 additions and 59 deletions

View File

@ -1,6 +1,7 @@
package convertor
import (
"errors"
"os/exec"
"strconv"
"strings"
@ -8,7 +9,7 @@ import (
type ServiceContract interface {
RunConvert(setting ConvertSetting) error
GetTotalDuration(file File) (float64, error)
GetTotalDuration(file *File) (float64, error)
}
type Service struct {
@ -23,7 +24,7 @@ type File struct {
}
type ConvertSetting struct {
VideoFileInput File
VideoFileInput *File
SocketPath string
}
@ -67,12 +68,12 @@ func (s Service) RunConvert(setting ConvertSetting) error {
return nil
}
func (s Service) GetTotalDuration(file File) (duration float64, err error) {
args := "-v error -select_streams v:0 -count_packets -show_entries stream=nb_read_packets -of csv=p=0 " + file.Path
cmd := exec.Command(s.pathFFprobe, strings.Split(args, " ")...)
func (s Service) GetTotalDuration(file *File) (duration float64, err error) {
args := []string{"-v", "error", "-select_streams", "v:0", "-count_packets", "-show_entries", "stream=nb_read_packets", "-of", "csv=p=0", file.Path}
cmd := exec.Command(s.pathFFprobe, args...)
out, err := cmd.CombinedOutput()
if err != nil {
return 0, err
return 0, errors.New(strings.TrimSpace(string(out)))
}
return strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
}

View File

@ -12,7 +12,7 @@ import (
type ViewContract interface {
Main(
runConvert func(setting HandleConvertSetting) error,
getSocketPath func(File, *widget.ProgressBar) (string, error),
getSocketPath func(*File, *widget.ProgressBar) (string, error),
)
}
@ -21,24 +21,24 @@ type View struct {
}
type HandleConvertSetting struct {
VideoFileInput File
VideoFileInput *File
SocketPath string
}
type enableFormConversionStruct struct {
fileVideoForConversion *widget.Button
form *widget.Form
}
func NewView(w fyne.Window) *View {
return &View{w}
}
func (v View) Main(
runConvert func(setting HandleConvertSetting) error,
getSocketPath func(File, *widget.ProgressBar) (string, error),
getSocketPath func(*File, *widget.ProgressBar) (string, error),
) {
var fileInput File
var form *widget.Form
fileVideoForConversionMessage := canvas.NewText("", color.RGBA{255, 0, 0, 255})
fileVideoForConversionMessage.TextSize = 16
fileVideoForConversionMessage.TextStyle = fyne.TextStyle{Bold: true}
form := &widget.Form{}
conversionMessage := canvas.NewText("", color.RGBA{255, 0, 0, 255})
conversionMessage.TextSize = 16
@ -47,7 +47,56 @@ func (v View) Main(
progress := widget.NewProgressBar()
progress.Hide()
fileVideoForConversion := widget.NewButton("выбрать", func() {
fileVideoForConversion, fileVideoForConversionMessage, fileInput := v.getButtonFileVideoForConversion(form, progress, conversionMessage)
form.Items = []*widget.FormItem{
{Text: "Файл для ковертации:", Widget: fileVideoForConversion},
{Widget: fileVideoForConversionMessage},
}
form.SubmitText = "Конвертировать"
enableFormConversionStruct := enableFormConversionStruct{
fileVideoForConversion: fileVideoForConversion,
form: form,
}
form.OnSubmit = func() {
fileVideoForConversion.Disable()
form.Disable()
socketPath, err := getSocketPath(fileInput, progress)
if err != nil {
showConversionMessage(conversionMessage, err)
enableFormConversion(enableFormConversionStruct)
return
}
setting := HandleConvertSetting{
VideoFileInput: fileInput,
SocketPath: socketPath,
}
err = runConvert(setting)
if err != nil {
showConversionMessage(conversionMessage, err)
enableFormConversion(enableFormConversionStruct)
return
}
enableFormConversion(enableFormConversionStruct)
}
v.w.SetContent(widget.NewCard("Конвертор видео файлов", "", container.NewVBox(form, conversionMessage, progress)))
form.Disable()
}
func (v View) getButtonFileVideoForConversion(form *widget.Form, progress *widget.ProgressBar, conversionMessage *canvas.Text) (*widget.Button, *canvas.Text, *File) {
fileInput := &File{}
fileVideoForConversionMessage := canvas.NewText("", color.RGBA{255, 0, 0, 255})
fileVideoForConversionMessage.TextSize = 16
fileVideoForConversionMessage.TextStyle = fyne.TextStyle{Bold: true}
button := widget.NewButton("выбрать", func() {
fileDialog := dialog.NewFileOpen(
func(r fyne.URIReadCloser, err error) {
if err != nil {
@ -55,59 +104,25 @@ func (v View) Main(
setStringErrorStyle(fileVideoForConversionMessage)
return
}
if r == nil {
return
}
fileInput = File{
Path: r.URI().Path(),
Name: r.URI().Name(),
Ext: r.URI().Extension(),
}
fileInput.Path = r.URI().Path()
fileInput.Name = r.URI().Name()
fileInput.Ext = r.URI().Extension()
fileVideoForConversionMessage.Text = r.URI().Path()
setStringSuccessStyle(fileVideoForConversionMessage)
form.Enable()
progress.Hide()
conversionMessage.Text = ""
}, v.w)
fileDialog.Show()
})
form = &widget.Form{
Items: []*widget.FormItem{
{Text: "Файл для ковертации:", Widget: fileVideoForConversion},
{Widget: fileVideoForConversionMessage},
},
SubmitText: "Конвертировать",
OnSubmit: func() {
fileVideoForConversion.Disable()
form.Disable()
socketPath, err := getSocketPath(fileInput, progress)
if err != nil {
conversionMessage.Text = err.Error()
setStringErrorStyle(conversionMessage)
fileVideoForConversion.Enable()
form.Enable()
}
setting := HandleConvertSetting{
VideoFileInput: fileInput,
SocketPath: socketPath,
}
err = runConvert(setting)
if err != nil {
conversionMessage.Text = err.Error()
setStringErrorStyle(conversionMessage)
}
fileVideoForConversion.Enable()
form.Enable()
},
}
v.w.SetContent(widget.NewCard("Конвертор видео файлов", "", container.NewVBox(form, conversionMessage, progress)))
form.Disable()
return button, fileVideoForConversionMessage, fileInput
}
func setStringErrorStyle(text *canvas.Text) {
@ -119,3 +134,13 @@ func setStringSuccessStyle(text *canvas.Text) {
text.Color = color.RGBA{49, 127, 114, 255}
text.Refresh()
}
func showConversionMessage(conversionMessage *canvas.Text, err error) {
conversionMessage.Text = err.Error()
setStringErrorStyle(conversionMessage)
}
func enableFormConversion(enableFormConversionStruct enableFormConversionStruct) {
enableFormConversionStruct.fileVideoForConversion.Enable()
enableFormConversionStruct.form.Enable()
}

View File

@ -38,7 +38,7 @@ func (h ConvertorHandler) GetConvertor() {
h.convertorView.Main(h.runConvert, h.getSockPath)
}
func (h ConvertorHandler) getSockPath(file convertor.File, progressbar *widget.ProgressBar) (string, error) {
func (h ConvertorHandler) getSockPath(file *convertor.File, progressbar *widget.ProgressBar) (string, error) {
totalDuration, err := h.getTotalDuration(file)
if err != nil {
@ -101,6 +101,6 @@ func (h ConvertorHandler) runConvert(setting convertor.HandleConvertSetting) err
)
}
func (h ConvertorHandler) getTotalDuration(file convertor.File) (float64, error) {
func (h ConvertorHandler) getTotalDuration(file *convertor.File) (float64, error) {
return h.convertorService.GetTotalDuration(file)
}