How to duplicate an arc_ecto upload

132 views Asked by At

One of the models of my Phoenix app has a logo, which uses arc_ecto and uploads to S3.

I'm duplicating one of the records and I want it to have the same image as a logo.

In my changeset I have:

|> cast_attachments(params, [:logo])

The value of the logo for the original item is:

iex(21)> MyRepo.get(MyModel, 1).logo
%{file_name: "mylogo.png",
updated_at: #Ecto.DateTime<2017-09-12 12:31:58>}

But if I try to set that directly, I get an error (Arc.File.new doesn't accept that structure, that makes sense).

I also tried extracting the S3 url of the original logo. This didn't return an error, but also didn't add the logo:

iex(22)> MyRepo.get(MyModel, 2).logo
nil

What's the right way to manage this scenario? Any hints?

1

There are 1 answers

0
Alan On BEST ANSWER

I managed to fix the issue, there were 2 things missing:

1 - Needed to add allow_paths: true to cast_attachments

|> cast_attachments(params, [:logo], allow_paths: true)

2 - I had this on the uploader:

def storage_dir(_version, {_file, my_model}) do
  "/my_models/#{my_model.id}/logo/"
end

So I was using the id to set the storage route. As I was duplicating the element, I still didn't have an ID and therefore the route was incorrect. I split the insert of the duplicated element and the update of the logo into 2 steps.