feat(image-to-text): unify single/multi/folder intake into studio, chunked streaming queue, and fixed download options
This commit is contained in:
@@ -674,6 +674,124 @@ func handleExportZip(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
type ExportZipDirectRequest struct {
|
||||
JobName string `json:"job_name"`
|
||||
Items []struct {
|
||||
Filename string `json:"filename"`
|
||||
Text string `json:"text"`
|
||||
Status string `json:"status"`
|
||||
Elapsed float64 `json:"elapsed"`
|
||||
} `json:"items"`
|
||||
}
|
||||
|
||||
func handleExportZipDirect(w http.ResponseWriter, r *http.Request) {
|
||||
var req ExportZipDirectRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
jobName := req.JobName
|
||||
if jobName == "" {
|
||||
jobName = "vision_dataset"
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/zip")
|
||||
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s_sidecars.zip\"", jobName))
|
||||
|
||||
zipWriter := zip.NewWriter(w)
|
||||
defer zipWriter.Close()
|
||||
|
||||
for _, it := range req.Items {
|
||||
if it.Text == "" {
|
||||
continue
|
||||
}
|
||||
baseName := strings.TrimSuffix(it.Filename, filepath.Ext(it.Filename)) + ".txt"
|
||||
if baseName == ".txt" {
|
||||
baseName = "prompt.txt"
|
||||
}
|
||||
f, err := zipWriter.Create(baseName)
|
||||
if err == nil {
|
||||
_, _ = f.Write([]byte(it.Text))
|
||||
}
|
||||
}
|
||||
|
||||
summaryBytes, _ := json.MarshalIndent(req, "", " ")
|
||||
if f, err := zipWriter.Create("dataset_summary.json"); err == nil {
|
||||
_, _ = f.Write(summaryBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func handleScanFolder(w http.ResponseWriter, r *http.Request) {
|
||||
folderPath := r.URL.Query().Get("path")
|
||||
if folderPath == "" {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "path query parameter is required"})
|
||||
return
|
||||
}
|
||||
recursive := r.URL.Query().Get("recursive") == "true"
|
||||
|
||||
fi, err := os.Stat(folderPath)
|
||||
if err != nil || !fi.IsDir() {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": fmt.Sprintf("Directory not found or invalid: %s", folderPath)})
|
||||
return
|
||||
}
|
||||
|
||||
validExts := map[string]bool{
|
||||
".jpg": true, ".jpeg": true, ".png": true, ".webp": true,
|
||||
".bmp": true, ".tiff": true, ".tif": true, ".avif": true,
|
||||
}
|
||||
|
||||
type ScannedFile struct {
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
|
||||
var files []ScannedFile
|
||||
if recursive {
|
||||
_ = filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
|
||||
if err == nil && !info.IsDir() {
|
||||
ext := strings.ToLower(filepath.Ext(path))
|
||||
if validExts[ext] {
|
||||
files = append(files, ScannedFile{
|
||||
Name: filepath.Base(path),
|
||||
Path: path,
|
||||
Size: info.Size(),
|
||||
})
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
} else {
|
||||
entries, err := os.ReadDir(folderPath)
|
||||
if err == nil {
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() {
|
||||
ext := strings.ToLower(filepath.Ext(entry.Name()))
|
||||
if validExts[ext] {
|
||||
info, err := entry.Info()
|
||||
var size int64
|
||||
if err == nil {
|
||||
size = info.Size()
|
||||
}
|
||||
files = append(files, ScannedFile{
|
||||
Name: entry.Name(),
|
||||
Path: filepath.Join(folderPath, entry.Name()),
|
||||
Size: size,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"folder": folderPath,
|
||||
"total": len(files),
|
||||
"files": files,
|
||||
})
|
||||
}
|
||||
|
||||
func handleExportJSON(w http.ResponseWriter, r *http.Request) {
|
||||
jobID := chi.URLParam(r, "job_id")
|
||||
jobData, err := fetchBatchJobData(jobID)
|
||||
@@ -802,6 +920,8 @@ func main() {
|
||||
r.Head("/api/batch/{job_id}/export-json", handleExportJSON)
|
||||
r.Get("/api/batch/{job_id}/export-csv", handleExportCSV)
|
||||
r.Head("/api/batch/{job_id}/export-csv", handleExportCSV)
|
||||
r.Post("/api/export-zip", handleExportZipDirect)
|
||||
r.Get("/api/scan-folder", handleScanFolder)
|
||||
|
||||
// History API
|
||||
r.Get("/api/history", handleGetHistory)
|
||||
|
||||
Reference in New Issue
Block a user