From 882f09abcd28e573b27efa156562f7628e8ced14 Mon Sep 17 00:00:00 2001 From: Tim Mecklem Date: Fri, 1 Jun 2018 14:27:35 -0400 Subject: [PATCH] Inline QR Codes Co-authored-by: Stamates --- .../controllers/address_qr_code_controller.ex | 15 --------------- .../transaction_qr_code_controller.ex | 15 --------------- apps/explorer_web/lib/explorer_web/router.ex | 3 --- .../templates/address/overview.html.eex | 2 +- .../templates/transaction/overview.html.eex | 2 +- .../lib/explorer_web/views/address_view.ex | 7 +++++++ .../explorer_web/views/transaction_view.ex | 7 +++++++ .../address_qr_code_controller_test.exs | 19 ------------------- .../transaction_qr_code_controller_test.exs | 19 ------------------- .../explorer_web/views/address_view_test.exs | 7 +++++++ .../views/transaction_view_test.exs | 7 +++++++ 11 files changed, 30 insertions(+), 73 deletions(-) delete mode 100644 apps/explorer_web/lib/explorer_web/controllers/address_qr_code_controller.ex delete mode 100644 apps/explorer_web/lib/explorer_web/controllers/transaction_qr_code_controller.ex delete mode 100644 apps/explorer_web/test/explorer_web/controllers/address_qr_code_controller_test.exs delete mode 100644 apps/explorer_web/test/explorer_web/controllers/transaction_qr_code_controller_test.exs diff --git a/apps/explorer_web/lib/explorer_web/controllers/address_qr_code_controller.ex b/apps/explorer_web/lib/explorer_web/controllers/address_qr_code_controller.ex deleted file mode 100644 index 5366f39c5c..0000000000 --- a/apps/explorer_web/lib/explorer_web/controllers/address_qr_code_controller.ex +++ /dev/null @@ -1,15 +0,0 @@ -defmodule ExplorerWeb.AddressQRCodeController do - use ExplorerWeb, :controller - - alias Explorer.Chain.Hash.Truncated - - def index(conn, %{"address_id" => id}) do - case Truncated.cast(id) do - {:ok, _} -> - send_download(conn, {:binary, QRCode.to_png(id)}, "content-type": "image/png", filename: "#{id}.png") - - _ -> - send_resp(conn, :not_found, "") - end - end -end diff --git a/apps/explorer_web/lib/explorer_web/controllers/transaction_qr_code_controller.ex b/apps/explorer_web/lib/explorer_web/controllers/transaction_qr_code_controller.ex deleted file mode 100644 index 0e2cb4255f..0000000000 --- a/apps/explorer_web/lib/explorer_web/controllers/transaction_qr_code_controller.ex +++ /dev/null @@ -1,15 +0,0 @@ -defmodule ExplorerWeb.TransactionQRCodeController do - use ExplorerWeb, :controller - - alias Explorer.Chain.Hash.Full - - def index(conn, %{"transaction_id" => id}) do - case Full.cast(id) do - {:ok, _} -> - send_download(conn, {:binary, QRCode.to_png(id)}, "content-type": "image/png", filename: "#{id}.png") - - _ -> - send_resp(conn, :not_found, "") - end - end -end diff --git a/apps/explorer_web/lib/explorer_web/router.ex b/apps/explorer_web/lib/explorer_web/router.ex index e187b7a0e2..a5e137d319 100644 --- a/apps/explorer_web/lib/explorer_web/router.ex +++ b/apps/explorer_web/lib/explorer_web/router.ex @@ -62,7 +62,6 @@ defmodule ExplorerWeb.Router do ) resources("/logs", TransactionLogController, only: [:index], as: :log) - resources("/qrcode.png", TransactionQRCodeController, only: [:index], as: :qr_code) end resources "/addresses", AddressController, only: [:show] do @@ -81,8 +80,6 @@ defmodule ExplorerWeb.Router do only: [:index], as: :contract ) - - resources("/qrcode.png", AddressQRCodeController, only: [:index], as: :qr_code) end get("/search", ChainController, :search) diff --git a/apps/explorer_web/lib/explorer_web/templates/address/overview.html.eex b/apps/explorer_web/lib/explorer_web/templates/address/overview.html.eex index 855bd93194..5e2923ec22 100644 --- a/apps/explorer_web/lib/explorer_web/templates/address/overview.html.eex +++ b/apps/explorer_web/lib/explorer_web/templates/address/overview.html.eex @@ -3,7 +3,7 @@
- <%= img_tag(address_qr_code_path(@conn, :index, @conn.assigns.locale, @address), alt: "qr_code", class: "qr-code") %> + qr_code
diff --git a/apps/explorer_web/lib/explorer_web/templates/transaction/overview.html.eex b/apps/explorer_web/lib/explorer_web/templates/transaction/overview.html.eex index 5ce3f3a3e2..9cbf7b761c 100644 --- a/apps/explorer_web/lib/explorer_web/templates/transaction/overview.html.eex +++ b/apps/explorer_web/lib/explorer_web/templates/transaction/overview.html.eex @@ -3,7 +3,7 @@
- <%= img_tag(transaction_qr_code_path(@conn, :index, @conn.assigns.locale, @transaction), alt: "qr_code", class: "qr-code") %> + qr_code
diff --git a/apps/explorer_web/lib/explorer_web/views/address_view.ex b/apps/explorer_web/lib/explorer_web/views/address_view.ex index 6081feadb9..fe130f9c29 100644 --- a/apps/explorer_web/lib/explorer_web/views/address_view.ex +++ b/apps/explorer_web/lib/explorer_web/views/address_view.ex @@ -44,4 +44,11 @@ defmodule ExplorerWeb.AddressView do def hash(%Address{hash: hash}) do to_string(hash) end + + def qr_code(%Address{hash: hash}) do + hash + |> to_string() + |> QRCode.to_png() + |> Base.encode64() + end end diff --git a/apps/explorer_web/lib/explorer_web/views/transaction_view.ex b/apps/explorer_web/lib/explorer_web/views/transaction_view.ex index c3b79874f2..6cbb68da70 100644 --- a/apps/explorer_web/lib/explorer_web/views/transaction_view.ex +++ b/apps/explorer_web/lib/explorer_web/views/transaction_view.ex @@ -32,6 +32,13 @@ defmodule ExplorerWeb.TransactionView do end end + def qr_code(%Transaction{hash: hash}) do + hash + |> to_string() + |> QRCode.to_png() + |> Base.encode64() + end + defp fee_to_currency({fee_type, fee}, denomination: denomination) do {fee_type, format_wei_value(Wei.from(fee, :wei), denomination)} end diff --git a/apps/explorer_web/test/explorer_web/controllers/address_qr_code_controller_test.exs b/apps/explorer_web/test/explorer_web/controllers/address_qr_code_controller_test.exs deleted file mode 100644 index e57a393e84..0000000000 --- a/apps/explorer_web/test/explorer_web/controllers/address_qr_code_controller_test.exs +++ /dev/null @@ -1,19 +0,0 @@ -defmodule ExplorerWeb.AddressQRCodeControllerTest do - use ExplorerWeb.ConnCase - - import ExplorerWeb.Router.Helpers, only: [address_qr_code_path: 4] - - describe "GET index/3" do - test "with valid address hash returns a QR code", %{conn: conn} do - conn = get(conn, address_qr_code_path(conn, :index, :en, address_hash())) - - assert response(conn, 200) - end - - test "with invalid address hash returns 404", %{conn: conn} do - conn = get(conn, address_qr_code_path(conn, :index, :en, "0xhaha")) - - assert response(conn, 404) - end - end -end diff --git a/apps/explorer_web/test/explorer_web/controllers/transaction_qr_code_controller_test.exs b/apps/explorer_web/test/explorer_web/controllers/transaction_qr_code_controller_test.exs deleted file mode 100644 index 1984846234..0000000000 --- a/apps/explorer_web/test/explorer_web/controllers/transaction_qr_code_controller_test.exs +++ /dev/null @@ -1,19 +0,0 @@ -defmodule ExplorerWeb.TransactionQRCodeControllerTest do - use ExplorerWeb.ConnCase - - import ExplorerWeb.Router.Helpers, only: [transaction_qr_code_path: 4] - - describe "GET index/3" do - test "with valid `Hash.Full` returns a QR code", %{conn: conn} do - conn = get(conn, transaction_qr_code_path(conn, :index, :en, transaction_hash())) - - assert response(conn, 200) - end - - test "with invalid address hash returns 404", %{conn: conn} do - conn = get(conn, transaction_qr_code_path(conn, :index, :en, "0xhaha")) - - assert response(conn, 404) - end - end -end diff --git a/apps/explorer_web/test/explorer_web/views/address_view_test.exs b/apps/explorer_web/test/explorer_web/views/address_view_test.exs index e3c15f3f0d..2372f56eb5 100644 --- a/apps/explorer_web/test/explorer_web/views/address_view_test.exs +++ b/apps/explorer_web/test/explorer_web/views/address_view_test.exs @@ -23,4 +23,11 @@ defmodule ExplorerWeb.AddressViewTest do assert "$0.000005 USD" == AddressView.formatted_usd(address, token) end end + + describe "qr_code/1" do + test "it returns an encoded value" do + address = build(:address) + assert {:ok, _} = Base.decode64(AddressView.qr_code(address)) + end + end end diff --git a/apps/explorer_web/test/explorer_web/views/transaction_view_test.exs b/apps/explorer_web/test/explorer_web/views/transaction_view_test.exs index bfdf9d6a93..996c3cb071 100644 --- a/apps/explorer_web/test/explorer_web/views/transaction_view_test.exs +++ b/apps/explorer_web/test/explorer_web/views/transaction_view_test.exs @@ -83,4 +83,11 @@ defmodule ExplorerWeb.TransactionViewTest do assert TransactionView.formatted_status(transaction) == "Success" end end + + describe "qr_code/1" do + test "it returns an encoded value" do + transaction = build(:transaction) + assert {:ok, _} = Base.decode64(TransactionView.qr_code(transaction)) + end + end end