forked from HeavyHorst/unoconv-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unoconv.go
53 lines (46 loc) · 854 Bytes
/
unoconv.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
package main
import (
"io"
"os/exec"
)
type request struct {
filename string
filetype string
w io.Writer
errChan chan error
}
type unoconv struct {
requestChan chan request
}
func initUnoconv() *unoconv {
uno := new(unoconv)
uno.requestChan = make(chan request)
//unoconv can only process one file at a time
go func(uno *unoconv) {
for {
select {
case data := <-uno.requestChan:
cmd := exec.Command("unoconv", "-f", data.filetype, "--stdout", data.filename)
cmd.Stdout = data.w
err := cmd.Run()
if err != nil {
data.errChan <- err
} else {
data.errChan <- nil
}
}
}
}(uno)
return uno
}
func (u *unoconv) convert(filename, filetype string, w io.Writer) error {
err := make(chan error)
req := request{
filename,
filetype,
w,
err,
}
u.requestChan <- req
return <-err
}