package web import ( "bytes" "encoding/csv" "encoding/json" "fmt" "io" "log" "math" "net/http" "sort" "strings" "sync" "time" root "marmic/servicetrade-toolbox" "marmic/servicetrade-toolbox/internal/api" "marmic/servicetrade-toolbox/internal/middleware" ) // DocumentsHandler handles the document upload page func DocumentsHandler(w http.ResponseWriter, r *http.Request) { session, ok := r.Context().Value(middleware.SessionKey).(*api.Session) if !ok { http.Error(w, "Unauthorized", http.StatusUnauthorized) return } tmpl := root.WebTemplates data := map[string]interface{}{ "Title": "Document Uploads", "Session": session, } if r.Header.Get("HX-Request") == "true" { // For HTMX requests, just send the document_upload partial if err := tmpl.ExecuteTemplate(w, "document_upload", data); err != nil { log.Printf("Template execution error: %v", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } } else { // For full page requests, first render document_upload into a buffer var contentBuf bytes.Buffer if err := tmpl.ExecuteTemplate(&contentBuf, "document_upload", data); err != nil { log.Printf("Template execution error: %v", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } // Add the rendered content to the data for the layout data["BodyContent"] = contentBuf.String() // Now render the layout with our content if err := tmpl.ExecuteTemplate(w, "layout.html", data); err != nil { log.Printf("Template execution error: %v", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } } } // ProcessCSVHandler processes a CSV file with job numbers func ProcessCSVHandler(w http.ResponseWriter, r *http.Request) { _, ok := r.Context().Value(middleware.SessionKey).(*api.Session) if !ok { http.Error(w, "Unauthorized", http.StatusUnauthorized) return } // Check if the request method is POST if r.Method != http.MethodPost { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } // Parse the multipart form data with a 10MB limit if err := r.ParseMultipartForm(10 << 20); err != nil { http.Error(w, "Unable to parse form: "+err.Error(), http.StatusBadRequest) return } // Get the file from the form file, _, err := r.FormFile("csvFile") if err != nil { http.Error(w, "Error retrieving file: "+err.Error(), http.StatusBadRequest) return } defer file.Close() // Read the CSV data csvData, err := csv.NewReader(file).ReadAll() if err != nil { http.Error(w, "Error reading CSV file: "+err.Error(), http.StatusBadRequest) return } if len(csvData) < 2 { http.Error(w, "CSV file must contain at least a header row and one data row", http.StatusBadRequest) return } // Find the index of the 'id' column headerRow := csvData[0] idColumnIndex := -1 for i, header := range headerRow { if strings.ToLower(strings.TrimSpace(header)) == "id" { idColumnIndex = i break } } // If 'id' column not found, try the first column if idColumnIndex == -1 { idColumnIndex = 0 log.Printf("No 'id' column found in CSV, using first column (header: %s)", headerRow[0]) } else { log.Printf("Found 'id' column at index %d", idColumnIndex) } // Extract job numbers from the CSV var jobNumbers []string for rowIndex, row := range csvData { // Skip header row if rowIndex == 0 { continue } if len(row) > idColumnIndex { // Extract and clean up the job ID jobID := strings.TrimSpace(row[idColumnIndex]) if jobID != "" { jobNumbers = append(jobNumbers, jobID) } } } totalJobs := len(jobNumbers) log.Printf("Extracted %d job IDs from CSV", totalJobs) if totalJobs == 0 { http.Error(w, "No valid job IDs found in the CSV file", http.StatusBadRequest) return } // Create a hidden input with the job IDs jobsValue := strings.Join(jobNumbers, ",") // Generate HTML for the main response (hidden input for job-ids-container) var responseHTML bytes.Buffer responseHTML.WriteString(fmt.Sprintf(``, jobsValue)) responseHTML.WriteString(fmt.Sprintf(`
Found %d job(s) in the CSV file
`, totalJobs)) // Generate out-of-band swap for the preview section - simplified version responseHTML.WriteString(fmt.Sprintf(`Upload to %d job(s)
No files were processed. If you selected files, please ensure some are active for upload.
")) return } // Validate that the lengths of metadata arrays match the number of files if len(fileHeaders) != len(displayNames) || len(fileHeaders) != len(documentTypes) || len(fileHeaders) != len(isActiveFlags) { log.Printf("Metadata array length mismatch. Files: %d, Names: %d, Types: %d, ActiveFlags: %d", len(fileHeaders), len(displayNames), len(documentTypes), len(isActiveFlags)) http.Error(w, "Mismatch in file metadata. Please clear selection and try uploading files again.", http.StatusBadRequest) return } type FileToUploadMetadata struct { OriginalFilename string DisplayName string Type string Content []byte // Store file content in memory } var filesToUpload []FileToUploadMetadata for i, fileHeader := range fileHeaders { if !isActiveFlags[i] { log.Printf("Skipping file %s (original index %d) as it's marked inactive.", fileHeader.Filename, i) continue // Skip inactive files } uploadedFile, err := fileHeader.Open() if err != nil { log.Printf("Error opening uploaded file %s (original index %d): %v. Skipping.", fileHeader.Filename, i, err) // No need to close uploadedFile here as it wasn't successfully opened. continue } displayName := displayNames[i] docType := documentTypes[i] if strings.TrimSpace(displayName) == "" { displayName = fileHeader.Filename // Fallback to original filename log.Printf("Warning: Empty display name for file %s (original index %d), using original filename.", fileHeader.Filename, i) } // Basic validation for docType could be added here if necessary, e.g., ensure it's not empty. if strings.TrimSpace(docType) == "" { docType = "1" // Fallback to a default type log.Printf("Warning: Empty document type for file %s (original index %d, display name %s), using default type '1'.", fileHeader.Filename, i, displayName) } // Read file content into memory fileBytes, err := io.ReadAll(uploadedFile) if err != nil { log.Printf("Error reading content of uploaded file %s (original index %d, display name %s): %v. Skipping.", fileHeader.Filename, i, displayName, err) uploadedFile.Close() continue } uploadedFile.Close() // Close the multipart file handle after reading its content metadata := FileToUploadMetadata{ OriginalFilename: fileHeader.Filename, DisplayName: displayName, Type: docType, Content: fileBytes, } filesToUpload = append(filesToUpload, metadata) } activeFilesProcessedCount := len(filesToUpload) if activeFilesProcessedCount == 0 { log.Println("No active files to upload after filtering.") // Send a user-friendly message back. The resultHTML later will also reflect this. w.Write([]byte("No active files were selected for upload. Please ensure files are selected and not marked as removed.
")) return } log.Printf("Total active files prepared for upload: %d", activeFilesProcessedCount) const maxConcurrent = 5 const requestDelay = 300 * time.Millisecond type UploadResult struct { JobID string DocName string Success bool Error string Data map[string]interface{} FileSize int64 } totalUploads := len(jobs) * activeFilesProcessedCount resultsChan := make(chan UploadResult, totalUploads) var wg sync.WaitGroup semaphore := make(chan struct{}, maxConcurrent) log.Printf("Starting up to %d concurrent upload workers for %d total uploads (%d jobs x %d active files)", maxConcurrent, totalUploads, len(jobs), activeFilesProcessedCount) for _, jobID := range jobs { for _, metadataToUpload := range filesToUpload { currentUploadMetadata := metadataToUpload wg.Add(1) go func(jobID string, meta FileToUploadMetadata) { defer wg.Done() semaphore <- struct{}{} defer func() { <-semaphore // No temp file to remove here as content is in memory }() time.Sleep(requestDelay) fileNameForUpload := meta.DisplayName fileReaderForUpload := io.NopCloser(bytes.NewReader(meta.Content)) expectedSize := int64(len(meta.Content)) // Error handling for fileReaderForUpload (e.g. if meta.Content is nil) is implicitly handled by API call failures later // but good to be mindful. For now, assume meta.Content is valid if it reached here. if len(jobs) > 10 { jitter := time.Duration(100+(time.Now().UnixNano()%400)) * time.Millisecond time.Sleep(jitter) } sizeTracker := &readCloserWithSize{reader: fileReaderForUpload, size: 0} log.Printf("Goroutine Info: Starting to stream in-memory data (original: %s, uploading as %s, type: %s, size: %.2f MB) to job %s", meta.OriginalFilename, fileNameForUpload, meta.Type, float64(expectedSize)/(1024*1024), jobID) // Define uploadStart here for per-goroutine timing uploadStartGoroutine := time.Now() uploadResultData, errUpload := session.UploadAttachmentFile(jobID, fileNameForUpload, meta.Type, sizeTracker) uploadDuration := time.Since(uploadStartGoroutine) fileSizeUploaded := sizeTracker.Size() sizeMatch := true if expectedSize > 0 && math.Abs(float64(expectedSize-fileSizeUploaded)) > float64(expectedSize)*0.05 { sizeMatch = false log.Printf("Goroutine WARNING: Size mismatch for in-memory data (original: %s, uploaded as %s) to job %s. Expected: %d, Uploaded: %d", meta.OriginalFilename, fileNameForUpload, jobID, expectedSize, fileSizeUploaded) } if errUpload != nil { log.Printf("Goroutine Error: Uploading in-memory data (original: %s, as %s) to job %s failed after %v: %v", meta.OriginalFilename, fileNameForUpload, jobID, uploadDuration, errUpload) resultsChan <- UploadResult{ JobID: jobID, DocName: fileNameForUpload, Success: false, Error: errUpload.Error(), FileSize: fileSizeUploaded, } } else if !sizeMatch { log.Printf("Goroutine Error: Upload of in-memory data (original: %s, as %s) to job %s appears corrupted. API reported success but file sizes mismatch.", meta.OriginalFilename, fileNameForUpload, jobID) resultsChan <- UploadResult{ JobID: jobID, DocName: fileNameForUpload, Success: false, Error: "Upload appears corrupted (file size mismatch)", FileSize: fileSizeUploaded, } } else { log.Printf("Goroutine Success: Uploaded in-memory data (original: %s, %.2f MB, as %s, type: %s) to job %s in %v", meta.OriginalFilename, float64(fileSizeUploaded)/(1024*1024), fileNameForUpload, meta.Type, jobID, uploadDuration) resultsChan <- UploadResult{ JobID: jobID, DocName: fileNameForUpload, Success: true, Data: uploadResultData, FileSize: fileSizeUploaded, } } }(jobID, currentUploadMetadata) } } go func() { wg.Wait() close(resultsChan) log.Println("All upload goroutines finished.") }() results := make(map[string][]UploadResult) resultsCount := 0 var totalBytesUploaded int64 for result := range resultsChan { resultsCount++ log.Printf("Received result %d/%d: Job %s, File %s, Success: %v, Size: %.2f MB", resultsCount, totalUploads, result.JobID, result.DocName, result.Success, float64(result.FileSize)/(1024*1024)) if result.Success { totalBytesUploaded += result.FileSize } if _, exists := results[result.JobID]; !exists { results[result.JobID] = []UploadResult{} } results[result.JobID] = append(results[result.JobID], result) } log.Printf("All results collected. Total: %d, Total bytes uploaded: %.2f MB", resultsCount, float64(totalBytesUploaded)/(1024*1024)) var resultHTML bytes.Buffer var totalSuccess, totalFailure int for _, jobResults := range results { for _, result := range jobResults { if result.Success { totalSuccess++ } else { totalFailure++ } } } resultHTML.WriteString("All documents were successfully uploaded to ServiceTrade!
") } else if resultsCount == 0 { resultHTML.WriteString("No documents were processed for upload.
") } else { resultHTML.WriteString("Some documents failed to upload. See details below.
") } resultHTML.WriteString("No file upload results for this job.
") } resultHTML.WriteString("