Alfresco – Disabling file upload by MIME type with 1 line of JS code

Alfresco handles every file format, in fact there is no limitation out of the box in order to restrict allowed MIME types. Executables, databases and any other potentially harmful content can be stored inside.

Developers can control this feature easily by listening onCreateNode event from NodePolicy. However, system administrators and other users without this skill require a simpler way. Below this simple approach is described.

Creating a one line server JavaScript

Create a new file at Repository > Data Dictionary > Scripts named exception.js including following contents:

throw "Unsupported file format";

alfresco-mimetype-javascript

Creating one rule by every forbidden mime type

Following screen capture includes a folder rule in order to avoid users uploading binary files: previous JavaScript is executed for every new file being binary.

alfresco-mimetype-rule

Result

Once this rule is working, users will receive an Unsupported file format message every time a binary file is uploaded by using Alfresco Share.

alfresco-mimetype-share

On the other hand, uploading binary files to this folder by any other means (WebDAV, FTP, email…) will produce also this error.

This type of content will not be uploaded to Alfresco in any case.

Other resources

GitHub Addon – https://github.com/keensoft/alfresco-mimetype-blocker

Anuncios

Alfresco – Customizing alfresco-unzip-action addon

Related to my last article Sending a notification email to Site Administrators by using JavaScript API, we are going to customize alfresco-unzip-action by using the same principles. This addon include one new Share action to unzip inside Alfresco any ZIP file.

Following JavaScript unzips a ZIP file on the same folder (by using the action defined by alfresco-unzip-action). Next, we’ll define a new rule in order to throw this script every time a new document is uploaded to the folder.

unzip.js


function main() {
    var importer = actions.create("import");
    importer.parameters.encoding = "UTF-8";
    importer.parameters.destination = document.parent;
    importer.execute(document);
}

main();

In order to have this script available in web interface, we need to upload it to scripts folder repository (Data Dictionary > Scripts)

Rule definition

Once our script is available, just choose one site folder and create a new rule by using Run script option and setting:

  • When:
    • Items are created or enter this folder
  • If all criteria are met:
    • MIME type for cm:content is ‘ZIP’
  • Run action
    • Run script ‘unzip.js’

From now, every time a new zip file is uploaded to the folder, its content will be unzipped in the same location.

Alfresco – Sending a notification email to Site Administrators by using JavaScript API

It occurs that we developers like to use Alfresco SDK to customize Alfresco in every way. However Alfresco provides a wide catalog of techniques to develop our projects and every technique is more or less suitable for every purpose. In this article, we are going to show how to send an email to every Site Administrator every time a document is uploaded to a folder.

Following tools are going to be used:

Script development

This script will be executed in the scope of a Site folder, so we are going to get access to required root objects in order to get Site Administrators list and their emails.

sendMailSiteManagers.js


function sendMail() {

    var site = siteService.getSite(document.siteShortName);
    var members = site.listMembers(null, "SiteManager", 0, false);

    for (userName in members) {

        var person = people.getPerson(userName);
        var email = person.properties["cm:email"];

        var mail = actions.create("mail");
        mail.parameters.to = email;
        mail.parameters.subject = "New document available: " + document.name;
        mail.parameters.template =
            companyhome.childByNamePath("Data Dictionary/Email Templates/Notify Email Templates/notify_user_email.html.ftl");
        mail.execute(document);

    }

}
sendMail();

In order to have this script available in web interface, we need to upload it to scripts folder repository (Data Dictionary > Scripts)

alfresco-js-deploy-folder

Rule definition

Once our script is available, just choose one site folder and create a new rule by using Run script option

alfresco-share-define-rule

 

From now, every Site Administrator will receive a new mail (based on notify_user_email.html.ftl email template) every time a new document is uploaded to this folder.

No special technical abilty is required to customize an Alfresco instance. Everyone have chance to adapt the product to their requirements.

Edited

As Bindu points out at the comments, this script can be simplified by using to_many mail parameter.

function sendMail()
{

    var mail = actions.create("mail");
    mail.parameters.to_many = "GROUP_site_" + document.siteShortName + "_SiteManager";
        mail.parameters.subject = "New document available: " + document.name;
        mail.parameters.template =
            companyhome.childByNamePath("Data Dictionary/Email Templates/Notify Email Templates/notify_user_email.html.ftl");

    mail.execute(document);

}
sendMail();