|
6 | 6 | "io"
|
7 | 7 | "io/ioutil"
|
8 | 8 | "net/http"
|
| 9 | + "strings" |
9 | 10 |
|
10 | 11 | "github.com/knadh/listmonk/subimporter"
|
11 | 12 | "github.com/labstack/echo"
|
@@ -37,8 +38,7 @@ func handleImportSubscribers(c echo.Context) error {
|
37 | 38 | }
|
38 | 39 |
|
39 | 40 | if r.Mode != subimporter.ModeSubscribe && r.Mode != subimporter.ModeBlacklist {
|
40 |
| - return echo.NewHTTPError(http.StatusBadRequest, |
41 |
| - "Invalid `mode`") |
| 41 | + return echo.NewHTTPError(http.StatusBadRequest, "Invalid `mode`") |
42 | 42 | }
|
43 | 43 |
|
44 | 44 | if len(r.Delim) != 1 {
|
@@ -78,17 +78,25 @@ func handleImportSubscribers(c echo.Context) error {
|
78 | 78 | }
|
79 | 79 | go impSess.Start()
|
80 | 80 |
|
81 |
| - // For now, we only extract 1 CSV from the ZIP. Handling async CSV |
82 |
| - // imports is more trouble than it's worth. |
83 |
| - dir, files, err := impSess.ExtractZIP(out.Name(), 1) |
84 |
| - if err != nil { |
85 |
| - return echo.NewHTTPError(http.StatusInternalServerError, |
86 |
| - fmt.Sprintf("Error extracting ZIP file: %v", err)) |
87 |
| - } else if len(files) == 0 { |
88 |
| - return echo.NewHTTPError(http.StatusBadRequest, |
89 |
| - "No CSV files found to import.") |
| 81 | + if strings.HasSuffix(strings.ToLower(file.Filename), ".csv") { |
| 82 | + go impSess.LoadCSV(out.Name(), rune(r.Delim[0])) |
| 83 | + } else { |
| 84 | + // Only 1 CSV from the ZIP is considered. If multiple files have |
| 85 | + // to be processed, counting the net number of lines (to track progress), |
| 86 | + // keeping the global import state (failed / successful) etc. across |
| 87 | + // multiple files becomes complex. Instead, it's just easier for the |
| 88 | + // end user to concat multiple CSVs (if there are multiple in the first) |
| 89 | + // place and uploada as one in the first place. |
| 90 | + dir, files, err := impSess.ExtractZIP(out.Name(), 1) |
| 91 | + if err != nil { |
| 92 | + return echo.NewHTTPError(http.StatusInternalServerError, |
| 93 | + fmt.Sprintf("Error extracting ZIP file: %v", err)) |
| 94 | + } else if len(files) == 0 { |
| 95 | + return echo.NewHTTPError(http.StatusBadRequest, |
| 96 | + "No CSV files found to import.") |
| 97 | + } |
| 98 | + go impSess.LoadCSV(dir+"/"+files[0], rune(r.Delim[0])) |
90 | 99 | }
|
91 |
| - go impSess.LoadCSV(dir+"/"+files[0], rune(r.Delim[0])) |
92 | 100 |
|
93 | 101 | return c.JSON(http.StatusOK, okResp{app.Importer.GetStats()})
|
94 | 102 | }
|
|
0 commit comments