001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     ldoguin
016 */
017package org.nuxeo.template.deckjs;
018
019import java.io.File;
020import java.io.FileWriter;
021import java.io.IOException;
022
023import org.apache.commons.io.FileUtils;
024import org.apache.commons.io.IOUtils;
025import org.nuxeo.ecm.automation.OperationContext;
026import org.nuxeo.ecm.automation.core.Constants;
027import org.nuxeo.ecm.automation.core.annotations.Context;
028import org.nuxeo.ecm.automation.core.annotations.Operation;
029import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
030import org.nuxeo.ecm.core.api.Blob;
031import org.nuxeo.ecm.core.api.Blobs;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
034import org.nuxeo.ecm.core.api.impl.blob.FileBlob;
035import org.nuxeo.ecm.core.convert.api.ConversionService;
036import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder;
037import org.nuxeo.template.jaxrs.context.JAXRSExtensions;
038
039@Operation(id = DeckJSPDFOperation.ID, category = Constants.CAT_CONVERSION, label = "Convert a deckJS slide to a pdf", description = "Convert a deckJS slide to a pdf.")
040public class DeckJSPDFOperation {
041
042    public static final String ID = "Blob.DeckJSToPDF";
043
044    @Context
045    OperationContext ctx;
046
047    @Context
048    ConversionService conversionService;
049
050    @OperationMethod
051    public Blob run(Blob blob) throws IOException {
052        DocumentModel templateSourceDocument = (DocumentModel) ctx.get("templateSourceDocument");
053        DocumentModel templateBasedDocument = (DocumentModel) ctx.get("templateBasedDocument");
054        String templateName = (String) ctx.get("templateName");
055
056        String workingDirPath = System.getProperty("java.io.tmpdir") + "/nuxeo-deckJS-cache/"
057                + templateBasedDocument.getId();
058        File workingDir = new File(workingDirPath);
059        if (!workingDir.exists()) {
060            workingDir.mkdirs();
061        }
062        JAXRSExtensions jaxRsExtensions = new JAXRSExtensions(templateBasedDocument, null, templateName);
063        BlobHolder sourceBh = templateSourceDocument.getAdapter(BlobHolder.class);
064        for (Blob b : sourceBh.getBlobs()) {
065            writeToTempDirectory(workingDir, b);
066        }
067        BlobHolder templatebasedBh = templateBasedDocument.getAdapter(BlobHolder.class);
068        for (Blob b : templatebasedBh.getBlobs()) {
069            writeToTempDirectory(workingDir, b);
070        }
071
072        String content = blob.getString();
073        String resourcePath = jaxRsExtensions.getResourceUrl("");
074        content = content.replaceAll(resourcePath, "./");
075        File index = new File(workingDir, blob.getFilename());
076        FileWriter fw = new FileWriter(index);
077        IOUtils.write(content, fw);
078        fw.flush();
079        fw.close();
080
081        Blob indexBlob = Blobs.createBlob(index);
082        indexBlob.setFilename(blob.getFilename());
083        BlobHolder bh = conversionService.convert("deckJSToPDF", new SimpleCachableBlobHolder(indexBlob), null);
084        FileUtils.deleteDirectory(workingDir);
085        return bh.getBlob();
086    }
087
088    private void writeToTempDirectory(File workingDir, Blob b) throws IOException {
089        File f = new File(workingDir, b.getFilename());
090        File parentFile = f.getParentFile();
091        parentFile.mkdirs();
092        b.transferTo(f);
093    }
094}