arc_ecto
arc_ecto copied to clipboard
Create option, to specify base path of image urls
In a typical Phoenix app, you would probably like to store the uploaded the images in the priv/static directory, as it's exposed to the public.
As i result i specified the following in my uploader.
# Override the storage directory:
def storage_dir(version, {file, scope}) do
"priv/static/images/company/logo/#{scope.id}"
end
Now the problem is that, when i try to fetch a url for my model,
<img src="<%= Mobil.Logo.url({company.logo, company}, :thumb) %>
I following path returned
priv/static/images/company/logo/1/thumb.png?v=63608960064
Which is not wrong, but priv/static is exposed, as only what's static folder is, the ideal path would be.
/company/logo/1/thumb.png?v=63608960064
So i guess i need i way to specify, i don't know a base level? of which the url helpers should look in?
Thanks - Let me think about this.
I personally don't use local storage as the apps I'm building are scaled across multiple machines - but this is something I would like to support.
@MartinElvar I've found two things that work now:
(1) Pipe the path through Path.relative_to
:
def logo_url(company) do
Mobil.Logo.url({company.logo, company}, :thumb)
|> Path.relative_to("priv/static")
end
(2) Or go with the default uploads directory and specify in lib/{your_app}/endpoint.ex
, underneath the first plug Plug.Static
, a second plug Plug.Static
to serve static assets from the upload directory:
plug Plug.Static,
at: "/uploads", from: Path.expand('./uploads'), gzip: false
Fair warning: I've only been using LocalStorage for sample apps and am using S3 for anything larger than that.
@posgarou Thank you for your feedback, i actually ended up creating this lille helper.
def correct_image_path(path) do
String.replace path, "priv/static/", "/"
end
I love your suggestions tho, specially #2, maybe we should add this to the documentation @stavro? :smile:
Thanks @posgarou, this was very helpful! I used (2)
and it worked very well.
@bratsche Glad to hear it helped!
Kudos to @posgarou second suggestion works great!
@posgarou Thanks for the hints, (2)
worked for me but only after modifying the default plug like so:
plug Plug.Static,
at: "/", from: :panda, gzip: false,
only: ~w(css fonts images js effort_csvs favicon.ico robots.txt)
Here effort_csvs
is my upload folder. Then I had to write a helper to replace the priv/static
as you might imagine. I explained my answer in more detail here: http://stackoverflow.com/a/38690793/1342734
@posgarou I did something similar to what @MartinElvar did, and it worked great on development. But when I made a release with exrm and deployed the compiled app (let's say to /apps/my-app/
), uploading a file would then upload to a dir at /apps/my-app/uploads/
, which isn't accessible from the compiled app, since it only looks in `priv/static. Has anyone had success with an exrm release and storing files locally?
@posgarou I used this option plug Plug.Static, at: "/uploads", from: Path.expand('./uploads'), gzip: false
This shows the images in the index page. But the problem is when I try to view a single record in the show action, it doesn't work. Looking at the path generated for the image, it adds the name of the model to the path. Please has anyone encountered this before?
@cd-rum Yes! This helped. It seems that if you're using a folder within priv/static
to store yout assets, you need to add that folder to the original list (attachments
in my case)
plug Plug.Static,
at: "/", from: :properties, gzip: false,
only: ~w(css fonts images attachments js favicon.ico robots.txt)