Search code examples
ruby-on-railsdragonfly-gem

Rails/Dragonfly: is it possible to decode URL (/media/W1siZiIsInRzYW...) to get the model instance it refers to?


This is what I want to accomplish:

On an extranet with a feed wall, users just type non-formatted text (non style, just \n and links recognition).

But quite often, users want to add a link to a document which is stored in the same extranet (using dragonfly). Obviously, the link is quite awful to display (ex: https://extranet.com/media/l0ngUiD/original_filename.pdf?sha=31310881DAEF1). This document refers to a Document instance which has a nice title, ex: "Original Filename (PDF)" I would like those links to be (automatically) replaced by

<a href="https://extranet.com/media/l0ngUiD/original_filename.pdf?sha=31310881DAEF1">Original Filename (PDF)</a>

Problem is: how to find which model and which document this document refers to, using the UID and sha.

I guess this is possible as Dragonfly decodes the url, but I can't find how to (not much comments in the code).


Solution

  • The variant's UID is Base64 encoded, so when decoding the UID you will get a JSON encoded array of Dragonfly processor steps.

    Here an actual example from a live database:

    uid = 'W1siZiIsIjIwMTUvMDcvMDkvMDkvMTMvMDIvOTE3LzE5NDg3ODk0MDc1XzE4NGYzMjc0MWVfay5qcGciXV0'
    Base64.decode64 uid
    # => "[[\"f\",\"2015/07/09/09/13/02/917/19487894075_184f32741e_k.jpg\"]]"
    

    Each step is an array with the step's operation in the first element and the step's arguments in the remaining.