-
Notifications
You must be signed in to change notification settings - Fork 0
/
gk.go
59 lines (50 loc) · 1.11 KB
/
gk.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
/*
* @Author: Xier
* @Date: 2015-02-02 12:34:26
* @Last Modified by: Xier
* @Last Modified time: 2015-02-03 10:00:11
*/
package gika
import (
"bytes"
"errors"
"io"
"os/exec"
"strings"
"time"
)
// Convert document to plain text
func DocToText(in io.Reader, out io.Writer) error {
cmd := exec.Command("java", "-jar", "./bin/tika-app-1.7.jar", "-t")
stderr := bytes.NewBuffer(nil)
cmd.Stdin = in
cmd.Stdout = out
cmd.Stderr = stderr
cmd.Start()
cmdDone := make(chan error, 1)
go func() {
cmdDone <- cmd.Wait()
}()
select {
case <-time.After(time.Duration(500000) * time.Millisecond):
if err := cmd.Process.Kill(); err != nil {
return errors.New(err.Error())
}
<-cmdDone
return errors.New("Command timed out")
case err := <-cmdDone:
if err != nil {
return errors.New(stderr.String())
}
}
return nil
}
func IsSupport(fileName string) bool {
paths := strings.Split(fileName, "/")
fileType := strings.Split(paths[len(paths)-1], ".")
switch fileType[len(fileType)-1] {
case "doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf", "epub", "html", "xml":
return true
}
return false
}