package main import ( "encoding/json" "fmt" "io" "log/slog" "net/http" "os" "path/filepath" "regexp" "sort" "strings" "sync" "time" "github.com/go-chi/chi/v5" ) var ( draftImageRegex = regexp.MustCompile(`/api/comparison-drafts/([a-zA-Z0-9_\-]+)/images/([a-zA-Z0-9_\-\.]+)`) compImageRegex = regexp.MustCompile(`/api/comparison/([a-zA-Z0-9_\-]+)/images/([a-zA-Z0-9_\-\.]+)`) ) type DraftItem struct { Index int `json:"index"` Status any `json:"status"` Latency int64 `json:"latency"` InputFilename string `json:"inputFilename"` OutputFilename string `json:"outputFilename"` ContentType string `json:"contentType"` BeforeImage string `json:"beforeImage"` AfterImage string `json:"afterImage"` TextContent string `json:"textContent"` HasBefore bool `json:"hasBefore"` HasAfter bool `json:"hasAfter"` SourceDraftID string `json:"sourceDraftId,omitempty"` } type ComparisonDraft struct { ID string `json:"id"` Title string `json:"title"` Notes string `json:"notes"` Endpoint string `json:"endpoint"` SpaceName string `json:"spaceName"` CreatedAt string `json:"createdAt"` UpdatedAt string `json:"updatedAt"` Total int `json:"total"` SuccessCount int `json:"successCount"` ErrorCount int `json:"errorCount"` AvgLatency int64 `json:"avgLatency"` Items []DraftItem `json:"items"` } type SaveDraftRequest struct { ID string `json:"id,omitempty"` Title string `json:"title"` Notes string `json:"notes"` Endpoint string `json:"endpoint"` SpaceName string `json:"spaceName"` Items []ShareRequestItem `json:"items"` } type CombineDraftsRequest struct { Heading string `json:"heading"` Items []CombineItemInput `json:"items"` } type CombineItemInput struct { Index int `json:"index"` Status any `json:"status"` Latency int64 `json:"latency"` InputFilename string `json:"inputFilename"` OutputFilename string `json:"outputFilename"` ContentType string `json:"contentType"` BeforeImage string `json:"beforeImage"` AfterImage string `json:"afterImage"` BeforeData string `json:"beforeData"` AfterData string `json:"afterData"` TextContent string `json:"textContent"` SourceDraftID string `json:"sourceDraftId"` } func copyFile(src, dst string) error { in, err := os.Open(src) if err != nil { return err } defer in.Close() if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil { return err } out, err := os.Create(dst) if err != nil { return err } defer out.Close() _, err = io.Copy(out, in) return err } func linkOrCopyFile(src, dst string) error { if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil { return err } // Try hard link first for instant zero-copy linking if err := os.Link(src, dst); err == nil { return nil } // Fallback to copy if cross-filesystem or link fails return copyFile(src, dst) } func resolveDiskPathFromURL(rawURL string) string { if rawURL == "" { return "" } if m := draftImageRegex.FindStringSubmatch(rawURL); len(m) == 3 { p := filepath.Join("data", "bulk-api-tester", "drafts", m[1], "images", filepath.Base(m[2])) if _, err := os.Stat(p); err == nil { return p } } if m := compImageRegex.FindStringSubmatch(rawURL); len(m) == 3 { p := filepath.Join("data", "bulk-api-tester", "comparisons", m[1], "images", filepath.Base(m[2])) if _, err := os.Stat(p); err == nil { return p } } return "" } func listComparisonDraftsHandler(w http.ResponseWriter, r *http.Request) { draftsDir := filepath.Join("data", "bulk-api-tester", "drafts") entries, err := os.ReadDir(draftsDir) if err != nil { writeJSON(w, http.StatusOK, []ComparisonDraft{}) return } drafts := []ComparisonDraft{} for _, entry := range entries { if !entry.IsDir() { continue } id := entry.Name() if !validIDRegex.MatchString(id) { continue } metaPath := filepath.Join(draftsDir, id, "meta.json") var d ComparisonDraft if err := readJSON(metaPath, &d); err == nil && d.ID != "" { drafts = append(drafts, d) } } // Sort newest first sort.Slice(drafts, func(i, j int) bool { return drafts[i].CreatedAt > drafts[j].CreatedAt }) w.Header().Set("Cache-Control", "no-cache, must-revalidate") writeJSON(w, http.StatusOK, drafts) } func getComparisonDraftHandler(w http.ResponseWriter, r *http.Request) { id := chi.URLParam(r, "id") if id == "" || !validIDRegex.MatchString(id) { writeJSON(w, http.StatusBadRequest, map[string]any{"error": "Invalid draft ID"}) return } metaPath := filepath.Join("data", "bulk-api-tester", "drafts", id, "meta.json") var draft ComparisonDraft if err := readJSON(metaPath, &draft); err != nil { writeJSON(w, http.StatusNotFound, map[string]any{"error": "Draft not found"}) return } w.Header().Set("Cache-Control", "no-cache, must-revalidate") writeJSON(w, http.StatusOK, draft) } func saveComparisonDraftHandler(w http.ResponseWriter, r *http.Request) { // Set 500MB limit on request body r.Body = http.MaxBytesReader(w, r.Body, 500*1024*1024) // High-speed direct binary streaming via multipart if strings.HasPrefix(r.Header.Get("Content-Type"), "multipart/form-data") { handleMultipartDraft(w, r) return } var req SaveDraftRequest if err := json.NewDecoder(r.Body).Decode(&req); err != nil { slog.Error("failed to decode save draft request", "error", err) writeJSON(w, http.StatusBadRequest, map[string]any{"success": false, "error": "Invalid request body: " + err.Error()}) return } if len(req.Items) == 0 { writeJSON(w, http.StatusBadRequest, map[string]any{"success": false, "error": "No items provided for comparison draft"}) return } draftID := req.ID if draftID == "" || !validIDRegex.MatchString(draftID) { draftID = fmt.Sprintf("draft_%x_%s", time.Now().Unix(), randomHexString(4)) } draftDir := filepath.Join("data", "bulk-api-tester", "drafts", draftID) imagesDir := filepath.Join(draftDir, "images") if err := os.MkdirAll(imagesDir, 0755); err != nil { slog.Error("failed to create draft directory", "dir", draftDir, "error", err) writeJSON(w, http.StatusInternalServerError, map[string]any{"success": false, "error": "Failed to create storage directory"}) return } var items []DraftItem var totalLatency int64 successCount := 0 errorCount := 0 for i, it := range req.Items { itemIndex := it.Index if itemIndex <= 0 { itemIndex = i + 1 } dItem := DraftItem{ Index: itemIndex, Status: it.Status, Latency: it.Latency, InputFilename: it.InputFilename, OutputFilename: it.OutputFilename, ContentType: it.ContentType, TextContent: it.TextContent, SourceDraftID: draftID, } if isSuccessStatus(it.Status) { successCount++ } else { errorCount++ } totalLatency += it.Latency // Save Before Image if it.BeforeData != "" { beforeBytes, ext, err := decodeImageData(it.BeforeData) if err == nil && len(beforeBytes) > 0 { filename := fmt.Sprintf("before_%d.%s", itemIndex, ext) filePath := filepath.Join(imagesDir, filename) if err := os.WriteFile(filePath, beforeBytes, 0644); err == nil { dItem.BeforeImage = fmt.Sprintf("/api/comparison-drafts/%s/images/%s", draftID, filename) dItem.HasBefore = true } } } // Save After Image if it.AfterData != "" { afterBytes, ext, err := decodeImageData(it.AfterData) if err == nil && len(afterBytes) > 0 { filename := fmt.Sprintf("after_%d.%s", itemIndex, ext) filePath := filepath.Join(imagesDir, filename) if err := os.WriteFile(filePath, afterBytes, 0644); err == nil { dItem.AfterImage = fmt.Sprintf("/api/comparison-drafts/%s/images/%s", draftID, filename) dItem.HasAfter = true } } } items = append(items, dItem) } title := strings.TrimSpace(req.Title) if title == "" { title = fmt.Sprintf("Draft Run (%d results) - %s", len(items), time.Now().Format("Jan 02, 15:04")) } var avgLatency int64 if len(items) > 0 { avgLatency = totalLatency / int64(len(items)) } nowStr := time.Now().UTC().Format(time.RFC3339) draftData := ComparisonDraft{ ID: draftID, Title: title, Notes: req.Notes, Endpoint: req.Endpoint, SpaceName: req.SpaceName, CreatedAt: nowStr, UpdatedAt: nowStr, Total: len(items), SuccessCount: successCount, ErrorCount: errorCount, AvgLatency: avgLatency, Items: items, } metaPath := filepath.Join(draftDir, "meta.json") if err := atomicWriteJSON(metaPath, draftData); err != nil { slog.Error("failed to save draft metadata", "path", metaPath, "error", err) writeJSON(w, http.StatusInternalServerError, map[string]any{"success": false, "error": "Failed to save draft metadata"}) return } slog.Info("saved comparison draft", "draft_id", draftID, "items_count", len(items)) writeJSON(w, http.StatusOK, map[string]any{ "success": true, "draftId": draftID, "draft": draftData, }) } func handleMultipartDraft(w http.ResponseWriter, r *http.Request) { mr, err := r.MultipartReader() if err != nil { slog.Error("failed to create multipart reader for draft", "error", err) writeJSON(w, http.StatusBadRequest, map[string]any{"success": false, "error": "Invalid multipart payload"}) return } draftID := fmt.Sprintf("draft_%x_%s", time.Now().Unix(), randomHexString(4)) draftDir := filepath.Join("data", "bulk-api-tester", "drafts", draftID) imagesDir := filepath.Join(draftDir, "images") if err := os.MkdirAll(imagesDir, 0755); err != nil { slog.Error("failed to create draft directory", "dir", draftDir, "error", err) writeJSON(w, http.StatusInternalServerError, map[string]any{"success": false, "error": "Failed to create storage directory"}) return } var metaReq SaveDraftRequest filesMap := make(map[string]string) // formName -> savedFilename for { part, err := mr.NextPart() if err == io.EOF { break } if err != nil { slog.Warn("error reading draft multipart part", "error", err) break } formName := part.FormName() if formName == "meta" { metaBytes, _ := io.ReadAll(part) json.Unmarshal(metaBytes, &metaReq) part.Close() continue } if strings.HasPrefix(formName, "before_") || strings.HasPrefix(formName, "after_") { fileName := part.FileName() ext := strings.ToLower(filepath.Ext(fileName)) if ext == "" { ext = ".png" } saveName := fmt.Sprintf("%s%s", formName, ext) filePath := filepath.Join(imagesDir, saveName) outFile, err := os.Create(filePath) if err == nil { io.Copy(outFile, part) outFile.Close() filesMap[formName] = saveName } else { slog.Warn("failed to write draft multipart image", "path", filePath, "error", err) } part.Close() continue } part.Close() } if metaReq.ID != "" && validIDRegex.MatchString(metaReq.ID) { if metaReq.ID != draftID { targetDir := filepath.Join("data", "bulk-api-tester", "drafts", metaReq.ID) if err := os.Rename(draftDir, targetDir); err == nil { draftID = metaReq.ID draftDir = targetDir imagesDir = filepath.Join(draftDir, "images") } } } var items []DraftItem var totalLatency int64 successCount := 0 errorCount := 0 for i, it := range metaReq.Items { itemIndex := it.Index if itemIndex <= 0 { itemIndex = i + 1 } dItem := DraftItem{ Index: itemIndex, Status: it.Status, Latency: it.Latency, InputFilename: it.InputFilename, OutputFilename: it.OutputFilename, ContentType: it.ContentType, TextContent: it.TextContent, SourceDraftID: draftID, } if isSuccessStatus(it.Status) { successCount++ } else { errorCount++ } totalLatency += it.Latency beforeKey := fmt.Sprintf("before_%d", itemIndex) if saveName, ok := filesMap[beforeKey]; ok { dItem.BeforeImage = fmt.Sprintf("/api/comparison-drafts/%s/images/%s", draftID, saveName) dItem.HasBefore = true } else if it.BeforeData != "" { beforeBytes, ext, err := decodeImageData(it.BeforeData) if err == nil && len(beforeBytes) > 0 { filename := fmt.Sprintf("before_%d.%s", itemIndex, ext) filePath := filepath.Join(imagesDir, filename) if err := os.WriteFile(filePath, beforeBytes, 0644); err == nil { dItem.BeforeImage = fmt.Sprintf("/api/comparison-drafts/%s/images/%s", draftID, filename) dItem.HasBefore = true } } } afterKey := fmt.Sprintf("after_%d", itemIndex) if saveName, ok := filesMap[afterKey]; ok { dItem.AfterImage = fmt.Sprintf("/api/comparison-drafts/%s/images/%s", draftID, saveName) dItem.HasAfter = true } else if it.AfterData != "" { afterBytes, ext, err := decodeImageData(it.AfterData) if err == nil && len(afterBytes) > 0 { filename := fmt.Sprintf("after_%d.%s", itemIndex, ext) filePath := filepath.Join(imagesDir, filename) if err := os.WriteFile(filePath, afterBytes, 0644); err == nil { dItem.AfterImage = fmt.Sprintf("/api/comparison-drafts/%s/images/%s", draftID, filename) dItem.HasAfter = true } } } items = append(items, dItem) } title := strings.TrimSpace(metaReq.Title) if title == "" { title = fmt.Sprintf("Draft Run (%d results) - %s", len(items), time.Now().Format("Jan 02, 15:04")) } var avgLatency int64 if len(items) > 0 { avgLatency = totalLatency / int64(len(items)) } nowStr := time.Now().UTC().Format(time.RFC3339) draftData := ComparisonDraft{ ID: draftID, Title: title, Notes: metaReq.Notes, Endpoint: metaReq.Endpoint, SpaceName: metaReq.SpaceName, CreatedAt: nowStr, UpdatedAt: nowStr, Total: len(items), SuccessCount: successCount, ErrorCount: errorCount, AvgLatency: avgLatency, Items: items, } metaPath := filepath.Join(draftDir, "meta.json") if err := atomicWriteJSON(metaPath, draftData); err != nil { slog.Error("failed to save draft metadata", "path", metaPath, "error", err) writeJSON(w, http.StatusInternalServerError, map[string]any{"success": false, "error": "Failed to save draft metadata"}) return } slog.Info("saved comparison draft via fast multipart", "draft_id", draftID, "items_count", len(items)) writeJSON(w, http.StatusOK, map[string]any{ "success": true, "draftId": draftID, "draft": draftData, }) } func deleteComparisonDraftHandler(w http.ResponseWriter, r *http.Request) { id := chi.URLParam(r, "id") if id == "" || !validIDRegex.MatchString(id) { writeJSON(w, http.StatusBadRequest, map[string]any{"error": "Invalid draft ID"}) return } draftDir := filepath.Join("data", "bulk-api-tester", "drafts", id) if err := os.RemoveAll(draftDir); err != nil { slog.Error("failed to delete draft", "dir", draftDir, "error", err) writeJSON(w, http.StatusInternalServerError, map[string]any{"success": false, "error": "Failed to delete draft"}) return } slog.Info("deleted comparison draft", "draft_id", id) writeJSON(w, http.StatusOK, map[string]any{"success": true, "draftId": id}) } func getDraftImageHandler(w http.ResponseWriter, r *http.Request) { id := chi.URLParam(r, "id") file := chi.URLParam(r, "file") if id == "" || !validIDRegex.MatchString(id) || file == "" { http.Error(w, "Invalid parameters", http.StatusBadRequest) return } cleanFile := filepath.Base(file) if cleanFile != file { http.Error(w, "Invalid filename", http.StatusBadRequest) return } filePath := filepath.Join("data", "bulk-api-tester", "drafts", id, "images", cleanFile) if _, err := os.Stat(filePath); os.IsNotExist(err) { http.Error(w, "Draft image not found", http.StatusNotFound) return } ext := strings.ToLower(filepath.Ext(cleanFile)) contentType := "application/octet-stream" switch ext { case ".png": contentType = "image/png" case ".jpg", ".jpeg": contentType = "image/jpeg" case ".webp": contentType = "image/webp" case ".gif": contentType = "image/gif" case ".svg": contentType = "image/svg+xml" case ".avif": contentType = "image/avif" } w.Header().Set("Content-Type", contentType) w.Header().Set("Cache-Control", "public, max-age=31536000, immutable") http.ServeFile(w, r, filePath) } func combineDraftsHandler(w http.ResponseWriter, r *http.Request) { // Set 500MB limit on request body r.Body = http.MaxBytesReader(w, r.Body, 500*1024*1024) var req CombineDraftsRequest if err := json.NewDecoder(r.Body).Decode(&req); err != nil { slog.Error("failed to decode combine drafts request", "error", err) writeJSON(w, http.StatusBadRequest, map[string]any{"success": false, "error": "Invalid request body: " + err.Error()}) return } if len(req.Items) == 0 { writeJSON(w, http.StatusBadRequest, map[string]any{"success": false, "error": "No items provided to combine"}) return } // Create new shareable comparison ID shareID := fmt.Sprintf("cmp_%x_%s", time.Now().Unix(), randomHexString(4)) compDir := filepath.Join("data", "bulk-api-tester", "comparisons", shareID) imagesDir := filepath.Join(compDir, "images") if err := os.MkdirAll(imagesDir, 0755); err != nil { slog.Error("failed to create combined comparison directory", "dir", compDir, "error", err) writeJSON(w, http.StatusInternalServerError, map[string]any{"success": false, "error": "Failed to create comparison directory"}) return } type fileTask struct { src string dst string } var linkTasks []fileTask items := make([]ComparisonItem, len(req.Items)) var totalLatency int64 successCount := 0 errorCount := 0 for i, it := range req.Items { newIndex := i + 1 cItem := ComparisonItem{ Index: newIndex, Status: it.Status, Latency: it.Latency, InputFilename: it.InputFilename, OutputFilename: it.OutputFilename, ContentType: it.ContentType, TextContent: it.TextContent, } if isSuccessStatus(it.Status) { successCount++ } else { errorCount++ } totalLatency += it.Latency // Handle Before Image if it.BeforeData != "" { beforeBytes, ext, err := decodeImageData(it.BeforeData) if err == nil && len(beforeBytes) > 0 { filename := fmt.Sprintf("before_%d.%s", newIndex, ext) dst := filepath.Join(imagesDir, filename) if err := os.WriteFile(dst, beforeBytes, 0644); err == nil { cItem.BeforeImage = fmt.Sprintf("/api/comparison/%s/images/%s", shareID, filename) cItem.HasBefore = true } } } else if it.BeforeImage != "" { srcDiskPath := resolveDiskPathFromURL(it.BeforeImage) if srcDiskPath != "" { ext := strings.ToLower(filepath.Ext(srcDiskPath)) if ext == "" { ext = ".png" } filename := fmt.Sprintf("before_%d%s", newIndex, ext) dst := filepath.Join(imagesDir, filename) linkTasks = append(linkTasks, fileTask{src: srcDiskPath, dst: dst}) cItem.BeforeImage = fmt.Sprintf("/api/comparison/%s/images/%s", shareID, filename) cItem.HasBefore = true } } // Handle After Image if it.AfterData != "" { afterBytes, ext, err := decodeImageData(it.AfterData) if err == nil && len(afterBytes) > 0 { filename := fmt.Sprintf("after_%d.%s", newIndex, ext) dst := filepath.Join(imagesDir, filename) if err := os.WriteFile(dst, afterBytes, 0644); err == nil { cItem.AfterImage = fmt.Sprintf("/api/comparison/%s/images/%s", shareID, filename) cItem.HasAfter = true } } } else if it.AfterImage != "" { srcDiskPath := resolveDiskPathFromURL(it.AfterImage) if srcDiskPath != "" { ext := strings.ToLower(filepath.Ext(srcDiskPath)) if ext == "" { ext = ".png" } filename := fmt.Sprintf("after_%d%s", newIndex, ext) dst := filepath.Join(imagesDir, filename) linkTasks = append(linkTasks, fileTask{src: srcDiskPath, dst: dst}) cItem.AfterImage = fmt.Sprintf("/api/comparison/%s/images/%s", shareID, filename) cItem.HasAfter = true } } items[i] = cItem } // Concurrently hard-link or copy files with worker pool if len(linkTasks) > 0 { var wg sync.WaitGroup sem := make(chan struct{}, 16) for _, task := range linkTasks { wg.Add(1) go func(t fileTask) { defer wg.Done() sem <- struct{}{} defer func() { <-sem }() if err := linkOrCopyFile(t.src, t.dst); err != nil { slog.Warn("failed to link/copy draft image", "src", t.src, "dst", t.dst, "error", err) } }(task) } wg.Wait() } heading := strings.TrimSpace(req.Heading) if heading == "" { heading = fmt.Sprintf("Combined Comparison (%d results)", len(items)) } var avgLatency int64 if len(items) > 0 { avgLatency = totalLatency / int64(len(items)) } shareData := ComparisonShare{ ID: shareID, Title: heading, Endpoint: "Combined Multi-Draft Run", SpaceName: "Drafts Combiner", CreatedAt: time.Now().UTC().Format(time.RFC3339), Total: len(items), SuccessCount: successCount, ErrorCount: errorCount, AvgLatency: avgLatency, Items: items, } metaPath := filepath.Join(compDir, "meta.json") if err := atomicWriteJSON(metaPath, shareData); err != nil { slog.Error("failed to save combined comparison meta", "path", metaPath, "error", err) writeJSON(w, http.StatusInternalServerError, map[string]any{"success": false, "error": "Failed to save comparison meta"}) return } slog.Info("created combined comparison share", "share_id", shareID, "heading", heading, "items_count", len(items)) writeJSON(w, http.StatusOK, map[string]any{ "success": true, "shareId": shareID, "url": "/tool/bulk-api-tester/compare/" + shareID, "directUrl": "/compare/" + shareID, "heading": heading, "total": len(items), }) }