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