Integrating with Filestack

22 views
Skip to first unread message

Paul Mackay

unread,
Sep 16, 2019, 12:51:42 PM9/16/19
to Shrine
I am attempting to build the elements needed to integrate with Filestack. I've written a very basic Storage plugin that uses the filestack-ruby gem to handle its operations. The bit I am less sure about is the best way to handle saving files.

We use Filestack.js in the frontend to save directly to Filestack. Then a metadata chunk of JSON can be passed down as part of saving the form. So far to save this to the model, I'm trying this:

I have a permanent store setup thus, with no cache:

Shrine.storages = {
  # cache: Shrine::Storage::Filestack.new(**filestack_options),
  store: Shrine::Storage::Filestack.new(**filestack_options),
}

Then to save a file to a model, am doing:

# Metadata is JSON data Filestack returns from saving a file
metadata = JSON.parse(metadata)
image_data = {
  id: metadata['handle'], # Filestack captures an ID for the file in a "handle" field
  storage: 'store',
  metadata: metadata
}
image_data = JSON.dump(image_data)

attacher = ImageUploader::Attacher.new(post, :image)
uploaded_file = Shrine.uploaded_file(image_data)
attacher.set(uploaded_file)

This seems to save the data to the field, is this the right kind of approach for Filestack where it saves from client-side and returns a full metadata blob?

Any tips much appreciated!

Janko Marohnić

unread,
Sep 17, 2019, 6:01:08 AM9/17/19
to Shrine, Paul Mackay
This looks fine to me. It even seems worth moving into a plugin, e.g:

  module Shrine::Plugins::Filestack
    module InstanceMethods
      def filestack_file(metadata)
        metadata = JSON.parse(metadata)

        file_data = {
          id:       metadata["handle"],
          storage:  storage_key,
          metadata: metadata,
        }
        file_data = JSON.dump(file_data)

        self.class.uploaded_file(file_data)
      end
    end
  end

  Shrine.plugin Shrine::Plugins::Filestack

(Note that in Shrine 3.0 you'll be able to remove the `JSON.dump` part.) Now in your controller you'd be able to do:

  attacher = ImageUploader::Attacher.new(post, :image)
  uploaded_file = attacher.store.filestack_file(metadata)
  attacher.set(uploaded_file)

Just note that without temporary storage, if the Filestack handle is visible from the file URL, it's theoretically possible for an attacker to hijack other users' files. That's one of the reasons Shrine uses temporary storage.

Also, note that we've deprecated the google group over Discourse :)

Kind regards,
Janko
--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/ce264f87-7118-4a39-9de8-35c267ae1c8c%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages