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

Add base64-encoded images support #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yunmikun2
Copy link

It is common case with json-api to send an image as base64-encoded string. Here goes support for this functionality.

Now when casting an image in base64-encoded format, it will be casted to a
binary image with generated file name.

{field, data = "data:image/" <> _}, fields ->
encoded_image = String.replace(data, ~r/^data\:image\/.*;base64,/, "")

case Base.decode64(encoded_image) do
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looking for something similar and maybe it's worth to have special key like Arc
something like that %{filename: filename, base64: base64}

stolen from https://github.com/stavro/arc/pull/108/files

@achempion
Copy link
Member

achempion commented Aug 23, 2020 via email

@yunmikun2
Copy link
Author

@achempion, well, the first way of handling this case I can think of, is to define an ecto schema that parses params before sending it into Ecto.Changeset.cast/4, which seems like overdo and, moreover, drags a lot of boilerplate.

If you ask me about a proper solution, this should be handled in the ecto-type itself (Waffle.Ecto.Type) instead of Waffle.Ecto.Schema.cast_attachments/4 (which is a macro for, I guess, some obscure optimization reasons), and the definition of Waffle.Ecto.Schema.cast_attachments/4 should be replaced with defdelegate to Ecto.Changeset.cast/4 (only for compatibility reasons).

When casting an image in base64-encoded format it will be casted to a
binary image with generated file name.
@yunmikun2 yunmikun2 force-pushed the bugfix/fix-base64-encoded-images branch from f55bd5e to 8f2f221 Compare October 1, 2020 17:03
@yunmikun2
Copy link
Author

Any info on whether it's going to be merged?

@szsoppa
Copy link

szsoppa commented Nov 3, 2021

Hey guys, are there any plans to merge this PR?

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 this pull request may close these issues.

4 participants