Add version after upload

59 views
Skip to first unread message

nikli...@gmail.com

unread,
Apr 16, 2019, 4:16:28 PM4/16/19
to Shrine
Hi! I am trying to determine if Shrine would be a good fit for us as we are coming from using Carrierwave. The one question I can't seem to find an answer to, and I apologize if it is obvious, is how to add a new version of an image after the original file is uploaded? The workflow is something like this:
  1. Client uploads original image.
  2. Original image, and a processed thumbnail image is generated and saved.
  3. At a later time, the client specifies a custom cropped version of the original image.
Is it possible to add the cropped image to the uploaded image's version hash?

Nik L.

Janko Marohnić

unread,
Apr 19, 2019, 5:22:34 AM4/19/19
to Shrine, nikli...@gmail.com
At the moment there is no dedicated API to do so. We're working on a rewrite of the "versions" plugin where adding/removing new versions after attaching would be part of the API.

For now you can do something like this (e.g. we have a Photo model with an "image" attachment):

    # generate a cropped file using an operation
    cropped_file = photo.image[:original].download do |file|
      Cropper.call(file, **crop_params)
    end

    # upload the cropped file, passing crop params into metadata for safe keeping
    cropped_uploaded_file = photo.image_attacher.store!(cropped, metadata: crop_params)

    # add the cropped to the versions hash
    versions = photo.image
    versions[:cropped] = cropped_uploaded_file

    # update the attachment column with the updated versions hash
    photo.update(image_data: versions.to_json)

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 post to this group, send email to ruby-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/3f4ffb38-54dc-4c08-8739-753bca036290%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

nikli...@gmail.com

unread,
Apr 22, 2019, 5:10:54 PM4/22/19
to Shrine
This seems like it will help us with our use-case, thank you very much! What would be the best way to stay up to date on any news in regards to the versions plugin rewrite?
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-...@googlegroups.com.

Janko Marohnić

unread,
May 22, 2019, 7:07:15 PM5/22/19
to Shrine, nikli...@gmail.com
I just created an issue on GitHub where we'll be tracking the "versions" plugin rewrite, you can subscribe to it:


Kind regards,
Janko
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine...@googlegroups.com.

To post to this group, send email to ruby-...@googlegroups.com.

Blake LeBlanc

unread,
Jun 14, 2019, 3:54:46 PM6/14/19
to Shrine
Really looking forward to what you guys are able to come up with here, Janko :) Cropping multiple portions of an image and tying the results back into ActiveRecord is part of my workflow as well, similar to OP's. Also coming from Carrierwave, exploring how to implement with Shrine and image_processing. Your posts throughout the group and github have been a great reference so far!
Reply all
Reply to author
Forward
0 new messages