-
Notifications
You must be signed in to change notification settings - Fork 1
/
main1.go
329 lines (276 loc) · 9.38 KB
/
main1.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package main
// import (
// "flag"
// "fmt"
// "net/http"
// "os"
// "os/exec"
// "strconv"
// "strings"
// "time"
// )
// //Convertor is the custom type to hold the docx to pdf conversion values
// type PDFConvertor struct {
// inputFile string
// method string
// }
// func main1() {
// //Check if input file is docx
// //Check if libreoffice or textutil/cupsfilter is present
// //
// if len(os.Args) == 3 {
// pdfConv := PDFConvertor{inputFile: os.Args[1], method: os.Args[2]}
// //First solution
// if pdfConv.method == "1" {
// err := ConvertUsingTextUtil(pdfConv.inputFile)
// if err != nil {
// fmt.Println(err)
// }
// return
// } else if pdfConv.method == "2" {
// err := ConvertUsingLibreOffice(pdfConv.inputFile)
// if err != nil {
// fmt.Println(err)
// }
// return
// } else {
// fmt.Println("Please enter the method as 1 or 2")
// }
// } else {
// fmt.Println("Please enter the files as argument and method")
// }
// // } else if len(os.Args) == 2 {
// // //Second solution
// // //soffice --headless --convert-to pdf sample.docx
// // err := ConvertUsingLibreOffice(os.Args[1])
// // if err != nil {
// // fmt.Println(err)
// // }
// // }
// }
// func main2() {
// // ch := make(chan int, 100)
// for i := 0; i < 10; i++ {
// fmt.Println("started ", i, " routine")
// go RConvertUsingLibreOffice("sample" + strconv.Itoa(i) + ".docx")
// }
// }
// // ConvertUsingTextUtil uses textutil and cupsfilter to convert docs to pdf
// // textutil -convert html -output "sample.html" sample.docx
// // cupsfilter sample.html > sample.pdf
// func ConvertUsingTextUtil(inputFile string) error {
// tempOutputHTMLFile := strings.Replace(inputFile, ".docx", ".html", -1)
// outputFile := strings.Replace(inputFile, ".docx", ".pdf", -1)
// docxToHTMLCmd := exec.Command("bash", "-c", "textutil -convert html -output "+tempOutputHTMLFile+" "+inputFile)
// docxToHTMLCmdErr := docxToHTMLCmd.Run()
// if docxToHTMLCmdErr != nil {
// fmt.Println("1.Conversion issue to pdf")
// return docxToHTMLCmdErr
// } else {
// htmlToPdfCmd := exec.Command("bash", "-c", "cupsfilter "+tempOutputHTMLFile+" > "+outputFile)
// htmlToPdfCmdErr := htmlToPdfCmd.Run()
// if htmlToPdfCmdErr != nil {
// fmt.Println("2.Conversion issue to pdf")
// return htmlToPdfCmdErr
// }
// }
// return nil
// }
// //ConvertUsingLibreOffice will use libreoffice to convert the docsx to pdf
// func ConvertUsingLibreOffice(inputFile string) error {
// executable := "libreoffice"
// if os.Getenv("GOOS") == "darwin" || os.Getenv("GOOS") == "" {
// executable = "/Applications/LibreOffice.app/Contents/MacOS/soffice"
// }
// fmt.Println(os.Getenv("GOOS"))
// fmt.Println(executable)
// docxToPDFCmd := exec.Command("bash", "-c", executable+" --headless --convert-to pdf "+inputFile)
// docxToPDFCmdErr := docxToPDFCmd.Run()
// if docxToPDFCmdErr != nil {
// fmt.Println("1.Conversion issue to pdf", docxToPDFCmdErr)
// return docxToPDFCmdErr
// }
// return nil
// }
// //ValidateInputFile is to validate if the inputfile value is correct
// func (c *PDFConvertor) ValidateInputFile() (bool, error) {
// if c.inputFile == "" {
// err := fmt.Errorf("Convertor| InputFile Empty")
// return false, err
// }
// if strings.Index(c.inputFile, ".docx") == -1 {
// err := fmt.Errorf("Convertor| InputFile is not in docx format")
// return false, err
// }
// return true, nil
// }
// // GO ROUTINES
// type WorkRequest struct {
// Name string
// Delay time.Duration
// File string
// }
// //COLLECTOR
// // A buffered channel that we can send work requests on.
// var WorkQueue = make(chan WorkRequest, 100)
// func Collector(w http.ResponseWriter, r *http.Request) {
// // Make sure we can only be called with an HTTP POST request.
// if r.Method != "POST" {
// w.Header().Set("Allow", "POST")
// w.WriteHeader(http.StatusMethodNotAllowed)
// return
// }
// // Parse the delay.
// delay, err := time.ParseDuration(r.FormValue("delay"))
// if err != nil {
// http.Error(w, "Bad delay value: "+err.Error(), http.StatusBadRequest)
// return
// }
// // Check to make sure the delay is anywhere from 1 to 10 seconds.
// if delay.Seconds() < 1 || delay.Seconds() > 10 {
// http.Error(w, "The delay must be between 1 and 10 seconds, inclusively.", http.StatusBadRequest)
// return
// }
// // Now, we retrieve the person's name from the request.
// name := r.FormValue("name")
// // Just do a quick bit of sanity checking to make sure the client actually provided us with a name.
// if name == "" {
// http.Error(w, "You must specify a name.", http.StatusBadRequest)
// return
// }
// // Now, we retrieve the person's name from the request.
// filename := r.FormValue("filename")
// // Just do a quick bit of sanity checking to make sure the client actually provided us with a name.
// if filename == "" {
// http.Error(w, "You must specify a docx filename.", http.StatusBadRequest)
// return
// }
// CollectorJob(name, delay, filename)
// // And let the user know their work request was created.
// w.WriteHeader(http.StatusCreated)
// return
// }
// // CollectorJob is the function to be called for PDF generation with requestor name of work and filename with path for docx
// func CollectorJob(name string, delay time.Duration, filename string) {
// // Now, we take the delay, and the person's name, and make a WorkRequest out of them.
// work := WorkRequest{Name: name, Delay: delay, File: filename}
// // Push the work onto the queue.
// WorkQueue <- work
// fmt.Println("Work request queued for PDF generation")
// }
// ///////WORKER
// // NewWorker creates, and returns a new Worker object. Its only argument
// // is a channel that the worker can add itself to whenever it is done its
// // work.
// func NewWorker(id int, workerQueue chan chan WorkRequest) Worker {
// // Create, and return the worker.
// worker := Worker{
// ID: id,
// Work: make(chan WorkRequest),
// WorkerQueue: workerQueue,
// QuitChan: make(chan bool)}
// return worker
// }
// type Worker struct {
// ID int
// Work chan WorkRequest
// WorkerQueue chan chan WorkRequest
// QuitChan chan bool
// }
// // This function "starts" the worker by starting a goroutine, that is
// // an infinite "for-select" loop.
// func (w *Worker) Start() {
// go func() {
// for {
// // Add ourselves into the worker queue.
// w.WorkerQueue <- w.Work
// select {
// case work := <-w.Work:
// // Receive a work request.
// fmt.Printf("worker%d: Received work request, delaying for %f seconds\n", w.ID, work.Delay.Seconds())
// //time.Sleep(work.Delay)
// fmt.Printf("Before Generating file:%s", work.File)
// err := RConvertUsingLibreOffice(work.File)
// if err != nil {
// fmt.Printf("Error Generating file:%s", work.File)
// // Failure recovery code here
// } else {
// fmt.Printf("Generated file:%s", work.File)
// }
// fmt.Printf("worker%d: Hello, %s!\n", w.ID, work.Name)
// case <-w.QuitChan:
// // We have been asked to stop.
// fmt.Printf("worker%d stopping\n", w.ID)
// return
// }
// }
// }()
// }
// //ConvertUsingLibreOffice will use libreoffice to convert the docsx to pdf
// func RConvertUsingLibreOffice(inputFile string) error {
// executable := "libreoffice"
// if os.Getenv("GOOS") == "darwin" || os.Getenv("GOOS") == "" {
// executable = "/Applications/LibreOffice.app/Contents/MacOS/soffice"
// }
// fmt.Println(os.Getenv("GOOS"))
// fmt.Println(executable)
// docxToPDFCmd := exec.Command("bash", "-c", executable+" --headless --convert-to pdf "+inputFile)
// docxToPDFCmdErr := docxToPDFCmd.Run()
// if docxToPDFCmdErr != nil {
// fmt.Println("1.Conversion issue to pdf", docxToPDFCmdErr)
// return docxToPDFCmdErr
// }
// return nil
// }
// // Stop tells the worker to stop listening for work requests.
// //
// // Note that the worker will only stop *after* it has finished its work.
// func (w *Worker) Stop() {
// go func() {
// w.QuitChan <- true
// }()
// }
// //DISPATCHER
// var WorkerQueue chan chan WorkRequest
// func StartDispatcher(nworkers int) {
// // First, initialize the channel we are going to but the workers' work channels into.
// WorkerQueue = make(chan chan WorkRequest, nworkers)
// // Now, create all of our workers.
// for i := 0; i < nworkers; i++ {
// fmt.Println("Starting worker", i+1)
// worker := NewWorker(i+1, WorkerQueue)
// worker.Start()
// }
// go func() {
// for {
// select {
// case work := <-WorkQueue:
// fmt.Println("Received work requeust")
// go func() {
// worker := <-WorkerQueue
// fmt.Println("Dispatching work request")
// worker <- work
// }()
// }
// }
// }()
// }
// var (
// NWorkers = flag.Int("n", 1, "The number of workers to start")
// HTTPAddr = flag.String("http", "127.0.0.1:8000", "Address to listen for HTTP requests on")
// )
// func main3() {
// // Parse the command-line flags.
// flag.Parse()
// // Start the dispatcher.
// fmt.Println("Starting the dispatcher")
// StartDispatcher(*NWorkers)
// // Register our collector as an HTTP handler function.
// fmt.Println("Registering the collector")
// http.HandleFunc("/work", Collector)
// // Start the HTTP server!
// fmt.Println("HTTP server listening on", *HTTPAddr)
// if err := http.ListenAndServe(*HTTPAddr, nil); err != nil {
// fmt.Println(err.Error())
// }
// }