AEM 6420 - Generate High resolution thumbnails of docs using InDesign server for zoom in AEM

Goal


Enterprises with AEM Assets as central repository for images can use AEM Desktop App for CC collaboration. Using the app, assets within AEM are easily accessible on local desktop and can be used in desktop applications like InDesign for placing on pages and create documents, upload created documents to AEM

Using AEM and InDesign server integration, the Media extraction workflow step of DAM Update Asset workflow creates thumbnail renditions, adds extracted pages as subassets etc. The web rendition thumbnail cq5dam.web.1280.1280.jpeg, generated with otb scripts is of low resolution (created from embedded page preview) and results in blurry/pixelated image when zoomed in AEM /assetdetails.html (adding AEM desktop app on InDesign server also does not help)

This post is on adding necessary scripts to Media Extraction, for downloading original binaries of referenced assets in AEM, relink and generate high resolution web rendition thumbnail and subassets for better zoom experience (eg. to read fine print in marketing approval workflow process)

Thank you Kiran Murugulla for the bug fixes

Demo | Package Install | Github


Product (exports Low resolution thumbnail)



Custom Script (exports High resolution thumbnail)



Solution


1) Media extraction step picks the script from /libs for relative path dam/indesign/scripts/ThumbnailExport.jsx specified in Extend Scripts multifield, if the same path does not exist in /apps, so otb way of generating thumbnails



2) Override otb thumbnail export script /libs/settings/dam/indesign/scripts/ThumbnailExport.jsx by creating /apps/settings/dam/indesign/scripts/ThumbnailExport.jsx, add the following code

                         a. eaemGetAllLinks() - get all link paths (placing images using desktop app, they all are AEM paths)

                         b. eaemDownloadAndGetHighResMap() - download the original binaries of referenced images from AEM, to a temp folder

                         c. eaemReplaceLowResWithHighRes() - relink to downloaded high res images

                         d. eaemExportThumbnailUsingIDSIndd() - export and upload the high res web thumbnail to AEM

app.consoleout('EAEM - Relinking to High-Res Originals downloading from AEM...');

eaemReplaceLowResWithHighRes(document);

eaemExportThumbnailUsingIDSIndd(document);

function eaemDownloadAndGetHighResMap(aemLinks){
    var aemToLocalMap = {}, aemLinkPath = "", fileName, localPath;

    try{
        app.consoleout('EAEM - START downloading High-Res renditions...');

        for(var i = 0; i < aemLinks.length; i++ ) {
            aemLinkPath = aemLinks[i];

            fileName = aemLinkPath.substring(aemLinkPath.lastIndexOf("/"));

            localPath = new File(sourceFolder.fullName + fileName);

            app.consoleout('EAEM - Downloading : ' + aemLinkPath + ", to : " + localPath.fsName);

            fetchResource (host,  credentials, aemLinks[i], localPath);

            app.consoleout('EAEM - Download complete : ' + aemLinkPath + ", to : " + localPath.fsName);

            aemToLocalMap[aemLinkPath] = localPath;
        }

        app.consoleout('EAEM - END downloading High-Res Renditions...');
    }catch(err){
        app.consoleout('EAEM - ERROR downloading : ' + aemLinkPath);
    }

    return aemToLocalMap;
}

function eaemReplaceLowResWithHighRes(document){
    var links = document.links, aemToLocalMap, link, filePath, highResPath;

    aemToLocalMap = eaemDownloadAndGetHighResMap(eaemGetAllLinks(document));

    try{
        app.consoleout('EAEM - START relinking with High-Res renditions...');

        for(var l = 0; l < links.length; l++ ) {
            link = links[l];

            filePath = eaemNormalizePath(link.filePath);

            filePath = eaemAddDAMRootToPath(filePath.substring(filePath.indexOf("/")));

            highResPath = aemToLocalMap[filePath];

            app.consoleout('EAEM - RELINKING High-Res : ' + highResPath);

            link.relink(highResPath);

            link.update();
        }

        app.consoleout('EAEM - END relinking with High-Res renditions...');
    }catch(err){
        app.consoleout('EAEM - ERROR relinking : ' + highResPath);
    }
}

function eaemNormalizePath(path){
    if(!path){
        return path;
    }

    path = path.replace(/\\/g, "/");

    if(path.indexOf(":/") == 1){
        //windows paths are like X:/content/dam/wip/myfile.jpg
    }else{
        path = path.replace(/:/g, "/");

        if(path.indexOf("/") > 0){
            path = "/" + path;
        }
    }

    return path;
}

function eaemAddDAMRootToPath(path){
    var EAEM_CONTENT_DAM_ROOT = "/content/dam",
        MAC_CONTENT_DAM_ROOT = "/DAM",
        MAC_CONTENT_DAM_VOLUME_ROOT = "/Volumes/DAM";

    if(!path){
        return path;
    }

    if(path.indexOf(MAC_CONTENT_DAM_ROOT) == 0){
        path = EAEM_CONTENT_DAM_ROOT + path.substring(MAC_CONTENT_DAM_ROOT.length);
    }else if(path.indexOf(MAC_CONTENT_DAM_VOLUME_ROOT) == 0){
        path = EAEM_CONTENT_DAM_ROOT + path.substring(MAC_CONTENT_DAM_VOLUME_ROOT.length);
    }else if(path.indexOf(EAEM_CONTENT_DAM_ROOT) != 0){
        path = EAEM_CONTENT_DAM_ROOT + path;
    }

    return path;
}

function eaemGetAllLinks(document){
    var linkAEMPaths = [],
        links = document.links, link, filePath;

    for(var i = 0; i < links.length; i++ ) {
        link = links[i];

        filePath = eaemNormalizePath(link.filePath);

        filePath = eaemAddDAMRootToPath(filePath.substring(filePath.indexOf("/")));

        linkAEMPaths.push(filePath);
    }

    return linkAEMPaths;
}

function eaemExportThumbnailUsingIDSIndd(document){
    app.consoleout('EAEM - Generating thumbnail renditions...');

    var tnFolder = new Folder(exportFolder.fullName + "/thumbnail"), thumbnail;

    tnFolder.create();

    with (app.jpegExportPreferences) {
        exportResolution = 300;
        jpegColorSpace = JpegColorSpaceEnum.RGB;
        jpegQuality = JPEGOptionsQuality.MAXIMUM;
        jpegRenderingStyle = JPEGOptionsFormat.PROGRESSIVE_ENCODING;
        viewDocumentAfterExport = false;
        pageString = document.pages.item(0).name;
        jpegExportRange = ExportRangeOrAllPages.EXPORT_RANGE;
    }

    thumbnail = new File(tnFolder.fullName + '/cq5dam.web.1280.1280.jpeg');

    document.exportFile(ExportFormat.JPG, thumbnail);

    app.consoleout('EAEM - Posting file cq5dam.web.1280.1280.jpeg to location: ' + target + '/jcr:content/renditions');

    putResource(host, credentials, thumbnail, 'cq5dam.web.1280.1280.jpeg', 'image/jpeg', target);

    app.jpegExportPreferences.exportResolution = 72;

    app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.LOW;

    thumbnail = new File(tnFolder.fullName + '/thumbnail.jpg');

    document.exportFile(ExportFormat.JPG, thumbnail);

    app.consoleout('EAEM - Posting file thumbnail.jpg to location: ' + target + '/jcr:content/renditions');

    putResource(host, credentials, thumbnail, 'thumbnail.jpg', 'image/jpeg', target);
}

No comments:

Post a Comment