Setting tags and retrieving pHash while using Active Storage (Rails)

Options
clemoun
clemoun Member Posts: 9 ✭

Hello, πŸ‘‹

I would like to know if there's a way to do the following :

  1. Set tags directly in the attach method (Active Storage) without making a second API call
    • Something like @item.photos.attach(io: file, filename:filename, content_type: 'image', cloudinary{tags: ["dog","cat"]})
  2. Retrieve the pHash for a photo directly from the attach method (Active Storage) without making a second API call
    • Something like @photo = @item.photos.attach(...)and then @photo[:pHash] #"ba19c8ab5fa05a59"

Bonus : if there is a simple way to save the pHash in the photo blob, I would love to learn about it πŸ™‚

Thanks

Tagged:

Answers

  • aleksandar
    aleksandar Member, Cloudinary Staff Posts: 14
    Options

    Hi @clemoun,

    1) It should be possible to do that since you could pass metadata: { custom: {...} } in the attach method call and that should then be passed/available to the underlying storage service (Cloudinary). Could you please try the below and let me know if it works for you?

    @item.photos.attach(io: file, filename:filename, content_type: 'image', metadata: { custom: { tags: ["dog","cat"] } })
    

    Note that for the above, you'll want to have Rails version >7.0.0.

    2) Apart from making a second request, one alternative is using Webhook Notifications (https://cloudinary.com/documentation/notifications) in Cloudinary. When you request `phash: true` in your upload call (such as via custom metadata like in 1) and also specify a notification_url (or a Global Notification URL in your cloud's Upload settings) and we'll send the upload response (JSON payload) to the URL defined for notifications. That will contain the standard response and if you requested phash in your upload request, the response will contain the value. You could then listen for such upload notifications on your server and update any metadata or perform post-processing as needed.

  • clemoun
    clemoun Member Posts: 9 ✭
    Options

    Thanks a lot for your answers Aleksandar !

    Unfortunately I am currently under Rails v6.1.3 and not too familiar with webhooks...

    After giving it some thoughts, Active Storage does not look like a good fit for my use case so I decided to create my own models and callbacks so that I can handle everything directly with the Cloudinary gem (which is super easy to use).

    It took me a few hours to set up but now, it works perfectly and it's easier to customize. Working with Active Storage would have taken me about the same amount of time while ending up with a more complex system with little upside (hopefully ?)


    Thanks a lot for helping !!