-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.go
More file actions
executable file
·45 lines (39 loc) · 1 KB
/
engine.go
File metadata and controls
executable file
·45 lines (39 loc) · 1 KB
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
package main
import (
"bytes"
"os/exec"
"strings"
"github.com/AidHamza/optimizers-api/log"
)
func printCommand(cmd *exec.Cmd) {
log.Logger.Info("Executing Command", "ARGS", strings.Join(cmd.Args, " "))
}
func printError(err error) {
if err != nil {
log.Logger.Error("Executing Command", "ARGS", err.Error())
}
}
func printOutput(outs []byte) string {
if len(outs) > 0 {
return string(outs)
}
return ""
}
func compressImage(filepath, filetype string) string {
var cmd string
var args []string
if filetype == "image/jpeg" {
cmd = "jpegoptim"
args = []string{"-s", "--max=80", "--dest=" + downloadPath, uploadPath + filepath}
} else if filetype == "image/png" {
cmd = "optipng"
args = []string{"-o2", uploadPath + filepath, "-out", downloadPath + filepath}
}
cmdExec := exec.Command(cmd, args...)
cmdOutput := &bytes.Buffer{}
cmdExec.Stdout = cmdOutput
printCommand(cmdExec)
err := cmdExec.Run() // will wait for command to return
printError(err)
return printOutput(cmdOutput.Bytes())
}