listmonk/cmd/import.go

133 lines
4.2 KiB
Go
Raw Normal View History

2018-10-25 13:51:47 +00:00
package main
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
2018-11-27 06:51:59 +00:00
"strings"
2018-10-25 13:51:47 +00:00
"github.com/knadh/listmonk/internal/subimporter"
"github.com/knadh/listmonk/models"
"github.com/labstack/echo/v4"
2018-10-25 13:51:47 +00:00
)
// handleImportSubscribers handles the uploading and bulk importing of
// a ZIP file of one or more CSV files.
func handleImportSubscribers(c echo.Context) error {
app := c.Get("app").(*App)
// Is an import already running?
if app.importer.GetStats().Status == subimporter.StatusImporting {
2020-12-19 10:55:52 +00:00
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("import.alreadyRunning"))
2018-10-25 13:51:47 +00:00
}
2022-02-13 07:37:00 +00:00
// Unmarshal the JSON params.
var opt subimporter.SessionOpt
if err := json.Unmarshal([]byte(c.FormValue("params")), &opt); err != nil {
2018-10-25 13:51:47 +00:00
return echo.NewHTTPError(http.StatusBadRequest,
app.i18n.Ts("import.invalidParams", "error", err.Error()))
2018-10-25 13:51:47 +00:00
}
// Validate mode.
if opt.Mode != subimporter.ModeSubscribe && opt.Mode != subimporter.ModeBlocklist {
2020-12-19 10:55:52 +00:00
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("import.invalidMode"))
}
// If no status is specified, pick a default one.
if opt.SubStatus == "" {
switch opt.Mode {
case subimporter.ModeSubscribe:
opt.SubStatus = models.SubscriptionStatusUnconfirmed
case subimporter.ModeBlocklist:
opt.SubStatus = models.SubscriptionStatusUnsubscribed
}
}
if opt.SubStatus != models.SubscriptionStatusUnconfirmed &&
opt.SubStatus != models.SubscriptionStatusConfirmed &&
opt.SubStatus != models.SubscriptionStatusUnsubscribed {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("import.invalidSubStatus"))
}
if len(opt.Delim) != 1 {
2020-12-19 10:55:52 +00:00
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("import.invalidDelim"))
2018-10-25 13:51:47 +00:00
}
file, err := c.FormFile("file")
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest,
app.i18n.Ts("import.invalidFile", "error", err.Error()))
2018-10-25 13:51:47 +00:00
}
src, err := file.Open()
if err != nil {
return err
}
defer src.Close()
out, err := ioutil.TempFile("", "listmonk")
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("import.errorCopyingFile", "error", err.Error()))
2018-10-25 13:51:47 +00:00
}
defer out.Close()
if _, err = io.Copy(out, src); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("import.errorCopyingFile", "error", err.Error()))
2018-10-25 13:51:47 +00:00
}
// Start the importer session.
opt.Filename = file.Filename
impSess, err := app.importer.NewSession(opt)
2018-10-25 13:51:47 +00:00
if err != nil {
2020-12-19 10:55:52 +00:00
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("import.errorStarting", "error", err.Error()))
2018-10-25 13:51:47 +00:00
}
go impSess.Start()
2018-11-27 06:51:59 +00:00
if strings.HasSuffix(strings.ToLower(file.Filename), ".csv") {
go impSess.LoadCSV(out.Name(), rune(opt.Delim[0]))
2018-11-27 06:51:59 +00:00
} else {
// Only 1 CSV from the ZIP is considered. If multiple files have
// to be processed, counting the net number of lines (to track progress),
// keeping the global import state (failed / successful) etc. across
// multiple files becomes complex. Instead, it's just easier for the
// end user to concat multiple CSVs (if there are multiple in the first)
2022-02-13 07:37:00 +00:00
// place and upload as one in the first place.
2018-11-27 06:51:59 +00:00
dir, files, err := impSess.ExtractZIP(out.Name(), 1)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("import.errorProcessingZIP", "error", err.Error()))
2018-11-27 06:51:59 +00:00
}
go impSess.LoadCSV(dir+"/"+files[0], rune(opt.Delim[0]))
2018-10-25 13:51:47 +00:00
}
return c.JSON(http.StatusOK, okResp{app.importer.GetStats()})
2018-10-25 13:51:47 +00:00
}
// handleGetImportSubscribers returns import statistics.
func handleGetImportSubscribers(c echo.Context) error {
var (
app = c.Get("app").(*App)
s = app.importer.GetStats()
2018-10-25 13:51:47 +00:00
)
return c.JSON(http.StatusOK, okResp{s})
}
// handleGetImportSubscriberStats returns import statistics.
func handleGetImportSubscriberStats(c echo.Context) error {
2018-10-25 13:51:47 +00:00
app := c.Get("app").(*App)
return c.JSON(http.StatusOK, okResp{string(app.importer.GetLogs())})
2018-10-25 13:51:47 +00:00
}
// handleStopImportSubscribers sends a stop signal to the importer.
// If there's an ongoing import, it'll be stopped, and if an import
// is finished, it's state is cleared.
func handleStopImportSubscribers(c echo.Context) error {
app := c.Get("app").(*App)
app.importer.Stop()
return c.JSON(http.StatusOK, okResp{app.importer.GetStats()})
2018-10-25 13:51:47 +00:00
}