cloudinary / cloudinary_gem

Cloudinary GEM for Ruby on Rails integration
https://cloudinary.com
420 stars 285 forks source link

Contextual metadata support for url_for_direct_upload #536

Closed jaydorsey closed 6 months ago

jaydorsey commented 7 months ago

Feature request for Cloudinary Ruby SDK

When using ActiveStorage, and calling url_for_direct_upload, I would like the ability to include contextual metadata when I generate my URLS

Explain your use case

  1. Our frontend is handling the upload of the image blob
  2. Our backend is using active storage and url_for_direct_upload to generate a URL which is used to upload the blob
  3. We have additional context metadata that we want the backend to send when the URL is created, rather than via a separate update API call. This is generally "business rule" data that the backend should control/set. Example: Annotating the unique identifier of a person or system that uploaded an asset

Describe the problem you’re trying to solve

An example call from a Ruby class:

class User
  has_one_attached :profile_photo
end

u = User.first
blob = ActiveStorage::Blob.create_before_direct_upload!(
  key:, filename:, byte_size:, checksum:, 
  content_type:, metadata:, service_name: :cloudinary
)

blob.service.url_for_direct_upload(
  blob.key,
  expires_in: ActiveStorage.service_urls_expire_in, 
  content_type: blob.content_type,
  content_length: blob.byte_size, 
  checksum: blob.checksum,
  access_mode: 'authenticated',
  # This is the data I want to send to cloudinary
  context: { caption:, alt:, user_id: u.id }
)

The cloudinary service hard-codes the context sent via url_for_direct_upload here

options[:context] = {active_storage_key: key}

What cloudinary does with this data is shown in this screenshots from the cloudinary UI.

active_storage_key is what cloudinary sent. company is something I sent using my patched gem, as a demo that you can just send extra key/value pairs and the API itself supports it

image

Do you have a proposed solution?

I have a PR here

I have some concerns about the naming convention I chose that are annotated in the PR as well:

  1. Whether sending context with the upload url request like this is a good behavior
  2. Whether there would be a conflict (key naming?) if rails 7 custom metadata support was added, or if there is a cloudinary strategy/interest to support that feature
  3. Whether this would conflict with supporting both contextual metadata & structured metadata
tamaracloudinary commented 6 months ago

Hi @jaydorsey, Thanks for the PR, I've forwarded your suggestion to our development team for consideration, and they will respond soon. Warm regards, Tamara

const-cloudinary commented 6 months ago

Fixed in #535