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 *     Miguel Nixo
018 */
019package org.nuxeo.ecm.platform.threed.importer;
020
021import static org.nuxeo.ecm.platform.threed.ThreeDConstants.SUPPORTED_EXTENSIONS;
022import static org.nuxeo.ecm.platform.threed.ThreeDConstants.THREED_TYPE;
023
024import java.io.IOException;
025import java.util.zip.ZipEntry;
026import java.util.zip.ZipInputStream;
027
028import org.nuxeo.common.utils.FileUtils;
029import org.nuxeo.ecm.core.api.Blob;
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.PathRef;
033import org.nuxeo.ecm.core.api.pathsegment.PathSegmentService;
034import org.nuxeo.ecm.platform.filemanager.service.extension.AbstractFileImporter;
035import org.nuxeo.ecm.platform.filemanager.utils.FileManagerUtils;
036import org.nuxeo.ecm.platform.types.TypeManager;
037import org.nuxeo.runtime.api.Framework;
038
039public class ThreeDImporter extends AbstractFileImporter {
040    public static final String MIMETYPE_ZIP = "application/zip";
041
042    public DocumentModel create(CoreSession session, Blob content, String path, boolean overwrite, String fullname,
043            TypeManager typeService) throws IOException {
044
045        boolean isThreeD = SUPPORTED_EXTENSIONS.contains(FileUtils.getFileExtension(content.getFilename()));
046        boolean isZipThreeD = MIMETYPE_ZIP.equals(content.getMimeType());
047        String title = null;
048        if (isZipThreeD) {
049            title = getModelFilename(content);
050            isZipThreeD = title != null;
051        }
052
053        if (!(isThreeD || isZipThreeD)) {
054            return null;
055        }
056        DocumentModel container = session.getDocument(new PathRef(path));
057        String docType = getDocType(container);
058        if (docType == null) {
059            docType = getDefaultDocType();
060        }
061        if (isThreeD) {
062            title = FileManagerUtils.fetchTitle(content.getFilename());
063        }
064        DocumentModel doc = session.createDocumentModel(docType);
065        doc.setPropertyValue("dc:title", title);
066        PathSegmentService pss = Framework.getService(PathSegmentService.class);
067        doc.setPathInfo(path, pss.generatePathSegment(doc));
068        updateDocument(doc, content);
069        doc = session.createDocument(doc);
070        session.save();
071        return doc;
072    }
073
074    protected String getModelFilename(final Blob zipContent) throws IOException {
075        /* Extract ZIP contents */
076        ZipEntry zipEntry;
077        ZipInputStream zipInputStream = null;
078        String threeDFilename = null;
079        try {
080            zipInputStream = new ZipInputStream(zipContent.getStream());
081            while ((zipEntry = zipInputStream.getNextEntry()) != null) {
082                // skip if the entry is a directory, if it's not a supported extension or if it's hidden (by convention)
083                boolean isSupported = SUPPORTED_EXTENSIONS.contains(FileUtils.getFileExtension(zipEntry.getName()));
084                if (zipEntry.isDirectory() || !isSupported || zipEntry.getName().startsWith(".")) {
085                    continue;
086                }
087                threeDFilename = FileManagerUtils.fetchTitle(zipEntry.getName());
088                break;
089            }
090        } finally {
091            try {
092                if (zipInputStream != null) {
093                    zipInputStream.close();
094                }
095            } catch (IOException e) {
096                e.printStackTrace();
097            }
098        }
099        return threeDFilename;
100    }
101
102    @Override
103    public String getDefaultDocType() {
104        return THREED_TYPE;
105    }
106
107    @Override
108    public boolean isOverwriteByTitle() {
109        return true;
110    }
111
112}