thojanssens1

thojanssens1

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?

Showing Posts 1 to 4

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.

thojanssens1

thojanssens1 OP

Why doesn’t Application.app_dir handle that? :sob:
I.e. if the app is run by mix, point to the root folder instead of the _build/dev/lib/... folder. You have to set some configs, add code, … to handle that; if the Elixir core function detected it that would have avoided all this code.

kokolegorille

kokolegorille

I am not a Core Team member :slight_smile:

But it’s easy to check how You run your application.

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 #4
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews