thojanssens1

thojanssens1

`:code.priv_dir(:my_app)` points to `priv` directory in `_build`

Hello,

I want to specify the path to the priv directory for user file uploads; I found this code:

:code.priv_dir(:my_app)

But it points to the priv directory in the _build directory:

‘c:/projects/my_app/_build/dev/lib/my_app/priv’

But then I was wondering, what is the difference with the priv directory in the root folder? I.e.

‘c:/projects/my_app/priv’

Are those two directories somehow in sync? Can I use the one or the other interchangeably?

Most Liked

kip

kip

ex_cldr Core Team

In dev mode, the my_app/priv dir is symlinked to to my_app/_build/dev/..... In prod it is copied to that dir. It’s actually controlled by the build_embedded key in the project/0 function in mix.exs.

This is because Elixir runs your compiled code which is stored in the my_app/_build directory. Since priv is build artifact, it is located there as well.

I think its better to consider the :code.priv_dir/1 to be part of the build process, not part of the runtime environment. And therefore I’d suggest considering an alternative location for your uploads.

11
Post #5
kokolegorille

kokolegorille

We are just in sync, I had the same thoughts.

But it’s not the same priv directory… the one You mention is for release. While the other is being used when starting the project with mix.

If You run this as a release, You can use

:code.priv_dir(:my_app)

# or 

Application.app_dir(:my_app)

# They are not really equivalent! but You can choose between both.

When it is used with mix, You should use priv from application root folder, because it is served by plug static.

I configured a path for dev and test, and when it is not configured… for example in prod, I use this

# in dev and test
config :waffle, storage_dir: "priv/static/uploads"

# in the uploader
  def storage_dir(version, {_file, scope}) do
    Path.join uploads_root(), "/painting/display_images/#{version}/#{scope.id}"
  end

  defp uploads_root do
    Application.get_env(:waffle, :storage_dir) || Application.app_dir(:calabrese, "priv/static/uploads")
  end

This allows me to have a different config between dev (mix) and prod (release).

It’s not the only way, You can also test if Mix is available with.

function_exported? Mix, :__info__, 1

# true with mix
# false in release

I found it was more elegant to define a static path for dev and test only than checking if Mix is available.

Where Next?

Popular in Questions Top

New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dokuzbir
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement