Giter Club home page Giter Club logo

Comments (5)

andreapavoni avatar andreapavoni commented on August 29, 2024 2

Your question is a bit too generic because you didn't specified if you are trying to store files in dedicated folders and/or you're going to generate several versions of the image.

As a hint, you can do this:

  • generate a UUID in changeset and append into a dedicated field in your schema (say uuid), this will be saved within your model. Be sure to do this before calling cast_attachments, otherwise the scope will have uuid set to nil:
def changeset(struct, params \\ %{}) do
  struct
  |> cast(...)
  |> put_change(:uuid, UUID.uuid4())
  |> cast_attachments(...)
  |> ...
end
  • override the function filename in your definition file (aka uploader), I suppose you called it Avatar, like in the examples provided in README. Then something like this should work:
def filename(_version, {_file, scope}) do
  "#{scope.uuid}"
end

if you also want different versions, then modify the function like this:

def filename(version, {_file, scope}) do
  "{scope.uuid}_#{version}"
end

hope this helps ;-)

from arc_ecto.

lekevicius avatar lekevicius commented on August 29, 2024

Here's the final solution that satisfied me - works without adding additional field to schema, and instead renames uploaded filename and persists that to database:

Person model:

def changeset(struct, params \\ %{}) do
  params = if params["photo"] do
    %{ params | "photo" => %Plug.Upload{ params["photo"] | filename: UUID.uuid4() <> Path.extname(params["photo"].filename) } }
  else
    param
  end

  struct
  |> cast(params, [:first_name, :last_name])
  |> cast_attachments(params, [:photo])
end

Photo uploader:

def filename(version, {file, _scope}) do
  "#{Path.rootname(file.file_name)}_#{version}"
end

Works really well: all versions are saved, useless original filename is not persisted, instead it renames it to UUID and deals with that normally.

from arc_ecto.

andreapavoni avatar andreapavoni commented on August 29, 2024

Does it work across non-valid forms? It looks like your UUID generation happens every time you call changeset. So in case you have to re-submit the form, you'll get different filenames from the first submit.
Another pain point is the storage_dir: in my use case, I want to namespace each upload (and its versions as well) under a unique/dedicated folder, this can't be done by only renaming filenames.

That's why I've used an additional field on schema.

For completeness, here's how I've implemented filename and storage_dir in my uploader definition:

...
def storage_dir(_, {_file, scope}) do
  "uploads/images/#{scope.uuid}"
end

def filename(_version, {file, _scope}) do
  "#{Path.rootname(file.file_name)}"
end
...

as you can see, I don't care about filenames, but I care to store file(s) under a unique folder.

from arc_ecto.

andreyk-code avatar andreyk-code commented on August 29, 2024

Believe this is duplicate of #15
There are more example workarounds on that thread

from arc_ecto.

andreapavoni avatar andreapavoni commented on August 29, 2024

@andreyk-code they're related because that's the same problem, but they don't solve it as well.

btw, I've forked arc_ecto to use a different approach/workaround. it's not perfect, but it's working well in production on 2 apps.

Here's the fork in case you're interested: https://github.com/FunkyStudioHQ/arc_ecto_ng

from arc_ecto.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.