-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (63 loc) · 1.54 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
"golang.org/x/exp/slices"
)
var tpl = template.Must(template.ParseFiles("./view/main.gohtml"))
const (
UPLOAD_FOLDER = "./files"
)
var ALLOWED_EXTENSIONS = []string{".doc", ".docx"}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "9091"
}
mux := http.NewServeMux()
mux.HandleFunc("/", indexHandeler)
http.ListenAndServe((":" + port), mux)
}
func indexHandeler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
tpl.Execute(w, nil)
case "POST":
r.ParseForm()
file, handler, err := r.FormFile("docFile")
if err != nil {
fmt.Println("Error retrieving the file")
fmt.Println(err)
return
}
defer file.Close()
if slices.Contains(ALLOWED_EXTENSIONS, filepath.Ext(handler.Filename)) {
tempFile, err := ioutil.TempFile(UPLOAD_FOLDER, "*.docx")
if err != nil {
fmt.Println(err)
}
defer tempFile.Close()
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println(err)
}
tempFile.Write(fileBytes)
convertFile((UPLOAD_FOLDER + "/" + handler.Filename))
newFilePath := strings.Replace(UPLOAD_FOLDER+"/"+handler.Filename, ".docx", "", 1)
pdfFile, _ := ioutil.ReadFile(newFilePath)
w.Header().Add("Content-Type", "application/octet-stream")
w.Write(pdfFile)
} else {
tpl.Execute(w, nil)
}
}
}
func convertFile(inputFile string) {
exec.Command("libreoffice --headless --convert-to pdf --outdir " + UPLOAD_FOLDER + inputFile).Output()
}