opml_upload.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package ui
  5. import (
  6. "net/http"
  7. "github.com/miniflux/miniflux/http/context"
  8. "github.com/miniflux/miniflux/http/response"
  9. "github.com/miniflux/miniflux/http/response/html"
  10. "github.com/miniflux/miniflux/http/route"
  11. "github.com/miniflux/miniflux/logger"
  12. "github.com/miniflux/miniflux/reader/opml"
  13. "github.com/miniflux/miniflux/ui/session"
  14. "github.com/miniflux/miniflux/ui/view"
  15. )
  16. // UploadOPML handles OPML file importation.
  17. func (c *Controller) UploadOPML(w http.ResponseWriter, r *http.Request) {
  18. ctx := context.New(r)
  19. user, err := c.store.UserByID(ctx.UserID())
  20. if err != nil {
  21. html.ServerError(w, err)
  22. return
  23. }
  24. file, fileHeader, err := r.FormFile("file")
  25. if err != nil {
  26. logger.Error("[Controller:UploadOPML] %v", err)
  27. response.Redirect(w, r, route.Path(c.router, "import"))
  28. return
  29. }
  30. defer file.Close()
  31. logger.Info(
  32. "[Controller:UploadOPML] User #%d uploaded this file: %s (%d bytes)",
  33. user.ID,
  34. fileHeader.Filename,
  35. fileHeader.Size,
  36. )
  37. sess := session.New(c.store, ctx)
  38. view := view.New(c.tpl, ctx, sess)
  39. view.Set("menu", "feeds")
  40. view.Set("user", user)
  41. view.Set("countUnread", c.store.CountUnreadEntries(user.ID))
  42. if fileHeader.Size == 0 {
  43. view.Set("errorMessage", "This file is empty")
  44. html.OK(w, r, view.Render("import"))
  45. return
  46. }
  47. if impErr := opml.NewHandler(c.store).Import(user.ID, file); impErr != nil {
  48. view.Set("errorMessage", impErr)
  49. html.OK(w, r, view.Render("import"))
  50. return
  51. }
  52. response.Redirect(w, r, route.Path(c.router, "feeds"))
  53. }