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