镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Photo metadata is nil until after AR callbacks have run #499

Open

Description

@kris

Describe the bug in a sentence or two.

I'm trying to extract colors from an uploaded image, following the docs on utilizing cloudinary_transformation and an after_save on the model. Unfortunately, metadata seems to always be nil. Once the after_save callback has finished, then source.metadata is populated. This seems like a misplaced lifecycle callback or something.

Issue Type (Can be multiple)

  • Build - Cannot install or import the SDK
  • Performance - Performance issues
  • Behaviour - Functions are not working as expected (such as generate URL)
  • Documentation - Inconsistency between the docs and behaviour
  • Other (Specify)

Steps to reproduce

class PhotoUploader < CarrierWave::Uploader::Base
  include Cloudinary::CarrierWave
  cloudinary_transformation image_metadata: true, colors: true
end

class Photo < ApplicationRecord
  mount_uploader :source, PhotoUploader

  after_save :extract_colors

  def extract_colors
    # `source.metadata` is `nil`
  end
end

Environment and Libraries (fill in the version numbers)

  • Cloudinary Ruby SDK version - 1.23.0
  • Ruby Version - 3.1.2
  • Rails Version - 7.0.4
  • Carrierwave - 2.2.2

Activity

  1. tommyg-cld commented on Sep 30, 2022

    @tommyg-cld
    Contributor

    @kris I just tested this and I'm unable to replicate it, I'm basically using the photo_album sample from https://github.com/cloudinary/cloudinary_gem/tree/master/samples and I just modified image_uploader to

    class ImageUploader < CarrierWave::Uploader::Base
      include Sprockets::Rails::Helper
    
      include Cloudinary::CarrierWave
      
      process :tags => ["photo_album_sample"]
      process :convert => "jpg"
      cloudinary_transformation :image_metadata => true, :colors => true
    
      version :thumbnail do
        eager
        resize_to_fit(150, 150)
        cloudinary_transformation :quality => 80          
      end 
    
    end
    

    And photo model to

    class Photo < ActiveRecord::Base
      belongs_to :album
    
      mount_uploader :image, ImageUploader
    
      validates_presence_of :title, :image
    
      after_save :extract_colors
    
      def extract_colors
        if self.image.present? && self.image.metadata.present?
          exif = self.image.metadata["image_metadata"]
          colors = self.image.metadata["colors"]
        end
      end
    end
    

    Can you try like that?

  2. kris commented on Oct 16, 2022

    @kris
    ContributorAuthor

    @tommyg-cld Sorry for the late reply. It still doesn't work. Try this:

    1. Add a binding.pry in #extract_colors
    2. Check image.metadata -- for me it's nil
    3. Exit pry
    4. Now check object.image.metadata and you will see it's set. The problem is that metadata appears to be nil until after the after_save is called.
  3. tommyg-cld commented on Oct 25, 2022

    @tommyg-cld
    Contributor

    @kris sorry for the delay. So yes after_save needs to be called so that the object is created first. Once the object is created, you can then access image.metadata.

    Can you explain what exactly you are trying to achieve?

  4. kris commented on Nov 3, 2022

    @kris
    ContributorAuthor

    @tommyg-cld When an image is uploaded, I wanted to have a callback that is triggered on the model to cache the extracted colors from Cloudinary. I was hoping to do this within a callback, but my current solution is to have an operation/service that saves the object, then updates the objects color cache via the mounted image, then call again. Less than ideal compared to an after_save, but I guess this is as good as I'll get.

  5. tommyg-cld commented on Nov 4, 2022

    @tommyg-cld
    Contributor

    @kris can you just not use after_save then?

  6. kris commented on Nov 7, 2022

    @kris
    ContributorAuthor

    @tommyg-cld metadata is nil for me in after_save. I'll create a repo over the next few days to demonstrate.

  7. self-assigned this
    on May 9, 2023
  8. tommyg-cld commented on May 9, 2023

    @tommyg-cld
    Contributor

    @kris just following up to check if this is still an issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions