001/*
002 * (C) Copyright 2002-2011 Nuxeo SARL <http://nuxeo.com> and others
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 */
013
014package org.nuxeo.ecm.platform.filemanager.service.extension;
015
016import java.io.File;
017import java.io.IOException;
018import java.util.zip.ZipEntry;
019import java.util.zip.ZipException;
020import java.util.zip.ZipFile;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.common.utils.Path;
025import org.nuxeo.ecm.core.api.Blob;
026import org.nuxeo.ecm.core.api.CloseableFile;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.DocumentRef;
030import org.nuxeo.ecm.core.api.IdRef;
031import org.nuxeo.ecm.core.api.PathRef;
032import org.nuxeo.ecm.core.io.DocumentPipe;
033import org.nuxeo.ecm.core.io.DocumentReader;
034import org.nuxeo.ecm.core.io.DocumentWriter;
035import org.nuxeo.ecm.core.io.ExportedDocument;
036import org.nuxeo.ecm.core.io.impl.DocumentPipeImpl;
037import org.nuxeo.ecm.core.io.impl.plugins.DocumentModelWriter;
038import org.nuxeo.ecm.core.io.impl.plugins.NuxeoArchiveReader;
039import org.nuxeo.ecm.platform.types.TypeManager;
040
041/**
042 * Simple Plugin that imports IO Zip archive into Nuxeo using the IO core service.
043 *
044 * @author tiry
045 */
046public class ExportedZipImporter extends AbstractFileImporter {
047
048    private static final long serialVersionUID = 1876876876L;
049
050    private static final Log log = LogFactory.getLog(ExportedZipImporter.class);
051
052    public static ZipFile getArchiveFileIfValid(File file) throws IOException {
053        ZipFile zip;
054
055        try {
056            zip = new ZipFile(file);
057        } catch (ZipException e) {
058            log.debug("file is not a zipfile ! ", e);
059            return null;
060        } catch (IOException e) {
061            log.debug("can not open zipfile ! ", e);
062            return null;
063        }
064
065        ZipEntry marker = zip.getEntry(".nuxeo-archive");
066
067        if (marker == null) {
068            zip.close();
069            return null;
070        } else {
071            return zip;
072        }
073    }
074
075    public DocumentModel create(CoreSession documentManager, Blob content, String path, boolean overwrite,
076            String filename, TypeManager typeService) throws IOException {
077        try (CloseableFile source = content.getCloseableFile()) {
078            ZipFile zip = getArchiveFileIfValid(source.getFile());
079            if (zip == null) {
080                return null;
081            }
082            zip.close();
083
084            boolean importWithIds = false;
085            DocumentReader reader = new NuxeoArchiveReader(source.getFile());
086            ExportedDocument root = reader.read();
087            IdRef rootRef = new IdRef(root.getId());
088
089            if (documentManager.exists(rootRef)) {
090                DocumentModel target = documentManager.getDocument(rootRef);
091                if (target.getPath().removeLastSegments(1).equals(new Path(path))) {
092                    importWithIds = true;
093                }
094            }
095
096            DocumentWriter writer = new DocumentModelWriter(documentManager, path, 10);
097            reader.close();
098            reader = new NuxeoArchiveReader(source.getFile());
099
100            DocumentRef resultingRef;
101            if (overwrite && importWithIds) {
102                resultingRef = rootRef;
103            } else {
104                String rootName = root.getPath().lastSegment();
105                resultingRef = new PathRef(path, rootName);
106            }
107
108            try {
109                DocumentPipe pipe = new DocumentPipeImpl(10);
110                pipe.setReader(reader);
111                pipe.setWriter(writer);
112                pipe.run();
113            } catch (IOException e) {
114                log.warn(e, e);
115            } finally {
116                reader.close();
117                writer.close();
118            }
119            return documentManager.getDocument(resultingRef);
120        }
121    }
122}