This commit is contained in:
greeneclipse committed 2026-10-27 14:53:19 +01:00
1 parent c347d2418f
commit 1ad180a052
49 files changed
+6339 -89

No files matched your search

+25 -7
View File
@@ -16,7 +16,10 @@ import (
"github.com/jackc/pgx/v5"
)
const maxUploadBytes = 5 << 20 // 5 MiB
const (
maxUploadBytes = 5 << 20 // 5 MiB
maxFeedUploadBytes = 32 << 20 // 32 MiB; feeds.maxExcelBytes is the Excel parse-time cap
)
func sanitizeFileName(name string) string {
name = filepath.Base(strings.TrimSpace(name))
@@ -49,10 +52,25 @@ func (s *Service) SaveUpload(ctx context.Context, companyID, userID uuid.UUID, u
isCSV := strings.HasSuffix(lower, ".csv") || strings.HasSuffix(lower, ".tsv")
isXML := strings.HasSuffix(lower, ".xml") && kind == "feed"
isJSON := strings.HasSuffix(lower, ".json")
if !isCSV && !isXML && !isJSON {
return nil, ClientMsg("only CSV, JSON or XML feed uploads are allowed")
// .zip = several workbooks as one feed (e.g. one GroupeSeb product sheet per file).
isExcel := kind == "feed" && (strings.HasSuffix(lower, ".xls") || strings.HasSuffix(lower, ".xlsx") || strings.HasSuffix(lower, ".xlsm") || strings.HasSuffix(lower, ".zip"))
if !isCSV && !isXML && !isJSON && !isExcel {
return nil, ClientMsg("only CSV, JSON, XML, Excel or zipped Excel feed uploads are allowed")
}
if contentType != "" &&
limit := int64(maxUploadBytes)
if kind == "feed" {
// Supplier catalogs (full XML exports, workbooks with embedded photos) run well past 5 MiB.
limit = maxFeedUploadBytes
}
ctLower := strings.ToLower(contentType)
if isExcel && contentType != "" &&
!strings.Contains(ctLower, "excel") &&
!strings.Contains(ctLower, "spreadsheetml") &&
!strings.Contains(ctLower, "octet-stream") &&
!strings.Contains(ctLower, "zip") {
return nil, ClientMsg("invalid content type for Excel upload")
}
if !isExcel && contentType != "" &&
!strings.Contains(strings.ToLower(contentType), "csv") &&
!strings.Contains(strings.ToLower(contentType), "json") &&
!strings.Contains(strings.ToLower(contentType), "xml") &&
@@ -82,14 +100,14 @@ func (s *Service) SaveUpload(ctx context.Context, companyID, userID uuid.UUID, u
}
defer f.Close()
n, err := io.Copy(f, io.LimitReader(r, maxUploadBytes+1))
n, err := io.Copy(f, io.LimitReader(r, limit+1))
if err != nil {
_ = os.Remove(abs)
return nil, err
}
if n > maxUploadBytes {
if n > limit {
_ = os.Remove(abs)
return nil, ClientMsg(fmt.Sprintf("file exceeds %d byte limit", maxUploadBytes))
return nil, ClientMsg(fmt.Sprintf("file exceeds %d byte limit", limit))
}
var uid any