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.apache.commons.io.FilenameUtils;
029import org.nuxeo.common.utils.FileUtils;
030import org.nuxeo.ecm.core.api.Blob;
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.PathRef;
034import org.nuxeo.ecm.core.api.pathsegment.PathSegmentService;
035import org.nuxeo.ecm.platform.filemanager.api.FileImporterContext;
036import org.nuxeo.ecm.platform.filemanager.service.extension.AbstractFileImporter;
037import org.nuxeo.ecm.platform.filemanager.utils.FileManagerUtils;
038import org.nuxeo.runtime.api.Framework;
039
040public class ThreeDImporter extends AbstractFileImporter {
041    private static final long serialVersionUID = 1L;
042    public static final String MIMETYPE_ZIP = "application/zip";
043
044    @Override
045    public DocumentModel createOrUpdate(FileImporterContext context) throws IOException {
046        CoreSession session = context.getSession();
047        Blob blob = context.getBlob();
048        boolean isThreeD = SUPPORTED_EXTENSIONS.contains(FileUtils.getFileExtension(blob.getFilename()));
049        boolean isZipThreeD = MIMETYPE_ZIP.equals(blob.getMimeType());
050        String title = null;
051        if (isZipThreeD) {
052            title = getModelFilename(blob);
053            isZipThreeD = title != null;
054        }
055
056        if (!(isThreeD || isZipThreeD)) {
057            return null;
058        }
059
060        String path = context.getParentPath();
061        DocumentModel container = session.getDocument(new PathRef(path));
062        String docType = getDocType(container);
063        if (docType == null) {
064            docType = getDefaultDocType();
065        }
066        if (isThreeD) {
067            title = FileManagerUtils.fetchTitle(blob.getFilename());
068        }
069        DocumentModel doc = session.createDocumentModel(docType);
070        doc.setPropertyValue("dc:title", title);
071        PathSegmentService pss = Framework.getService(PathSegmentService.class);
072        doc.setPathInfo(path, pss.generatePathSegment(doc));
073        updateDocument(doc, blob);
074        doc = session.createDocument(doc);
075        session.save();
076        return doc;
077    }
078
079    protected String getModelFilename(final Blob zipContent) throws IOException {
080        /* Extract ZIP contents */
081        ZipEntry zipEntry;
082        String threeDFilename = null;
083        try (ZipInputStream zipInputStream = new ZipInputStream(zipContent.getStream())) {
084            while ((zipEntry = zipInputStream.getNextEntry()) != null) {
085                // skip if the entry is a directory, if it's not a supported extension or if it's hidden (by convention)
086                String ext = FilenameUtils.getExtension(zipEntry.getName()).toLowerCase();
087                boolean isSupported = SUPPORTED_EXTENSIONS.contains(ext);
088                if (zipEntry.isDirectory() || !isSupported || zipEntry.getName().startsWith(".")) {
089                    continue;
090                }
091                threeDFilename = FileManagerUtils.fetchTitle(zipEntry.getName());
092                break;
093            }
094        }
095        return threeDFilename;
096    }
097
098    @Override
099    public String getDefaultDocType() {
100        return THREED_TYPE;
101    }
102
103    @Override
104    public boolean isOverwriteByTitle() {
105        return true;
106    }
107
108}