Newxan

Newxan

Hi, I’m using waffle to upload files a s3 bucket. It’s been working well by I’ve decided I wan’t to change what names are used for the files. So i implement the filename/2 as follows:

  # Override the persisted filenames:
  def filename(version, {file, scope}) do
    file_name = Path.basename(file.file_name, Path.extname(file.file_name))
    "#{file_name}_#{version}_#{:os.system_time()}"
  end

But now all my already uploaded files have incorrect references to them. Anyone know of a good way to do this, I guess I could just compare the files updated_at timestamp to see if it happend before or after the updated filename definition but I get the feeling I’m missing a more elegant solution.

Thanks in advance, any suggestions are appreciated :slight_smile:

Showing Posts 1 to 3

dimitarvp

dimitarvp

Not sure there’s an elegant solution per se. Maybe you should make a script that retroactively changes the filenames of all files who don’t conform to the new format yet.

tangui

tangui

You might be interested in this issue on Github: Support dynamic file naming.

Had the same problem recently, you can also make the change of filename by tweaking the changeset (see https://github.com/elixir-waffle/waffle_ecto/issues/13) However, Waffle automatically added file extension and I gave up with this solution. Using the underlying ExAws.S3 is actually not hard and gives more flexibility if you don’t need advanced Waffle features.

Newxan

Newxan OP

Thanks for your suggestions!
After banging my head against this trying to to find a safe and effective solution i decided to solve the problem outside of waffle. So in my controllers I now have a function that goes through the attachments in the parameters and adjusts the filenames in the different %Plug.Upload{}.

  def unique_attachments(params, options \\ []) do
    defaults = [
      field: "attachments"
    ]

    options = defaults |> Keyword.merge(options) |> Enum.into(%{})

    attachments = Map.get(params, options.field, %{})

    Enum.reduce(attachments, %{}, fn {key, value}, acc ->
      case Map.get(value, "file", nil) do
        nil ->
          Map.put(acc, key, value)

        file ->
          filename = Map.fetch!(file, :filename)
          unique_filename = "#{:os.system_time()}_#{filename}"
          file = Map.put(file, :filename, unique_filename)
          value = Map.put(value, "file", file)
          Map.put(acc, key, value)
      end
    end)
  end

It’s not elegant but it works and it does not risk corrupting my old existing attachments, since it will only apply to new files. :slightly_smiling_face:

— 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
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
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
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
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

Other Trending Topics Top

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
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews