URL URL for images in local storage

I use arc and arc_ecto to load avatars for the model user. During development, I would like to use local storage. The download is in progress, but I don’t know how to actually display the image.

I can display the file name but don’t get the URL for local storage to use in the tag <img>. Should I use a different directory in the configuration or in a different way to access it in the view?

Web / templates / user / show.html.eex

<h2>Show user</h2>

[...]

<%= MyApp.Avatar.url({@user.avatar, @user}, :original) %>

Web / Uploaders / avatar.ex

defmodule MyApp.Avatar do
  use Arc.Definition
  use Arc.Ecto.Definition

  @versions [:original, :thumb]

  def transform(:thumb, _) do
    {:convert, "-strip -thumbnail 100x100^ -gravity center -extent 100x100"}
  end

  def __storage, do: Arc.Storage.Local

  def filename(version,  {file, scope}), do: "#{version}-#{file.file_name}"

  # Override the storage directory:
  def storage_dir(version, {file, scope}) do
    "web/static/assets/images/avatars/#{scope.id}"
  end
end
+4
source share
1 answer

, , priv/static/. arc . :

# helper
defmodule MyApp.AssetHelper do
  def upload_path(path) do
    case is_binary(path) do
      true ->
        path
        |> String.replace("priv/static/", "/")
      _ ->
        nil
    end
  end
end

# web/web.ex
  def view do
    quote do
      ....
      import MyApp.AssetHelper, only: [upload_path: 1]
      ....
      import MyApp.Router.Helpers
      import MyApp.ErrorHelpers
      import MyApp.Gettext
    end
  end

# view
defmodule MyApp.AvatarView do
  use MyApp.Web, :view

  def avatar_path(avatar) do
    MyApp.Avatar.url({avatar.image, avatar}, :thumb)
    |> upload_path
  end
end

,

+2

All Articles