CKeditor and shrine

Did anyone got shrine working with ckeditor rails integration?

It seems someone add support for shrine with mongoid, but i can’t that working, and besides i need to get it working with activerecord.

Asking in case someone already made progress with that as i strugle a copule of days without luck, but i’ll continue investigating in either way.

Thanks!

Ok! I got some progress using this branch that has updated templates for shrine 3 with activerecord

Now the main issue i’m having is the urls that are being generated, as my bucket is private but normally urls on an html editor need to be public as the urls are stored inside the html.

So i was trying adding on the uploader (https://github.com/glebtv/ckeditor/blob/31664c2cb96e82b3213ec3eb32d4a3157df6de7b/lib/generators/ckeditor/templates/active_record/shrine/ckeditor/picture.rb) the public switchs on upload_options and url_options

module Ckeditor
  class PictureUploader < Shrine
    plugin :determine_mime_type
    plugin :validation_helpers
    plugin :derivatives
    plugin :upload_options, store: -> (io, **) { { acl: "public-read" } }
    plugin :url_options,    store: -> (io, **) { { public: true } }

    Attacher.validate do
      validate_mime_type_inclusion %w[image/jpeg image/gif image/png]
      validate_max_size 2.megabytes
    end

    Attacher.derivatives do |original|
      magick = SHRINE_PICTURE_PROCESSOR.source(original)

      {
        content:  magick.resize_to_limit!(800, 800),
        thumb: magick.resize_to_limit!(118, 100),
      }
    end
  end

  class Picture < Ckeditor::Asset
    include PictureUploader.attachment(:data)

    before_create do
      data_derivatives!
    end

    before_update do
      data_derivatives! if data_changed?
    end


    validates :data, presence: true

    def url_content
      data_url(:content)
    end

    def url_thumb
      data_url(:thumb)
    end

    def path
      data[:thumb].storage.path(data[:thumb].id)
    end
  end
end

But urls being generated are still private so they stop working after some time.

So the question is, do i’m using the upload_options and url_options in the right way? Or it’s because my bucket is private that urls are still private although setting them as public?

Thanks!

This looks correct, the URLs should be public now, unless URLs are generated with a different uploader.

It doesn’t matter if your S3 bucket is private, Shrine will generate private/public URLs according to configuration (private URLs by default, public if so configured).

Hello! So i think i found the problem i was seeing.

When uploading through ckeditor file browser, the picture is shown precisely after saving.

As i’m using the background plugin, the main file was still in cache, so the main url was generated just a few milliseconds before the promote job move the file to store. And it seems that for files on cache, so still not promoted, the main url generated is not public although having upload_options and url_options as public.
But once the file is promoted, if i refresh the ckeditor file browser, the main url generated is on store and correctly a public one.

Nevetherless, as derivatives (:content and :thumb) are created in a before hook right before saving, those are already on store after saving, and public urls are correctly generated for them.
So i just need to use any of them instead of the main one for presenting on ckeditor right after the file is upload and before the promote job run.

Knowing this i have now shrine working with ckeditor/activerecord :slight_smile:

1 Like