001/*
002 * (C) Copyright 2006-2016 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 *     Nuxeo - initial API and implementation
018 */
019package org.nuxeo.template.odt;
020
021import java.io.File;
022import java.io.FileInputStream;
023import java.io.FileOutputStream;
024import java.io.IOException;
025import java.io.InputStream;
026import java.nio.charset.StandardCharsets;
027import java.util.List;
028import java.util.zip.CRC32;
029import java.util.zip.ZipEntry;
030import java.util.zip.ZipOutputStream;
031
032import org.apache.commons.io.FileUtils;
033import org.apache.commons.io.IOUtils;
034import org.nuxeo.common.utils.ZipUtils;
035import org.nuxeo.ecm.core.api.Blob;
036
037/**
038 * Helper used to modify a ODT/Zip archive for addition Pictures (and potentially fragments)
039 *
040 * @author Tiry (tdelprat@nuxeo.com)
041 */
042public class OOoArchiveModifier {
043
044    public File updateArchive(File workingDir, File oooFile, List<Blob> blobs) throws IOException {
045        if (blobs == null || blobs.size() == 0) {
046            return oooFile;
047        }
048
049        File unzipDir = new File(workingDir, "unzip-" + oooFile.getName());
050        unzipDir.mkdirs();
051
052        ZipUtils.unzip(oooFile, unzipDir);
053
054        File pictureDirs = new File(unzipDir, "Pictures");
055        if (!pictureDirs.exists()) {
056            pictureDirs.mkdir();
057            pictureDirs = new File(unzipDir, "Pictures");
058        }
059
060        File contentDirs = new File(unzipDir, "Content");
061        if (!contentDirs.exists()) {
062            contentDirs.mkdir();
063            contentDirs = new File(unzipDir, "Content");
064        }
065
066        StringBuilder blobsManifest = new StringBuilder();
067        for (Blob blob : blobs) {
068            try (InputStream in = blob.getStream()) {
069                File parentDir = contentDirs;
070                if (blob.getMimeType().startsWith("image")) {
071                    parentDir = pictureDirs;
072                }
073                FileUtils.copyInputStreamToFile(in, new File(parentDir, blob.getFilename()));
074            }
075
076            blobsManifest.append("<manifest:file-entry manifest:media-type=\"");
077            blobsManifest.append(blob.getMimeType());
078            if (blob.getMimeType().startsWith("image")) {
079                blobsManifest.append("\" manifest:full-path=\"Pictures/");
080            } else {
081                blobsManifest.append("\" manifest:full-path=\"Content/");
082            }
083            blobsManifest.append(blob.getFilename());
084            blobsManifest.append("\"/>\n");
085        }
086
087        File xmlManifestFile = new File(unzipDir.getPath() + "/META-INF/manifest.xml");
088        String xmlManifest = FileUtils.readFileToString(xmlManifestFile, StandardCharsets.UTF_8);
089        int idx = xmlManifest.indexOf("</manifest:manifest>");
090        xmlManifest = xmlManifest.substring(0, idx) + blobsManifest.toString() + xmlManifest.substring(idx);
091        FileUtils.writeByteArrayToFile(xmlManifestFile, xmlManifest.getBytes());
092
093        String path = oooFile.getAbsolutePath();
094
095        oooFile.delete();
096
097        oooFile = new File(path);
098        oooFile.createNewFile();
099
100        // ZipUtils.zip(unzipDir.listFiles(), oooFile);
101        mkOOoZip(unzipDir, oooFile);
102
103        org.apache.commons.io.FileUtils.deleteDirectory(unzipDir);
104        unzipDir.delete();
105
106        return oooFile;
107
108    }
109
110    protected void mkOOoZip(File directory, File outFile) throws IOException {
111        try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(outFile))) {
112            File manif = new File(directory, "mimetype");
113            writeOOoEntry(zipOutputStream, manif.getName(), manif, ZipEntry.STORED);
114
115            for (File fileEntry : directory.listFiles()) {
116                if (!fileEntry.getName().equals(manif.getName())) {
117                    writeOOoEntry(zipOutputStream, fileEntry.getName(), fileEntry, ZipEntry.DEFLATED);
118                }
119            }
120        }
121    }
122
123    protected void writeOOoEntry(ZipOutputStream zipOutputStream, String entryName, File fileEntry, int zipMethod)
124            throws IOException {
125
126        if (fileEntry.isDirectory()) {
127            entryName = entryName + "/";
128            ZipEntry zentry = new ZipEntry(entryName);
129            zipOutputStream.putNextEntry(zentry);
130            zipOutputStream.closeEntry();
131            for (File child : fileEntry.listFiles()) {
132                writeOOoEntry(zipOutputStream, entryName + child.getName(), child, zipMethod);
133            }
134            return;
135        }
136
137        ZipEntry zipEntry = new ZipEntry(entryName);
138        try (InputStream entryInputStream = new FileInputStream(fileEntry)) {
139            zipEntry.setMethod(zipMethod);
140            if (zipMethod == ZipEntry.STORED) {
141                byte[] inputBytes = IOUtils.toByteArray(entryInputStream);
142                CRC32 crc = new CRC32();
143                crc.update(inputBytes);
144                zipEntry.setCrc(crc.getValue());
145                zipEntry.setSize(inputBytes.length);
146                zipEntry.setCompressedSize(inputBytes.length);
147                zipOutputStream.putNextEntry(zipEntry);
148                IOUtils.write(inputBytes, zipOutputStream);
149            } else {
150                zipOutputStream.putNextEntry(zipEntry);
151                IOUtils.copy(entryInputStream, zipOutputStream);
152            }
153        }
154        zipOutputStream.closeEntry();
155    }
156
157}