Faster uploads, fix replacement of existing files

This commit is contained in:
byte[] 2022-05-26 20:35:45 -04:00
parent 244ea56d0f
commit dd85badae4
3 changed files with 19 additions and 1 deletions

View file

@ -36,6 +36,22 @@ defmodule Philomena.Objects do
end) end)
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, # Copies a key from the source to the destination,
# overwriting the destination object if its exists. # overwriting the destination object if its exists.

View file

@ -72,7 +72,7 @@ defmodule Philomena.Uploader do
content type and permissions. content type and permissions.
""" """
def persist_file(path, file) do def persist_file(path, file) do
Objects.put(path, file) Objects.upload(path, file)
end end
@doc """ @doc """

View file

@ -10,6 +10,8 @@ defmodule PhilomenaWeb.Image.FileController do
plug PhilomenaWeb.ScraperPlug, params_name: "image", params_key: "image" plug PhilomenaWeb.ScraperPlug, params_name: "image", params_key: "image"
def update(conn, %{"image" => image_params}) do def update(conn, %{"image" => image_params}) do
Images.remove_hash(conn.assigns.image)
case Images.update_file(conn.assigns.image, image_params) do case Images.update_file(conn.assigns.image, image_params) do
{:ok, image} -> {:ok, image} ->
conn conn