-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoheiv.go
132 lines (92 loc) · 2.09 KB
/
goheiv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package goheiv
import (
"errors"
"os"
"os/exec"
"strconv"
"fmt"
"bufio"
"strings"
)
type HEIVFile struct {
FilePath string
OutputPath string
Width int
Height int
Crop bool
}
func (FileHeiv *HEIVFile) SetFile(filepath string) *HEIVFile {
if _, err := os.Stat(filepath); err != nil {
panic(err)
}
FileHeiv.FilePath = filepath
return FileHeiv
}
func (FileHeiv *HEIVFile) SetOutput(filepath string) *HEIVFile {
FileHeiv.OutputPath = filepath
return FileHeiv
}
func (FileHeiv *HEIVFile) SetWidth(width int) *HEIVFile {
FileHeiv.Width = width
return FileHeiv
}
func (FileHeiv *HEIVFile) SetHeight(height int) *HEIVFile {
FileHeiv.Height = height
return FileHeiv
}
func (FileHeiv *HEIVFile) UseCrop() *HEIVFile {
FileHeiv.Crop = true
return FileHeiv
}
func (FileHeiv *HEIVFile) Convert() (string, error) {
cmdName, err := exec.LookPath("tifig")
if err != nil {
return "", errors.New("tifig is not installed")
}
var args []string
args = append(args,"-v")
args = append(args, "-p")
if FileHeiv.Crop {
args = append(args, "--crop")
}
if FileHeiv.Width > 0 {
args = append(args, "--width")
args = append(args, strconv.Itoa(FileHeiv.Width))
}
if FileHeiv.Height > 0 {
args = append(args, "--height")
args = append(args, strconv.Itoa(FileHeiv.Height))
}
if FileHeiv.FilePath == "" {
return "", errors.New("File path is required")
}
if FileHeiv.OutputPath == "" {
return "", errors.New("Output file path is required")
}
args = append(args, FileHeiv.FilePath)
args = append(args, FileHeiv.OutputPath)
cmd := exec.Command(cmdName, args...)
cmdReader, err := cmd.StdoutPipe()
if err != nil {
fmt.Println("this 1")
return "", err
}
scanner := bufio.NewScanner(cmdReader)
var outputCmd []string
go func() {
for scanner.Scan() {
outputCmd = append(outputCmd, scanner.Text())
}
}()
err = cmd.Start()
if err != nil {
fmt.Fprintln(os.Stderr, "Error starting Cmd", err)
return "", err
}
err = cmd.Wait()
if err != nil {
fmt.Fprintln(os.Stderr, "Error waiting for Cmd", err)
return "", err
}
return strings.Join(outputCmd,"\n"),nil
}