Teqnology
Teqnology

Reputation: 1298

Alfresco media viewer web-preview extensibility

So I followed Will Abson's guide and source code for extending custom media viewers in Alfresco. I have a couple of issues though. I'm already using 4.2+ Alfresco, so no need to use head.ftl as deprecated, I'm using a second extensibility module to add my own configuration automatically, BUT: how can I access the jsNode in my web-preview.get.js? Or better, is there a way to access properties values and aspects of a node which is being displayed? I know about both server and client side var jsNode = new Alfresco.util.Node(model.widgets[i].options.nodeRef) and var jsNode = AlfrescoUtil.getNodeDetails(model.widgets[i].options.nodeRef); which were mentioned in another question here, but it seems like except of default values like, mimeType, size, nodeRef, I'm not able to use those to get data from the file. These are my changes:

web-preview.get.js in -config folder of my custom media-viewer

    //<import resource="classpath:/alfresco/templates/org/alfresco/import/alfresco-util.js">
if (model.widgets)
{
   for (var i = 0; i < model.widgets.length; i++)
   {
      var at = "test";
      //var jsNode = AlfrescoUtil.getNodeDetails(model.widgets[i].options.nodeRef);
      //var author = jsNode.properties["cm:author"];
      var widget = model.widgets[i];
      if (widget.id == "WebPreview")
      {
         var conditions = [];
         // Insert new pluginCondition(s) at start of the chain
         conditions.push({
            attributes: {
               mimeType: "application/pdf"
            },
            plugins: [{
               name: "PDF",
               attributes: {
               }
            }]
         });
         var oldConditions = eval("(" + widget.options.pluginConditions + ")");
         // Add the other conditions back in
         for (var j = 0; j < oldConditions.length; j++)
         {
            conditions.push(oldConditions[j]);
         }
         // Override the original conditions
         model.pluginConditions = jsonUtils.toJSONString(conditions);
         widget.options.pluginConditions = model.pluginConditions;
      }
   }
}

PDF.js

/**
 * Copyright (C) 2014 Will Abson
 */

/**
 * This is the "PDF" plug-in used to display documents directly in the web browser.
 *
 * Supports the "application/pdf" mime types.
 *
 * @namespace Alfresco.WebPreview.prototype.Plugins
 * @class Alfresco.WebPreview.prototype.Plugins.PDF
 */
(function()
{
    /**
     * PDF plug-in constructor
     *
     * @param wp {Alfresco.WebPreview} The Alfresco.WebPreview instance that decides which plugin to use
     * @param attributes {Object} Arbitrary attributes brought in from the <plugin> element
     */
    Alfresco.WebPreview.prototype.Plugins.PDF = function(wp, attributes)
    {
       this.wp = wp;
       this.attributes = YAHOO.lang.merge(Alfresco.util.deepCopy(this.attributes), attributes);
       //this.wp.options.nodeRef = this.wp.nodeRef;
       return this;
    };

    Alfresco.WebPreview.prototype.Plugins.PDF.prototype =
    {
       /**
        * Attributes
        */
       attributes:
       {
          /**
           * Maximum size to display given in bytes if the node's content is used.
           * If the node content is larger than this value the image won't be displayed.
           * Note! This doesn't apply if src is set to a thumbnail.
           *
           * @property srcMaxSize
           * @type String
           * @default "2000000"
           */
          srcMaxSize: "2000000"
       },

       /**
        * Tests if the plugin can be used in the users browser.
        *
        * @method report
        * @return {String} Returns nothing if the plugin may be used, otherwise returns a message containing the reason
        *         it cant be used as a string.
        * @public
        */
       report: function PDF_report()
       {
          // TODO: Detect whether Adobe PDF plugin is installed, or if navigator is Chrome
          // See https://stackoverflow.com/questions/185952/how-do-i-detect-the-adobe-acrobat-version-installed-in-firefox-via-javascript
          var srcMaxSize = this.attributes.srcMaxSize;
          if (!this.attributes.src && srcMaxSize.match(/^\d+$/) && this.wp.options.size > parseInt(srcMaxSize))
          {
             return this.wp.msg("pdf.tooLargeFile", this.wp.options.name, Alfresco.util.formatFileSize(this.wp.options.size), Alfresco.util.formatFileSize(this.attributes.srcMaxSize));
          }
       },

       /**
        * Display the node.
        *
        * @method display
        * @public
        */
       display: function PDF_display()
       {
          // TODO: Support rendering the content of the thumbnail specified
          var src = this.wp.getContentUrl();
          var test = this.attributes.author;
          //var test = this.wp.options.nodeRef;
          //var jsNode = new Alfresco.util.Node(test);
          //var jsNode = AlfrescoUtil.getNodeDetails(this.wp.options.nodeRef);
          //var author = jsNode.properties["cm:author"];
          //var test = this.wp.options.author;
          //var test1 = this.wp.options.mimeType;
          //var test = this.attributes.author.replace(/[^\w_\-\. ]/g, "");
          //.replace(/[^\w_\-\. ]/g, "");
          return '<iframe name="' + test + '" src="' + src + '"></iframe>';
       }
    };

})();

As you can see by commented sections I tried different methods to access node properties/values, even simple strings, but I'm missing something for sure. Thanks.

Upvotes: 0

Views: 907

Answers (1)

Tahir Malik
Tahir Malik

Reputation: 6643

If you take a look at the source code you'll see that the helper method is nothing else than doing a remote call to var url = '/slingshot/doclib2/node/' + nodeRef.replace('://', '/');

So take a look at what that Repository WebScript is returning en match it to the properties you need.

I normally don't use this one and I know for sure the /api/metadata returns all the properties.

Upvotes: 2

Related Questions