This commit is contained in:
greeneclipse committed 2026-10-15 21:22:43 +02:00
1 parent 04794aea54
commit cfd31f398c
61 files changed
+2062 -254

No files matched your search

+84
View File
@@ -759,6 +759,9 @@ func (s *Service) PublicExportXML(ctx context.Context, w io.Writer, token string
}
// PublicExportCSV streams CSV for a public export token.
func (s *Service) PublicExportJSON(ctx context.Context, w io.Writer, token string) error {
return s.StreamPublicExport(ctx, w, token, "json")
}
func (s *Service) PublicExportCSV(ctx context.Context, w io.Writer, token string) error {
return s.StreamPublicExport(ctx, w, token, "csv")
}
@@ -805,6 +808,8 @@ func (s *Service) streamExport(ctx context.Context, w io.Writer, row exportFeedR
switch format {
case "csv":
return streamCSV(w, seq, tpl)
case "json":
return streamJSON(w, seq, tpl)
default:
return streamXML(w, seq, tpl, row.Name)
}
@@ -966,6 +971,9 @@ func (s *Service) ExportSelectedProducts(ctx context.Context, companyID, feedID
case "csv":
count, err = streamCSV(&buf, seq, tpl)
mimeType = "text/csv; charset=utf-8"
case "json":
count, err = streamJSON(&buf, seq, tpl)
mimeType = "application/json; charset=utf-8"
default:
count, err = streamXML(&buf, seq, tpl, row.Name)
mimeType = "application/xml; charset=utf-8"
@@ -1002,6 +1010,17 @@ func (s *Service) queryExportProductsByIDs(ctx context.Context, companyID uuid.U
func renderExportSnippet(format string, tpl exportTemplate, products []exportProduct, feedName string) (string, int, error) {
var buf bytes.Buffer
switch strings.ToLower(strings.TrimSpace(format)) {
case "json":
seq := exportProductSeq(func(yield func(exportProduct) error) error {
for _, p := range products {
if err := yield(p); err != nil {
return err
}
}
return nil
})
n, err := streamJSON(&buf, seq, tpl)
return buf.String(), n, err
case "csv":
cw := csv.NewWriter(&buf)
headers := make([]string, len(tpl.Fields))
@@ -1063,3 +1082,68 @@ func sanitizeExportFileName(name string) string {
}
return out
}
// streamJSON emits valid JSON objects one at a time with no catalog-wide buffering.
// The outer key follows the configured template root (products by default).
func streamJSON(w io.Writer, seq exportProductSeq, tpl exportTemplate) (int, error) {
root := strings.TrimSpace(tpl.Root)
if root == "" {
root = "products"
}
key, err := json.Marshal(root)
if err != nil {
return 0, err
}
if _, err := fmt.Fprintf(w, "{%s:[", key); err != nil {
return 0, err
}
count := 0
err = seq(func(p exportProduct) error {
row := make(map[string]any, len(tpl.Fields))
for _, field := range tpl.Fields {
name := strings.TrimSpace(field.Key)
if name == "" {
name = field.Source
}
if isSpecExpandSource(field.Source) {
specs := map[string]string{}
for _, spec := range expandSpecFields(p) {
if val := productFieldValue(p, spec.Source); val != "" {
specs[spec.Key] = val
}
}
if len(specs) > 0 {
row[name] = specs
}
continue
}
if name != "" {
row[name] = productFieldValue(p, field.Source)
}
}
encoded, err := json.Marshal(row)
if err != nil {
return err
}
if count > 0 {
if _, err := io.WriteString(w, ","); err != nil {
return err
}
}
if _, err := w.Write(encoded); err != nil {
return err
}
count++
if count%exportChunkHint == 0 {
if f, ok := w.(interface{ Flush() }); ok {
f.Flush()
}
}
return nil
})
if err != nil {
return count, err
}
_, err = io.WriteString(w, "]}\n")
return count, err
}