Skip to content

Instantly share code, notes, and snippets.

@agea
Created May 24, 2012 06:48
Show Gist options
  • Save agea/2779867 to your computer and use it in GitHub Desktop.
Save agea/2779867 to your computer and use it in GitHub Desktop.
Alfresco Short URL

How to create a simple url shortener for Alfresco (>=4.0):

  1. Create the Shortable Aspect:

    • Copy shortUrlModel.xml in the repository in /Data dictionary/Models
    • edit document properties and activate the model
  2. Make the aspect and the property visible:

    • Add the content of share-config-custom.xml to /tomcat/shared/classes/alfresco/web-extension/share-config-custom.xml
    • restart Alfresco
  3. Create the script to generate the short url:

  4. Create a Rule in a folder (can be the root of the document library of your share site):

    • When the content is added
    • For all the content NOT having the aspect "Short url enabled"
    • Execute two actions:
      • Add "Short url enabled" aspect
      • Execute Script createShortUrl.js
    • Apply rule to subfolders
  5. If you add a document to the folder you will see it will have a property (dw:shorturl) of 3 or 4 chars eg. xyz

  6. To have a really short url you can use, for example, a rewrite rule on Apache as in rewrite-rule.conf

var shortUrl = document.properties['dw:shorturl'];
while (!shortUrl){
var newUrl = "";
var length = 3;
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < length; i++ ){
newUrl += possible.charAt(Math.floor(Math.random() * possible.length));
}
var nodes = search.luceneSearch("@dw\\:shorturl:"+newUrl);
if (nodes.length==0){
shortUrl = newUrl;
document.properties['dw:shorturl']=shortUrl;
}
length++;
}
document.save();
RewriteEngine On
RewriteRule ^/s/(.*)$ /share/page/search?t=dw:shorturl:$1 [R]
<config evaluator="aspect" condition="dw:shortable">
<forms>
<form>
<field-visibility>
<show id="dw:shorturl" for-mode="view"/>
</field-visibility>
</form>
</forms>
</config>
<config evaluator="string-compare" condition="DocumentLibrary">
<aspects>
<visible>
<aspect name="dw:shortable" />
</visible>
</aspects>
</config>
<model xmlns="http://www.alfresco.org/model/dictionary/1.0" name="dw:shortable">
<description>Shortable Aspect</description>
<author>Andrea Agili</author>
<version>1.0</version>
<imports>
<import uri="http://www.alfresco.org/model/dictionary/1.0" prefix="d"/>
<import uri="http://www.alfresco.org/model/content/1.0" prefix="cm"/>
</imports>
<namespaces>
<namespace uri="http://drwolf.it/model/1.0" prefix="dw"/>
</namespaces>
<aspects>
<aspect name="dw:shortable">
<title>Short url enabled</title>
<properties>
<property name="dw:shorturl">
<type>d:text</type>
</property>
</properties>
</aspect>
</aspects>
</model>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment