Refactor package "convertor" "Main" method.
Moved the button for selecting a video file into a separate function.
This commit is contained in:
parent
97dd0f4b32
commit
dddbfa65bc
@ -1,6 +1,7 @@
|
|||||||
package convertor
|
package convertor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -8,7 +9,7 @@ import (
|
|||||||
|
|
||||||
type ServiceContract interface {
|
type ServiceContract interface {
|
||||||
RunConvert(setting ConvertSetting) error
|
RunConvert(setting ConvertSetting) error
|
||||||
GetTotalDuration(file File) (float64, error)
|
GetTotalDuration(file *File) (float64, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
@ -23,7 +24,7 @@ type File struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ConvertSetting struct {
|
type ConvertSetting struct {
|
||||||
VideoFileInput File
|
VideoFileInput *File
|
||||||
SocketPath string
|
SocketPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,12 +68,12 @@ func (s Service) RunConvert(setting ConvertSetting) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Service) GetTotalDuration(file File) (duration float64, err error) {
|
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
|
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, strings.Split(args, " ")...)
|
cmd := exec.Command(s.pathFFprobe, args...)
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, errors.New(strings.TrimSpace(string(out)))
|
||||||
}
|
}
|
||||||
return strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
|
return strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
type ViewContract interface {
|
type ViewContract interface {
|
||||||
Main(
|
Main(
|
||||||
runConvert func(setting HandleConvertSetting) error,
|
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 {
|
type HandleConvertSetting struct {
|
||||||
VideoFileInput File
|
VideoFileInput *File
|
||||||
SocketPath string
|
SocketPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type enableFormConversionStruct struct {
|
||||||
|
fileVideoForConversion *widget.Button
|
||||||
|
form *widget.Form
|
||||||
|
}
|
||||||
|
|
||||||
func NewView(w fyne.Window) *View {
|
func NewView(w fyne.Window) *View {
|
||||||
return &View{w}
|
return &View{w}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v View) Main(
|
func (v View) Main(
|
||||||
runConvert func(setting HandleConvertSetting) error,
|
runConvert func(setting HandleConvertSetting) error,
|
||||||
getSocketPath func(File, *widget.ProgressBar) (string, error),
|
getSocketPath func(*File, *widget.ProgressBar) (string, error),
|
||||||
) {
|
) {
|
||||||
var fileInput File
|
form := &widget.Form{}
|
||||||
var form *widget.Form
|
|
||||||
|
|
||||||
fileVideoForConversionMessage := canvas.NewText("", color.RGBA{255, 0, 0, 255})
|
|
||||||
fileVideoForConversionMessage.TextSize = 16
|
|
||||||
fileVideoForConversionMessage.TextStyle = fyne.TextStyle{Bold: true}
|
|
||||||
|
|
||||||
conversionMessage := canvas.NewText("", color.RGBA{255, 0, 0, 255})
|
conversionMessage := canvas.NewText("", color.RGBA{255, 0, 0, 255})
|
||||||
conversionMessage.TextSize = 16
|
conversionMessage.TextSize = 16
|
||||||
@ -47,7 +47,56 @@ func (v View) Main(
|
|||||||
progress := widget.NewProgressBar()
|
progress := widget.NewProgressBar()
|
||||||
progress.Hide()
|
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(
|
fileDialog := dialog.NewFileOpen(
|
||||||
func(r fyne.URIReadCloser, err error) {
|
func(r fyne.URIReadCloser, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -55,59 +104,25 @@ func (v View) Main(
|
|||||||
setStringErrorStyle(fileVideoForConversionMessage)
|
setStringErrorStyle(fileVideoForConversionMessage)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if r == nil {
|
if r == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fileInput = File{
|
fileInput.Path = r.URI().Path()
|
||||||
Path: r.URI().Path(),
|
fileInput.Name = r.URI().Name()
|
||||||
Name: r.URI().Name(),
|
fileInput.Ext = r.URI().Extension()
|
||||||
Ext: r.URI().Extension(),
|
|
||||||
}
|
|
||||||
fileVideoForConversionMessage.Text = r.URI().Path()
|
fileVideoForConversionMessage.Text = r.URI().Path()
|
||||||
setStringSuccessStyle(fileVideoForConversionMessage)
|
setStringSuccessStyle(fileVideoForConversionMessage)
|
||||||
|
|
||||||
form.Enable()
|
form.Enable()
|
||||||
|
progress.Hide()
|
||||||
|
conversionMessage.Text = ""
|
||||||
}, v.w)
|
}, v.w)
|
||||||
fileDialog.Show()
|
fileDialog.Show()
|
||||||
})
|
})
|
||||||
|
|
||||||
form = &widget.Form{
|
return button, fileVideoForConversionMessage, fileInput
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func setStringErrorStyle(text *canvas.Text) {
|
func setStringErrorStyle(text *canvas.Text) {
|
||||||
@ -119,3 +134,13 @@ func setStringSuccessStyle(text *canvas.Text) {
|
|||||||
text.Color = color.RGBA{49, 127, 114, 255}
|
text.Color = color.RGBA{49, 127, 114, 255}
|
||||||
text.Refresh()
|
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()
|
||||||
|
}
|
||||||
|
@ -38,7 +38,7 @@ func (h ConvertorHandler) GetConvertor() {
|
|||||||
h.convertorView.Main(h.runConvert, h.getSockPath)
|
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)
|
totalDuration, err := h.getTotalDuration(file)
|
||||||
|
|
||||||
if err != nil {
|
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)
|
return h.convertorService.GetTotalDuration(file)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user