001/* 002 * (C) Copyright 2019 Nuxeo (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 * Peter Di Lorenzo 018 */ 019package org.nuxeo.ecm.platform.audio.extension; 020 021import java.io.IOException; 022import java.io.Serializable; 023 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026import org.nuxeo.ecm.core.api.Blob; 027import org.nuxeo.ecm.core.api.CoreSession; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.core.api.pathsegment.PathSegmentService; 030import org.nuxeo.ecm.platform.filemanager.api.FileImporterContext; 031import org.nuxeo.ecm.platform.filemanager.service.extension.AbstractFileImporter; 032import org.nuxeo.ecm.platform.filemanager.utils.FileManagerUtils; 033import org.nuxeo.ecm.platform.types.Type; 034import org.nuxeo.ecm.platform.types.TypeManager; 035import org.nuxeo.runtime.api.Framework; 036 037/** 038 * This class will create a Document of type "Audio" from the uploaded file, if the uploaded file matches any of the 039 * mime types listed in the filemanager-plugins.xml file. 040 * <p> 041 * If an existing document with the same title is found, it will overwrite it and increment the version number if the 042 * overwrite flag is set to true; Otherwise, it will generate a new title and create a new Document of type Audio with 043 * that title. 044 */ 045public class AudioImporter extends AbstractFileImporter { 046 047 private static final long serialVersionUID = 1L; 048 049 @SuppressWarnings("unused") 050 private static final Log log = LogFactory.getLog(AudioImporter.class); 051 052 public static final String AUDIO_TYPE = "Audio"; 053 054 @Override 055 public DocumentModel createOrUpdate(FileImporterContext context) throws IOException { 056 CoreSession session = context.getSession(); 057 Blob blob = context.getBlob(); 058 059 String filename = FileManagerUtils.fetchFileName(context.getFileName()); 060 blob.setFilename(filename); 061 062 String title = FileManagerUtils.fetchTitle(filename); 063 064 // Check to see if an existing Document with the same title exists. 065 String parentPath = context.getParentPath(); 066 DocumentModel docModel = FileManagerUtils.getExistingDocByTitle(session, parentPath, title); 067 068 // if overwrite flag is true and the file already exists, overwrite it 069 if (context.isOverwrite() && (docModel != null)) { 070 071 // update known attributes, format is: schema, attribute, value 072 docModel.setPropertyValue("file:content", (Serializable) blob); 073 docModel.setPropertyValue("dc:title", title); 074 075 // now save the uploaded file as another new version 076 checkIn(docModel); 077 docModel = session.saveDocument(docModel); 078 079 } else { 080 PathSegmentService pss = Framework.getService(PathSegmentService.class); 081 082 String docType = getDocType(); 083 if (docType == null) { 084 docType = AUDIO_TYPE; 085 } 086 087 docModel = session.createDocumentModel(docType); 088 // update known attributes, format is: schema, attribute, value 089 docModel.setPropertyValue("file:content", (Serializable) blob); 090 docModel.setPropertyValue("dc:title", title); 091 092 // updating icon 093 Type type = Framework.getService(TypeManager.class).getType(docType); 094 if (type != null) { 095 String iconPath = type.getIcon(); 096 docModel.setProperty("common", "icon", iconPath); 097 } 098 docModel.setPathInfo(parentPath, pss.generatePathSegment(docModel)); 099 docModel = session.createDocument(docModel); 100 } 101 return docModel; 102 } 103 104}