ImportSubscribers handles the uploading and bulk importing of a ZIP file of one or more CSV files.
(c echo.Context)
| 16 | // ImportSubscribers handles the uploading and bulk importing of |
| 17 | // a ZIP file of one or more CSV files. |
| 18 | func (a *App) ImportSubscribers(c echo.Context) error { |
| 19 | // Is an import already running? |
| 20 | if a.importer.GetStats().Status == subimporter.StatusImporting { |
| 21 | return echo.NewHTTPError(http.StatusBadRequest, a.i18n.T("import.alreadyRunning")) |
| 22 | } |
| 23 | |
| 24 | // Unmarshal the JSON params. |
| 25 | var opt subimporter.SessionOpt |
| 26 | if err := json.Unmarshal([]byte(c.FormValue("params")), &opt); err != nil { |
| 27 | return echo.NewHTTPError(http.StatusBadRequest, |
| 28 | a.i18n.Ts("import.invalidParams", "error", err.Error())) |
| 29 | } |
| 30 | |
| 31 | // Filter list IDs against the current user's permitted lists. |
| 32 | // Blocklist mode doesn't require list subscriptions. |
| 33 | user := auth.GetUser(c) |
| 34 | opt.ListIDs = user.FilterListsByPerm(auth.PermTypeManage, opt.ListIDs) |
| 35 | if len(opt.ListIDs) == 0 && opt.Mode != subimporter.ModeBlocklist { |
| 36 | return echo.NewHTTPError(http.StatusForbidden, |
| 37 | a.i18n.Ts("globals.messages.permissionDenied", "name", "lists")) |
| 38 | } |
| 39 | |
| 40 | // Validate mode. |
| 41 | if opt.Mode != subimporter.ModeSubscribe && opt.Mode != subimporter.ModeBlocklist { |
| 42 | return echo.NewHTTPError(http.StatusBadRequest, a.i18n.T("import.invalidMode")) |
| 43 | } |
| 44 | |
| 45 | // If no status is specified, pick a default one. |
| 46 | if opt.SubStatus == "" { |
| 47 | switch opt.Mode { |
| 48 | case subimporter.ModeSubscribe: |
| 49 | opt.SubStatus = models.SubscriptionStatusUnconfirmed |
| 50 | case subimporter.ModeBlocklist: |
| 51 | opt.SubStatus = models.SubscriptionStatusUnsubscribed |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if opt.SubStatus != models.SubscriptionStatusUnconfirmed && |
| 56 | opt.SubStatus != models.SubscriptionStatusConfirmed && |
| 57 | opt.SubStatus != models.SubscriptionStatusUnsubscribed { |
| 58 | return echo.NewHTTPError(http.StatusBadRequest, a.i18n.T("import.invalidSubStatus")) |
| 59 | } |
| 60 | |
| 61 | if len(opt.Delim) != 1 { |
| 62 | return echo.NewHTTPError(http.StatusBadRequest, a.i18n.T("import.invalidDelim")) |
| 63 | } |
| 64 | |
| 65 | // Open the HTTP file. |
| 66 | file, err := c.FormFile("file") |
| 67 | if err != nil { |
| 68 | return echo.NewHTTPError(http.StatusBadRequest, |
| 69 | a.i18n.Ts("import.invalidFile", "error", err.Error())) |
| 70 | } |
| 71 | |
| 72 | src, err := file.Open() |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
nothing calls this directly
no test coverage detected