func startPulse(filenames []string) { checkList(filenames) stdIn := make(chan string) c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) // On keyboard interrup cleanup the program go func() { for _ = range c { fmt.Println("Exiting for Keyboard Interupt") os.Exit(0) } }() pulse.Run(stdIn, email.Send) for _, filename := range filenames { line := make(chan string) file.Read(filename, line) for l := range line { stdIn <- l } } close(stdIn) }
// SendFile listens for a POST that has a form field named file and email in the body. // Using the file field we will download the specified file to the server. // The email field is used to email the user the results once algorithm is done. func SendFile(w http.ResponseWriter, r *http.Request) { // Checking to see if the request was a post. // If not return a 400: bad request if r.Method != "POST" { w.Header().Set("Content-Type", "application/json") result, _ := json.Marshal(Result{400, "bad request"}) io.WriteString(w, string(result)) return } compressed := false // Get the file field from the form in the response. // If we cannot parse it a 400 bad request is returned f, header, err := r.FormFile("file") fmt.Println("Form File") if err != nil { w.Header().Set("Content-Type", "application/json") result, _ := json.Marshal(Result{400, "bad request"}) io.WriteString(w, string(result)) return } defer f.Close() var body struct { Email string `json:"email"` } // Parse the Form in the response. // Check if the email field is a valid email. // If not return an 400: bad request r.ParseForm() body.Email = r.Form["email"][0] if !email.IsValid(body.Email) { w.Header().Set("Content-Type", "application/json") result, _ := json.Marshal(Result{400, "email not valid"}) io.WriteString(w, string(result)) return } if err != nil { w.Header().Set("Content-Type", "application/json") result, _ := json.Marshal(Result{400, "bad request"}) io.WriteString(w, string(result)) return } fmt.Println("Received bodys", body.Email) extension := filepath.Ext(header.Filename) filename := header.Filename[0 : len(header.Filename)-len(extension)] stdIn := make(chan string) email.ByPassMail = true // Needs to bypass emails and store in JSON email.OutputFile = fmt.Sprintf("%s-%s.json", filename, body.Email) email.EmailList = []string{body.Email} if _, err := os.Stat(email.OutputFile); err == nil { w.Header().Set("Content-Type", "application/json") result, _ := json.Marshal(Result{406, "file is being processed"}) io.WriteString(w, string(result)) return } spew.Dump(email.OutputFile) fmt.Println("File does not exist") if extension == ".gz" { // Load compressed file on disk out, err := os.Create(fmt.Sprintf("%s.gz", filename)) if err != nil { w.Header().Set("Content-Type", "application/json") result, _ := json.Marshal(Result{400, "bad request"}) io.WriteString(w, string(result)) return } defer out.Close() // Write the content from POST to the file _, err = io.Copy(out, f) if err != nil { w.Header().Set("Content-Type", "application/json") result, _ := json.Marshal(Result{400, "gzip copy failed"}) io.WriteString(w, string(result)) return } // Uncompress file err = file.UnGZip(fmt.Sprintf("%s.gz", filename)) if err != nil { log.Printf("api.UnGZip: %s\n", err) w.Header().Set("Content-Type", "application/json") result, _ := json.Marshal(Result{400, "gzip uncompressed failed"}) io.WriteString(w, string(result)) return } compressed = true } // Run on separat go routine so that we can give users a response on page first. go func() { // Clean up defer func() { fmt.Println("Deleting files") err = os.Remove(email.OutputFile) if err != nil { fmt.Println("Failed to delete output file, please delete") } if _, err := os.Stat(email.OutputFile); err == nil { err = os.Remove(email.OutputFile) if err != nil { fmt.Println("Failed to delete output file, please delete") } } if compressed { err := os.Remove(filename) if err != nil { fmt.Println("Failed to delete uncompressed file, please delete") } err = os.Remove(fmt.Sprintf("%s.gz", filename)) if err != nil { fmt.Println("Failed to delete uncompressed file, please delete") } } }() start := time.Now() // Start the pulse algorithm pulse.Run(stdIn, email.SaveToCache) line := make(chan string) if compressed { file.Read(filename, line) } else { file.StreamRead(f, line) } for l := range line { if l == "EOF" { email.ByPassMail = false // Once EOF, time to send email from cache JSON storage email.SendFromCache(email.OutputFile) close(stdIn) break } stdIn <- l } elapsed := time.Since(start) log.Printf("Pulse Algorithm took %s", elapsed) }() // Return a 200 success even if algorithm is still going. w.Header().Set("Content-Type", "application/json") result, _ := json.Marshal(Result{200, "success"}) io.WriteString(w, string(result)) }