Yxizzy
Elixir sending empty response on file upload
Hey, I am trying to upload an image to Elixir from a form and then to gcs using arc. The form returns an empty response from Elixir on submit. Whenever I upload an image, I get empty parameter from Elixir, please what can be the cause, why is Elixir receiving empty parameters. I am new to using Elixir and Arc, so I will like to confirm if my code is correct.
This is my mix.exs file
{:arc_gcs, "~> 0.1.0"},
{:arc_ecto, "~> 0.11.1"},
{:arc, "~> 0.11.0"}
Then my route for creating the post is
resources “/images”, ImagesController, except: [:new, :edit]
My Images Controller.create
def create(conn, images_params) do
images_params["id"]})
with {:ok, %Images{} = images} <- Images.create_images(images_params) do
conn
|> put_status(:created)
|> put_resp_header("location", images_path(conn, :show, images))
|> render("show.json", images: images)
end
end
This is my Images Model
schema "image" do
field :name, :string
field :image, Myapp.Avatar.Type
timestamps()
end
@doc false
def changeset(images, attrs) do
images
|> cast(attrs, [:name])
|> validate_required([:name])
|> put_name
|> cast_attachments(attrs, [:image])
end
def put_name(changeset) do
case changeset do
%Ecto.Changeset{
valid?: true,
changes: %{upload: %Plug.Upload{content_type: "image/" <> _, filename: name}}
} ->
put_change(changeset, :name, name)
_ ->
changeset
end
end
def store(%Plug.Upload{} = upload, image) do
Avatar.store({upload, image})
end
def url(image, version) do
Avatar.url({image.name, image}, version)
end
end
And finally my Arc Uploader
@versions [:original]
def __storage, do: Arc.Storage.GCS
def gcs_object_headers(:original, {file, _scope}) do
[content_type: MIME.from_path(file.file_name)]
end
#To add a thumbnail version:
@versions [:original, :thumb]
# Override the bucket on a per definition basis:
# def bucket do
# :custom_bucket_name
# end
# Whitelist file extensions:
def validate({file, _}) do
~w(.jpg .jpeg .gif .png) |> Enum.member?(Path.extname(file.file_name))
end
def transform(:thumb, _file) do
{:convert, "-thumbnail x#{@heights[:thumb]} -gravity center -format jpg"}
end
def transform(:show, _file) do
{:convert, "-strip -resize x#{@heights[:show]} -gravity center -format png"}
end
def storage_dir(version, {_, image}) do
"uploads/posts/images/#{image.id}/#{version}"
end
def filename(_version, {file, _}) do
Path.rootname(file.file_name)
end
end
This is expected to receive the uploaded image from the web and save it on Google Cloud Storage but the I'm still getting an empty parameter. I have been on this for days, any help at all would be really appreciated, please someone should look into this and tell me where I might be getting it wrong.
Thanks
Most Liked
alvinncx
Popular in Questions
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Hello everyone,
I try to use an Javascript Event Handler in my root.html.leex file.
Therefore I created a function in the app.js file: ...
New
Other popular topics
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security










