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.

10
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

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
aalberti333
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
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement