Kurisu
How can I use unique filename generator function with arc_ecto?
I’ve already created my own function to generate unique string name per given directory, using a counter stored in the directory.
MyApp.File.generate_name/1.
Ex : name = MyApp.File.generate_name “uploads/user/avatars”
The generated file name doesn’t contain the file extension.
I tried to override Arc.Definition.filename function in the file generated by Arc.g avatar.
But when when I add new user with an avatar pic, file stored in disk name differs from name stored in column “avatar” of user schema. The first name (in disk) is what I expect from overrided filename function, and the second name (in database user table) is the initial uploaded pic name.
The other problem I’m facing is that each time the Arc.Definition.filename function is called the name is generated again. I try inside the function to guess when to generate a new name but it doesn’t work well. For each new user insertion it’s called twice. Here is my overriding code :
# Override the persisted filenames:
def filename(version, {file, _}) do
# is it a storage request ?
if Map.has_key?(file, :path) do
IO.inspect file
MyApp.File.generate_name("uploads/user/avatars/#{version}")
else
Path.rootname(file.file_name)
end
end
Could someone guide me please ?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #elixirconf-us
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 7 of 7 Posts!
Kurisu
Finally I’ve found why uploaded files are treated twice. The problem was in my user model changeset. Arc_Ecto provides a changeset function “cast_attachments(changeset, params, opts)” that manages the upload stuff. So I didn’t have to add my file field to the default “cast(params, opts)” function of my model changeset. Now that I add the upload field only to Arc_Ecto cast_attachments function, things go very better.
I still don’t have a solution to persist in DB my own generated filename but I found another solution. Now I’m ussing a UUID from elixir-uuid to save the uploaded pics. I just add a uuid column to my user schema, then check for its existence in the model changeset. If it’s nil, then I set it. ^^
Qqwy
I have been fiddling with getting proper filenames in arc/arc_ecto as well.
The final filename function I use right now:
My model contains a field with the type
Ecto.UUID, and in my changeset function I make sure this is set before thecast_attachmentscall by usingMain tip to give to people would be that Ecto already has UUID-generation/handling functionality built in, so using an external library is not required.
Oh, and the approach with using
!
Ecto.Changeset.get_fieldandforce_changeis definitely cleanerKurisu
@Qqwy
Your reply is very instructive for me. I just tested Ecto.UUID and found it perfect for the job. I will drop the external library. Thanks.
For the filename function I keep it simple like this :
I just worked on the storage_dir function to make unique path :
This way for each image file uploaded, we’ll have a folder named with generated uuid, containing all the versions created (thumb, original…). The point is that when I have to delete an entry in the DB, I can simply remove its uuid corresponding folder. Beside that, I found it simpler to write my own image_delete function in the custom uploader module :
To be honest I have to say that I was a bit confused with the delete function of arc, so I wrote my simple own. ^^
To finish I don’t know if will have any impact on DB performance but while I’m using an uuid field for arc._ecto usage, I’m still using an Ecto default primary key for the same model. I guess a proper way to do things, would be using the uuid field as primary key in the same time ?
dokicro
I ended up doing this:
:os.system_timeshould be always unique if I am not wrongOvermindDL1
Not ‘always’, but close enough for ‘most’ people not to care.
m31271n
Reference: Generate random file name · Issue #85 · stavro/arc · GitHub
julismz
Just a little apport to take in mind the performance:
And if you wanna use UUID, V3 and V5 will be your friends…