



There is an interesting but annoying issue with Alfresco document links. It is way too long and it looks particularly terrible in email messages. Here is a good example
http://localhost:8080/alfresco/d/a/workspace/SpacesStore/
5b302efc-2ce5-11dd-a496-9db70aebc347/IMG_1422_932.JPG
Well, it does provide a lot information for developers, we can tell the NodeRef and file name from the link. But for business users, the long string of NodeRef means nothing but some mystical stuff that terrifies them. And we all know business users are important in real life.
So let us find out some simple solutions to shorten the links.
One quick solutions is to use the services to provide by vendors like TinyURL. But is that really necessary? The answer is no.
How about writing a simple webscript and assign it a short URL with a short unique ID?
The first thing pops in my mind is using db id. It is an integer and it is unique for all managed objects in Alfresco repository. The shortest URL we can get for webscript url can be something like
http://localhost:8080/s/d
and we can append the db id after that. So a really short URL can look like
http://localhost:8080/s/d/1234
For webscript, we can first get the db id from the URL and then run a lucene query using the db id. Once we get hold of the returned document, we can find out the download link, the long one, and then setup the forward status code and make it forward to the real download link.
// Get the db id
var dbid = url.extension;
// Prepare the Lucene Query String
var luceneQueryStr = “@sys\\:node-dbid:”+dbid;
// Execute the query
var nodes = search.luceneSearch(luceneQueryStr);
// Find the document
var node = nodes[0];
// Get the download link
var nodeUrl = node.url;
// Setup the forward
status.code = 303; // Temporary redirect
status.location = url.context+nodeUrl;
Now let us add up rest of sanity checkings to the script
script:
{
var dbid = url.extension;
if ( dbid == null ) {
status.code = 400;
status.message = “DB id has not been provided.”;
status.redirect = true;
break script;
} else {
var luceneQueryStr = “@sys\\:node-dbid:”+dbid;
var nodes = search.luceneSearch(luceneQueryStr);
if ( nodes != null ) {
var node = nodes[0];
if ( node != null && node.isDocument ) {
var nodeUrl = node.url;
status.code = 303; // Temporary redirect
status.location = url.context+nodeUrl;
} else {
status.code = 400;
status.message = “Document with given DB id “+dbid+” is not a document.”;
status.redirect = true;
break script;
}
} else {
status.code = 404;
status.message = “Document with given DB id “+dbid+” has not been found.”;
status.redirect = true;
break script;
}
}
}
And you can get the complete webscript from here.

