From dd85badae4f21b16530f7dc9be2c2f9ea3b77956 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Thu, 26 May 2022 20:35:45 -0400 Subject: [PATCH] Faster uploads, fix replacement of existing files --- lib/philomena/objects.ex | 16 ++++++++++++++++ lib/philomena/uploader.ex | 2 +- .../controllers/image/file_controller.ex | 2 ++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/philomena/objects.ex b/lib/philomena/objects.ex index 2232617f..212bdf92 100644 --- a/lib/philomena/objects.ex +++ b/lib/philomena/objects.ex @@ -36,6 +36,22 @@ defmodule Philomena.Objects do end) end + # + # Upload a file using multiple API calls, writing the + # contents from the given path to storage. + # + @spec upload(String.t(), String.t()) :: any() + def upload(key, file_path) do + {_, mime} = Mime.file(file_path) + + run_all(fn opts -> + file_path + |> ExAws.S3.Upload.stream_file() + |> ExAws.S3.upload(opts[:bucket], key, content_type: mime) + |> ExAws.request!(opts[:config_overrides]) + end) + end + # # Copies a key from the source to the destination, # overwriting the destination object if its exists. diff --git a/lib/philomena/uploader.ex b/lib/philomena/uploader.ex index 76ba4c8d..df982897 100644 --- a/lib/philomena/uploader.ex +++ b/lib/philomena/uploader.ex @@ -72,7 +72,7 @@ defmodule Philomena.Uploader do content type and permissions. """ def persist_file(path, file) do - Objects.put(path, file) + Objects.upload(path, file) end @doc """ diff --git a/lib/philomena_web/controllers/image/file_controller.ex b/lib/philomena_web/controllers/image/file_controller.ex index a8d6175a..8b3f0e3f 100644 --- a/lib/philomena_web/controllers/image/file_controller.ex +++ b/lib/philomena_web/controllers/image/file_controller.ex @@ -10,6 +10,8 @@ defmodule PhilomenaWeb.Image.FileController do plug PhilomenaWeb.ScraperPlug, params_name: "image", params_key: "image" def update(conn, %{"image" => image_params}) do + Images.remove_hash(conn.assigns.image) + case Images.update_file(conn.assigns.image, image_params) do {:ok, image} -> conn