Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass binary to cast_attachments #54

Open
darkbaby123 opened this issue Dec 27, 2016 · 5 comments · May be fixed by #87
Open

Pass binary to cast_attachments #54

darkbaby123 opened this issue Dec 27, 2016 · 5 comments · May be fixed by #87

Comments

@darkbaby123
Copy link

There's a referenced issue in arc but I think it's arc_ecto's responsibility so I create an issue here.

My requirement is uploading file as base64 encoded string to the server. It's common for small files and is easy to work with JSON API. After googling I found arc support binary, so I thought I can decode base64 to binary and save the file.

arc support passing binary like this Avatar.store(%{filename: "file.png", binary: binary}) but there's no way to pass binary in arc_ecto, The cast_attachments(%{filename: "file.png", binary: binary}) fails. I checked the source and it seems the arc_ecto only support Plug.Upload struct. Is there a way or workaround to achieve this?

@philipgiuliani
Copy link

philipgiuliani commented Dec 29, 2016

I am doing the same in my project. Version v0.5 broke this behaviour and I am not aware of a workaround. If you use version v0.5.0-rc1 or v0.4.0 everything should be fine.

@stavro Is there a way to do this now?

@yogipatel
Copy link

I agree, this would be a nice feature to have. I can try to make a PR if you'd like, @stavro.

@darkbaby123 @philipgiuliani A workaround you can use for now is to write the data out to a temp file and pass that temp file's path in.

azhi added a commit to azhi/arc_ecto that referenced this issue Jan 16, 2018
arc allows storing `%{filename: filename, binary: data}`,
and now arc_ecto allows these structs to be accepted in
`cast_attachments`

fixes stavro#54
@azhi azhi linked a pull request Jan 16, 2018 that will close this issue
azhi added a commit to azhi/arc_ecto that referenced this issue Aug 30, 2018
arc allows storing `%{filename: filename, binary: data}`,
and now arc_ecto allows these structs to be accepted in
`cast_attachments`

fixes stavro#54
@Clyvv
Copy link

Clyvv commented Sep 25, 2018

Hi, im using v0.7.0 and still could not pass binary data to cast_attachments. Was this ever solved? Also I tried using Plug.Upload by creating a temp file but it seems it works at times and does not work the other times. Can someone help me out here. My function that converts my base64 to Plug.Upload is as follow
def process_image(image_base64) do {:ok, image_binary} = Base.decode64(image_base64) filename = image_binary |> image_extension() |> unique_filename() case File.write!("candidate_pics/#{filename}", image_binary, [:binary]) do :ok -> %Plug.Upload{ content_type: "image/#{image_extension(image_binary)}", filename: filename, path: Path.expand("candidate_pics/#{filename}") |> Path.absname() } {:error, reason} -> {:error, reason} end end

@jalcine
Copy link

jalcine commented Oct 12, 2018

Going to bump this issue and the accompanying PR #87 that fixes this.

For now, I'm just going to work off that branch until this is merged.

Nice work with the project @stavro, I know you must be busy.

tcitworld pushed a commit to tcitworld/arc_ecto that referenced this issue Apr 26, 2019
arc allows storing `%{filename: filename, binary: data}`,
and now arc_ecto allows these structs to be accepted in
`cast_attachments`

fixes stavro#54
@jeremyjh
Copy link

jeremyjh commented May 6, 2019

arc works fine if you pass a map %{binary: <<your bytes>>, filename: "filename.txt"}. The problem in arc_ecto is cast_attachment matches the file arg against %{__struct__: Plug Upload}.

So as a workaround, you can pass:

    %{
      __struct__: Plug.Upload,
      binary: bytes,
      filename: "filename.txt"
    }

In place of an actual Plug.Upload. Ugly as sin but it works.

azhi added a commit to azhi/arc_ecto that referenced this issue Jun 19, 2019
arc allows storing `%{filename: filename, binary: data}`,
and now arc_ecto allows these structs to be accepted in
`cast_attachments`

fixes stavro#54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants