-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.go
More file actions
129 lines (109 loc) · 3.35 KB
/
main.go
File metadata and controls
129 lines (109 loc) · 3.35 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
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
package main
import (
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"time"
)
const tempDir = "./tmp" // Directory for temporary files
func main() {
// Ensure the temporary directory exists
if err := os.MkdirAll(tempDir, os.ModePerm); err != nil {
fmt.Println("Failed to create temp directory:", err)
return
}
// Start the file cleanup goroutine
go cleanupOldFiles(tempDir, 1*time.Hour)
http.HandleFunc("/", handleHealthCheck)
http.HandleFunc("/convert", handleConvert)
fmt.Println("Starting server on :5000")
if err := http.ListenAndServe(":5000", nil); err != nil {
fmt.Println("Failed to start server:", err)
}
}
func handleHealthCheck(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
}
func handleConvert(w http.ResponseWriter, r *http.Request) {
// Ensure the request method is POST
if r.Method != http.MethodPost {
http.Error(w, "Only POST method is allowed", http.StatusMethodNotAllowed)
return
}
// Parse the uploaded file
file, _, err := r.FormFile("file")
if err != nil {
http.Error(w, "Failed to read uploaded file", http.StatusBadRequest)
return
}
defer file.Close()
// Save the Excel file to a temporary location
baseName := time.Now().Format("20060102150405") // Timestamp format
inputFilePath := filepath.Join(tempDir, baseName+".xlsx")
outputFilePath := filepath.Join(tempDir, baseName+".pdf")
inputFile, err := os.Create(inputFilePath)
if err != nil {
http.Error(w, "Failed to create temporary file", http.StatusInternalServerError)
return
}
defer inputFile.Close()
defer os.Remove(inputFilePath)
_, err = io.Copy(inputFile, file)
if err != nil {
http.Error(w, "Failed to save uploaded file", http.StatusInternalServerError)
return
}
// Convert the Excel file to PDF using LibreOffice
cmd := exec.Command("soffice", "--headless", "--convert-to", "pdf:calc_pdf_Export", inputFilePath, "--outdir", tempDir)
if err := cmd.Run(); err != nil {
fmt.Println(err)
http.Error(w, "Failed to convert file to PDF", http.StatusInternalServerError)
return
}
defer os.Remove(outputFilePath)
// Read the converted PDF file
pdfFile, err := os.Open(outputFilePath)
if err != nil {
fmt.Println(err)
http.Error(w, "Failed to read converted PDF", http.StatusInternalServerError)
return
}
defer pdfFile.Close()
// Write the PDF file as response
w.Header().Set("Content-Type", "application/pdf")
w.Header().Set("Content-Disposition", `attachment; filename="output.pdf"`)
if _, err := io.Copy(w, pdfFile); err != nil {
http.Error(w, "Failed to write PDF to response", http.StatusInternalServerError)
return
}
}
// cleanupOldFiles removes files older than the specified duration from the given directory
func cleanupOldFiles(dir string, maxAge time.Duration) {
for {
time.Sleep(1 * time.Hour) // Check every minute
files, err := os.ReadDir(dir)
if err != nil {
fmt.Println("Failed to read temp directory:", err)
continue
}
for _, file := range files {
filePath := filepath.Join(dir, file.Name())
info, err := os.Stat(filePath)
if err != nil {
fmt.Println("Failed to get file info:", err)
continue
}
// Check if the file is older than maxAge
if time.Since(info.ModTime()) > maxAge {
if err := os.Remove(filePath); err != nil {
fmt.Println("Failed to delete file:", err)
} else {
fmt.Println("Deleted old file:", filePath)
}
}
}
}
}