Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Does not actually work without downloading the full image #5

Open
mhluska opened this issue Feb 6, 2020 · 2 comments
Open

Does not actually work without downloading the full image #5

mhluska opened this issue Feb 6, 2020 · 2 comments

Comments

@mhluska
Copy link

mhluska commented Feb 6, 2020

Instead of doing what the readme recommends:

  add_metadata :dominant_color do |io|
    dominant_color(io.path)
  end

I had to do this:

  add_metadata :dominant_color do |io, **options|
    next unless options[:background]

    Shrine.with_file(io) { |file| dominant_color(file.path) }
  end

With the following background worker:

class ShrinePromoteWorker < ApplicationWorker
  def perform(attacher_class, record_class, record_id, name, file_data)
    attacher_class = Object.const_get(attacher_class)
    record         = Object.const_get(record_class).find(record_id)

    attacher = attacher_class.retrieve(model: record, name: name, file: file_data)
    attacher.refresh_metadata!(background: true)
    attacher.atomic_promote
  end
end

This is after updating to the latest shrine version. The reason is io.path is not defined for UploadedFile objects. We have to ultimately download the full file to get a color from it. And best to do it in a background job.

I'm not sure but this could be a case unique to people doing direct to S3 uploads 🤷‍♂

@mhluska
Copy link
Author

mhluska commented Feb 6, 2020

Also I'm not sure if it's the custom fork of colorscore that this uses or something in colorscore itself but i always ended up with puke green for many of the dominant colors.

I ended up switching to the miro gem and doing this:

  add_metadata :dominant_color do |io, **options|
    next unless options[:background]

    Shrine.with_file(io) do |file|
      Miro::DominantColors.new(file.path).to_hex.first
    end
  end

I think the reason miro produces better results is it handles the alpha channel of PNGs internally but colorscore doesn't.

My change above does renders this plugin useless for me 😅

@jnylen
Copy link
Owner

jnylen commented Feb 6, 2020

Hello @mhluska

As the s3 direct upload mean that the file doesn't actually end up on the server itself, it does need to download the file and grab the colors.

Yeah colorscore is a mess. I haven't touched any of this except upping the shrine version semver. If you do a PR for addition of Miro i'll merge it.

By the way, you should be able to do:

  add_metadata :dominant_color do |io, **options|
    next unless options[:background]
    Miro::DominantColors.new(io.url).to_hex.first
  end

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants