diff --git a/lib/crawly_ui_web/controllers/item_controller.ex b/lib/crawly_ui_web/controllers/item_controller.ex new file mode 100644 index 0000000..41581d6 --- /dev/null +++ b/lib/crawly_ui_web/controllers/item_controller.ex @@ -0,0 +1,41 @@ +defmodule CrawlyUIWeb.ItemController do + use CrawlyUIWeb, :controller + + import Ecto.Query, warn: false + alias CrawlyUI.Repo + + alias CrawlyUI.Manager.Item + + def export(conn, %{"job_id" => job_id} = _params) do + query = from i in Item, where: i.job_id == ^job_id, select: i.data + + conn = + conn + |> put_resp_header("content-disposition", "attachment; filename=job_#{job_id}") + |> put_resp_content_type("application/json") + |> send_chunked(200) + + stream = Repo.stream(query) + + {:ok, conn} = + Repo.transaction(fn -> + Enum.reduce_while( + stream, + conn, + fn data, conn -> + data = Jason.encode!(data) + + case chunk(conn, data) do + {:ok, conn} -> + {:cont, conn} + + {:error, :closed} -> + {:halt, conn} + end + end + ) + end) + + conn + end +end diff --git a/lib/crawly_ui_web/router.ex b/lib/crawly_ui_web/router.ex index 60e7331..da43ea5 100644 --- a/lib/crawly_ui_web/router.ex +++ b/lib/crawly_ui_web/router.ex @@ -27,5 +27,7 @@ defmodule CrawlyUIWeb.Router do live "/jobs/:job_id/items", ItemLive, :index live "/jobs/:job_id/items/:id", ItemLive, :show + + get "/jobs/:job_id/export", ItemController, :export end end diff --git a/lib/crawly_ui_web/templates/item/index.html.leex b/lib/crawly_ui_web/templates/item/index.html.leex index 8738268..10cc7ca 100644 --- a/lib/crawly_ui_web/templates/item/index.html.leex +++ b/lib/crawly_ui_web/templates/item/index.html.leex @@ -22,6 +22,8 @@ <% end %> + + Export as JSON <%= for item <- @rows do %>