Need help updating an element inside params

I have this:

%{
  "file" => %Plug.Upload{
    content_type: "application/msword",
    filename: "luggar-cat6-sftp-bc-23awg-nyy-305m.doc",
    path: "/tmp/plug-1643/multipart-1643576402-233496152491789-3"
  },
  "record_id" => "12"
}

I want to change the file name to something else, how do that, tried by Map.update but failed. Thank you.

Path is the real filename, filename is just an indication.

As it is a plug upload, it should be temporary. You need to consume it.

I usually do this with File.rename!(path, dest) with whatever name I want for dest. But I don’t try to update the Plug.Upload map.

I need to do so because the built in method in waffle for file renaming is not working with waffle_ecto (changes file name on storage but not in DB). So, I think changing it here would solve the issue. Updating nested maps confuses me. I have tried, but the resulting object is not accepted as a changeset.

You should show your uploader code…

I do rename my files without problem.

For example, I use Slugger to rename filename (with Waffle and Waffle Ecto).

  def filename(_version, {file, scope}) do
    filename = file.file_name
      |> Path.basename(Path.extname(file.file_name))
      |> slugify()

    "#{scope.permalink}_#{filename}"
  end

  # Slugify filename
  defp slugify(filename) do
    Slugger.slugify(filename)
  end
1 Like

Do you think the scope.permalink thing is esssential? I have tried without that part…

This is a sample of my config, where I use a permalink field to identify my resources.

I use for filename a combination of a custom field, and slug of the original name.

It won’t work unless You define your resources like I do :slight_smile:

This thing is not working on latest versions of waffle and waffle_ecto. File saved to disk as specified by the function but in DB, original filename is used. Something wrong there.