mirror of
https://github.com/miniflux/v2.git
synced 2025-09-15 18:57:04 +00:00
Refactor HTTP Client and LocalizedError packages
This commit is contained in:
parent
120aabfbce
commit
14e25ab9fe
104 changed files with 1277 additions and 10672 deletions
|
@ -6,12 +6,14 @@ package ui // import "miniflux.app/v2/internal/ui"
|
|||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"miniflux.app/v2/internal/config"
|
||||
"miniflux.app/v2/internal/http/client"
|
||||
"miniflux.app/v2/internal/http/request"
|
||||
"miniflux.app/v2/internal/http/response/html"
|
||||
"miniflux.app/v2/internal/http/route"
|
||||
"miniflux.app/v2/internal/locale"
|
||||
"miniflux.app/v2/internal/reader/fetcher"
|
||||
"miniflux.app/v2/internal/reader/opml"
|
||||
"miniflux.app/v2/internal/ui/session"
|
||||
"miniflux.app/v2/internal/ui/view"
|
||||
|
@ -51,7 +53,7 @@ func (h *handler) uploadOPML(w http.ResponseWriter, r *http.Request) {
|
|||
view.Set("countErrorFeeds", h.store.CountUserFeedsWithErrors(user.ID))
|
||||
|
||||
if fileHeader.Size == 0 {
|
||||
view.Set("errorMessage", "error.empty_file")
|
||||
view.Set("errorMessage", locale.NewLocalizedError("error.empty_file").Translate(user.Language))
|
||||
html.OK(w, r, view.Render("import"))
|
||||
return
|
||||
}
|
||||
|
@ -73,15 +75,15 @@ func (h *handler) fetchOPML(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
url := r.FormValue("url")
|
||||
if url == "" {
|
||||
opmlFileURL := strings.TrimSpace(r.FormValue("url"))
|
||||
if opmlFileURL == "" {
|
||||
html.Redirect(w, r, route.Path(h.router, "import"))
|
||||
return
|
||||
}
|
||||
|
||||
slog.Info("Fetching OPML file remotely",
|
||||
slog.Int64("user_id", loggedUserID),
|
||||
slog.String("opml_file_url", url),
|
||||
slog.String("opml_file_url", opmlFileURL),
|
||||
)
|
||||
|
||||
sess := session.New(h.store, request.SessionID(r))
|
||||
|
@ -91,15 +93,21 @@ func (h *handler) fetchOPML(w http.ResponseWriter, r *http.Request) {
|
|||
view.Set("countUnread", h.store.CountUnreadEntries(user.ID))
|
||||
view.Set("countErrorFeeds", h.store.CountUserFeedsWithErrors(user.ID))
|
||||
|
||||
clt := client.NewClientWithConfig(url, config.Opts)
|
||||
resp, err := clt.Get()
|
||||
if err != nil {
|
||||
view.Set("errorMessage", err)
|
||||
requestBuilder := fetcher.NewRequestBuilder()
|
||||
requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
|
||||
requestBuilder.WithProxy(config.Opts.HTTPClientProxy())
|
||||
|
||||
responseHandler := fetcher.NewResponseHandler(requestBuilder.ExecuteRequest(opmlFileURL))
|
||||
defer responseHandler.Close()
|
||||
|
||||
if localizedError := responseHandler.LocalizedError(); localizedError != nil {
|
||||
slog.Warn("Unable to fetch OPML file", slog.String("opml_file_url", opmlFileURL), slog.Any("error", localizedError.Error()))
|
||||
view.Set("errorMessage", localizedError.Translate(user.Language))
|
||||
html.OK(w, r, view.Render("import"))
|
||||
return
|
||||
}
|
||||
|
||||
if impErr := opml.NewHandler(h.store).Import(user.ID, resp.Body); impErr != nil {
|
||||
if impErr := opml.NewHandler(h.store).Import(user.ID, responseHandler.Body(config.Opts.HTTPClientMaxBodySize())); impErr != nil {
|
||||
view.Set("errorMessage", impErr)
|
||||
html.OK(w, r, view.Render("import"))
|
||||
return
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue