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

+37 -8
View File
@@ -1,6 +1,7 @@
package httpapi
import (
"encoding/json"
"errors"
"fmt"
"net/http"
@@ -46,11 +47,12 @@ func (s *Server) handleCreateFeed(w http.ResponseWriter, r *http.Request) {
}
var body struct {
Name string `json:"name"`
URL string `json:"url"`
ItemPath string `json:"item_path"`
FeedType string `json:"feed_type"`
SyncIntervalMinutes int `json:"sync_interval_minutes"`
Name string `json:"name"`
URL string `json:"url"`
ItemPath string `json:"item_path"`
FeedType string `json:"feed_type"`
SyncIntervalMinutes int `json:"sync_interval_minutes"`
RequestHeaders map[string]string `json:"request_headers"`
}
if err := DecodeJSON(r, &body); err != nil {
Error(w, http.StatusBadRequest, "invalid json")
@@ -62,6 +64,7 @@ func (s *Server) handleCreateFeed(w http.ResponseWriter, r *http.Request) {
ItemPath: body.ItemPath,
FeedType: body.FeedType,
SyncIntervalMinutes: body.SyncIntervalMinutes,
RequestHeaders: body.RequestHeaders,
})
if err != nil {
ClientOrLog(w, http.StatusBadRequest, "could not create feed", err, feeds.ClientError)
@@ -81,6 +84,13 @@ func (s *Server) createFeedFromMultipart(w http.ResponseWriter, r *http.Request,
feedType := strings.TrimSpace(r.FormValue("feed_type"))
itemPath := strings.TrimSpace(r.FormValue("item_path"))
interval, _ := strconv.Atoi(strings.TrimSpace(r.FormValue("sync_interval_minutes")))
headers := map[string]string{}
if raw := strings.TrimSpace(r.FormValue("request_headers")); raw != "" {
if err := json.Unmarshal([]byte(raw), &headers); err != nil {
Error(w, http.StatusBadRequest, "invalid request_headers JSON")
return
}
}
file, header, fileErr := r.FormFile("file")
var options map[string]any
@@ -107,10 +117,15 @@ func (s *Server) createFeedFromMultipart(w http.ResponseWriter, r *http.Request,
"source_path": pathStr,
"source_file_id": fileID,
"source_filename": fileName,
"source_kind": "csv",
"source_kind": feedType,
}
if feedType == "" {
feedType = "csv"
if strings.HasSuffix(strings.ToLower(header.Filename), ".xml") {
feedType = "xml"
} else if strings.HasSuffix(strings.ToLower(header.Filename), ".json") {
feedType = "json"
}
}
if fid, err := uuid.Parse(fileID); err == nil {
_, _ = s.Catalog.UpdateFileStatus(r.Context(), cid, fid, "uploaded", map[string]any{
@@ -131,6 +146,7 @@ func (s *Server) createFeedFromMultipart(w http.ResponseWriter, r *http.Request,
FeedType: feedType,
SyncIntervalMinutes: interval,
Options: options,
RequestHeaders: headers,
})
if err != nil {
ClientOrLog(w, http.StatusBadRequest, "could not create feed", err, feeds.ClientError)
@@ -192,7 +208,8 @@ func (s *Server) handleDeleteFeed(w http.ResponseWriter, r *http.Request) {
Error(w, http.StatusBadRequest, "invalid id")
return
}
if err := s.Feeds.Delete(r.Context(), cid, id); err != nil {
deletedUnprocessed, err := s.Feeds.DeleteWithCleanup(r.Context(), cid, id)
if err != nil {
if feeds.IsNotFound(err) {
Error(w, http.StatusNotFound, "not found")
return
@@ -200,7 +217,10 @@ func (s *Server) handleDeleteFeed(w http.ResponseWriter, r *http.Request) {
Error(w, http.StatusInternalServerError, "delete failed")
return
}
JSON(w, http.StatusOK, map[string]any{"id": id.String(), "deleted": true})
JSON(w, http.StatusOK, map[string]any{
"id": id.String(), "deleted": true,
"deleted_unprocessed_products": deletedUnprocessed,
})
}
func (s *Server) handleSyncFeed(w http.ResponseWriter, r *http.Request) {
@@ -584,6 +604,15 @@ func (s *Server) handlePublicExportCSV(w http.ResponseWriter, r *http.Request) {
}
}
func (s *Server) handlePublicExportJSON(w http.ResponseWriter, r *http.Request) {
token := chi.URLParam(r, "token")
lw := &lazyHeaderWriter{ResponseWriter: w, contentType: "application/json; charset=utf-8"}
if err := s.Feeds.PublicExportJSON(r.Context(), lw, token); err != nil {
if !lw.wrote {
writePublicExportError(w, err)
}
}
}
func writePublicExportError(w http.ResponseWriter, err error) {
// Format mismatch must look identical to unknown tokens so probing .xml/.csv
// cannot confirm whether a guessed public_token exists.