From 3f8a538ef20a7795705f67540521cb855fd67bca Mon Sep 17 00:00:00 2001 From: Alisson Machado Date: Sun, 8 Dec 2024 16:55:44 -0300 Subject: [PATCH] fix: handle upload fixes --- .../helpers/error_helpers.ex | 7 ++- .../live/products/form_component.ex | 43 +++++++++++-------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/lib/digistab_store_web/helpers/error_helpers.ex b/lib/digistab_store_web/helpers/error_helpers.ex index 59d7b92..ed0c1b6 100644 --- a/lib/digistab_store_web/helpers/error_helpers.ex +++ b/lib/digistab_store_web/helpers/error_helpers.ex @@ -67,7 +67,12 @@ defmodule DigistabStoreWeb.ErrorHelpers do ) def error_to_string(:external_client_failure), - do: Gettext.dgettext(DigistabStoreWeb.Gettext, "errors", "Failed to upload") + do: + Gettext.dgettext( + DigistabStoreWeb.Gettext, + "errors", + "Failed to upload - external_client_failure" + ) def error_to_string(_) do Gettext.dgettext(DigistabStoreWeb.Gettext, "errors", "Failed to upload") diff --git a/lib/digistab_store_web/live/products/form_component.ex b/lib/digistab_store_web/live/products/form_component.ex index dc20e51..ee2a806 100644 --- a/lib/digistab_store_web/live/products/form_component.ex +++ b/lib/digistab_store_web/live/products/form_component.ex @@ -437,7 +437,7 @@ defmodule DigistabStoreWeb.ProductLive.FormComponent do for entry <- completed do key = "digistab_store/products/#{entry.client_name}" - dest = Path.join("http://#{s3_config().bucket}.s3.amazonaws.com", key) + dest = Path.join("https://#{s3_config().bucket}.s3.amazonaws.com", key) %{"url" => dest} end @@ -609,24 +609,31 @@ defmodule DigistabStoreWeb.ProductLive.FormComponent do # AWS fields for secure file uploads, as well as any necessary file restrictions. defp presign_upload(entry, socket) do uploads = socket.assigns.uploads - key = "digistab_store/products/#{entry.client_name}" - - {:ok, fields} = - SimpleS3Upload.sign_form_upload(s3_config(), s3_config().bucket, - key: key, - content_type: entry.client_type, - max_file_size: uploads[entry.upload_config].max_file_size, - expires_in: :timer.hours(1) - ) - - meta = %{ - uploader: "S3", - key: key, - url: "http://#{s3_config().bucket}.s3-#{s3_config().region}.amazonaws.com", - fields: fields - } - {:ok, meta, socket} + case uploads[entry.upload_config] do + nil -> + {:error, :upload_cancelled} + + upload_config -> + key = "digistab_store/products/#{entry.client_name}" + + {:ok, fields} = + SimpleS3Upload.sign_form_upload(s3_config(), s3_config().bucket, + key: key, + content_type: entry.client_type, + max_file_size: upload_config.max_file_size, + expires_in: :timer.hours(1) + ) + + meta = %{ + uploader: "S3", + key: key, + url: "https://#{s3_config().bucket}.s3-#{s3_config().region}.amazonaws.com", + fields: fields + } + + {:ok, meta, socket} + end end # s3 config params at runtime execution