Skip to content

Commit

Permalink
Merge 58f009f into e50031f
Browse files Browse the repository at this point in the history
  • Loading branch information
oltarasenko committed Oct 3, 2020
2 parents e50031f + 58f009f commit 46cb86d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
41 changes: 41 additions & 0 deletions lib/crawly_ui_web/controllers/item_controller.ex
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions lib/crawly_ui_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions lib/crawly_ui_web/templates/item/index.html.leex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<% end %>

</form>

<a href="/jobs/<%= @job_id %>/export" class="float-right"> Export as JSON</a>
<%= for item <- @rows do %>
<table class="w3-table-all card">
<tbody>
Expand Down

0 comments on commit 46cb86d

Please sign in to comment.