001/*
002 * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     ldoguin
018 */
019package org.nuxeo.template.deckjs;
020
021import java.io.File;
022import java.io.FileWriter;
023import java.io.IOException;
024
025import org.apache.commons.io.FileUtils;
026import org.apache.commons.io.IOUtils;
027
028import org.nuxeo.common.Environment;
029import org.nuxeo.ecm.automation.OperationContext;
030import org.nuxeo.ecm.automation.core.Constants;
031import org.nuxeo.ecm.automation.core.annotations.Context;
032import org.nuxeo.ecm.automation.core.annotations.Operation;
033import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
034import org.nuxeo.ecm.core.api.Blob;
035import org.nuxeo.ecm.core.api.Blobs;
036import org.nuxeo.ecm.core.api.DocumentModel;
037import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
038import org.nuxeo.ecm.core.convert.api.ConversionService;
039import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder;
040import org.nuxeo.template.jaxrs.context.JAXRSExtensions;
041
042@Operation(id = DeckJSPDFOperation.ID, category = Constants.CAT_CONVERSION, label = "Convert a deckJS slide to a pdf", description = "Convert a deckJS slide to a pdf.")
043public class DeckJSPDFOperation {
044
045    public static final String ID = "Blob.DeckJSToPDF";
046
047    @Context
048    OperationContext ctx;
049
050    @Context
051    ConversionService conversionService;
052
053    @OperationMethod
054    public Blob run(Blob blob) throws IOException {
055        DocumentModel templateSourceDocument = (DocumentModel) ctx.get("templateSourceDocument");
056        DocumentModel templateBasedDocument = (DocumentModel) ctx.get("templateBasedDocument");
057        String templateName = (String) ctx.get("templateName");
058
059        File workingDir = new File(Environment.getDefault().getTemp(), "nuxeo-deckJS-cache/"
060                + templateBasedDocument.getId());
061        workingDir.mkdirs();
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}