From a1dad5d71d60cc41bbd683dcd8f45ecb965a17c0 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 5 Sep 2021 23:46:44 -0400 Subject: [PATCH] Allow getting uploads as non-chunked --- lib/wiki_web/controllers/page_controller.ex | 34 +++++++++++++-------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/wiki_web/controllers/page_controller.ex b/lib/wiki_web/controllers/page_controller.ex index af54289..c9330df 100644 --- a/lib/wiki_web/controllers/page_controller.ex +++ b/lib/wiki_web/controllers/page_controller.ex @@ -136,27 +136,35 @@ defmodule WikiWeb.PageController do redirect(conn, to: Routes.page_path(conn, :edit, page.id)) end - def get_upload(conn, _params) do + def get_upload(conn, params) do upload = conn.assigns.upload key = get_session(conn, :content_encryption_key) key = Base.decode16!(key, case: :lower) - conn = + if Map.get(params, "oneshot") do + data = Upload.decrypt_content(upload, key) + conn |> put_resp_header("content-type", upload.content_type) - |> send_chunked(200) + |> send_resp(200, data) + else + conn = + conn + |> put_resp_header("content-type", upload.content_type) + |> send_chunked(200) - upload - |> Upload.decrypt_stream(key) - |> Enum.reduce_while(conn, fn decrypted_chunk, conn -> - case chunk(conn, decrypted_chunk) do - {:ok, conn} -> - {:cont, conn} + upload + |> Upload.decrypt_stream(key) + |> Enum.reduce_while(conn, fn decrypted_chunk, conn -> + case chunk(conn, decrypted_chunk) do + {:ok, conn} -> + {:cont, conn} - {:error, :closed} -> - {:halt, {:halt, conn}} - end - end) + {:error, :closed} -> + {:halt, {:halt, conn}} + end + end) + end end def delete_upload(conn, _params) do