Giter Club home page Giter Club logo

Comments (10)

stavro avatar stavro commented on August 29, 2024 4

There is no convenience yet for deleting objects through Ecto / arc_ecto. I will consider adding this.

However, to delete for now, the best option is likely to:

  1. Update the user to remove the reference to the avatar (do this first, so all new image urls will result in nil or the default url. You never want to show a url to a removed object, which can happen if you don't do this first.
  2. After the record is nillified, remove it from the file storage (this can even be done async, as it will be a slower operation).
user = Repo.get!(User, 1) # Assume there is a property :avatar on User

user
|> User.changeset(%{avatar: nil})
|> Repo.update!()

:ok = Avatar.delete({user.avatar, user})

# Since the above deletion doesn't really need to happen synchronously, you can delete it asynchronously to speed up the request/response.
# spawn(fn -> Avatar.delete({user.avatar, user}) end)

from arc_ecto.

GBH avatar GBH commented on August 29, 2024 4

Deleting seems to remove file(s), but not directory it was in.

from arc_ecto.

bchase avatar bchase commented on August 29, 2024 3

@mamantoha I was running into the same issue, but got things working with:

attachment = Repo.get Attachment, 1
path = Rumbl.Image.url({attachment.file, attachment})
[path | _] = String.split path, "?" # strips the "?v=1234" from the URL string
Rumbl.Image.delete({path, attachment})

Or perhaps more idiomatically:

attachment = Repo.get Attachment, 1
path =
  Rumbl.Image.url({attachment.file, attachment})
  |> String.split("?")
  |> List.first 
Rumbl.Image.delete({path, attachment})

Apparently the return from Image.url/1 needs to be trimmed as follows to make it usable by Image.delete/1:

"/foo/bar?v=1234"
"/foo/bar"

Hope this helps!

from arc_ecto.

mamantoha avatar mamantoha commented on August 29, 2024 2

When I try to delete attachment from iex -S mix nothing happens:

attachment = Repo.get Attachment, 1
path = Rumbl.Image.url({attachment.file, attachment})
Rumbl.Image.delete({path, attachment})

It returns :ok but the file still exists on the hard drive.

from arc_ecto.

brayhoward avatar brayhoward commented on August 29, 2024 1

@jerodsanto I fugured out a way to delete the image going by the documentation for delete in the Arc README

Assuming you have already stored and uploaded an avatar to this user deleting it will look something like this.

# With a scope:
user = Repo.get! User, 1
path = YourApp.Avatar.url({user.avatar, user})

Avatar.delete({path, user})

The problem is that it only deletes the the image from wherever your storing it at, ex: S3, but doesn't remove the path url that is associated to the model.
so calling YourApp.Avatar.url({user.avatar, user}) returns a broken url which isn't worth a fuck really.

If I figure out how to get the correct behavior I'll post an update here.

from arc_ecto.

jgonera avatar jgonera commented on August 29, 2024 1

Are there any plans to add an easier / more automatic way of removing files? Ideally I would like to just set the property for my attachment to nil and have arc_ecto take care of removing the file.

from arc_ecto.

brayhoward avatar brayhoward commented on August 29, 2024

Thank you @stavro.
This is much better than what I was trying to implement!

from arc_ecto.

brayhoward avatar brayhoward commented on August 29, 2024

This is pretty straight forward. I think updating the readme will be sufficient. I'll do that tomorrow and submit a PR. Thanks again.

from arc_ecto.

brayhoward avatar brayhoward commented on August 29, 2024

When calling User.changeset(user, %{avatar: nil})

It's raising this error:
(FunctionClauseError) no function clause matching in Arc.Actions.Store.store/2

Any thoughts on what to do next?

from arc_ecto.

stavro avatar stavro commented on August 29, 2024

Ah - if you currently send nil through cast_attachments it will fail.

Ok - for now send it through cast or use change directly:

Ecto.Changeset.change(user, %{avatar: nil})

rather than sending through cast_attachments. I'll get this fixed shortly.

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.