001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo 016 */ 017 018package org.nuxeo.ecm.showcase.content; 019 020import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_IMPORTED; 021import static org.nuxeo.ecm.platform.video.VideoConstants.HAS_VIDEO_PREVIEW_FACET; 022import static org.nuxeo.ecm.platform.video.VideoConstants.VIDEO_CHANGED_EVENT; 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.DocumentModel; 028import org.nuxeo.ecm.core.api.NuxeoException; 029import org.nuxeo.ecm.core.api.model.Property; 030import org.nuxeo.ecm.core.event.Event; 031import org.nuxeo.ecm.core.event.EventBundle; 032import org.nuxeo.ecm.core.event.EventService; 033import org.nuxeo.ecm.core.event.PostCommitEventListener; 034import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 035import org.nuxeo.ecm.platform.video.VideoHelper; 036import org.nuxeo.runtime.api.Framework; 037 038/** 039 * Override default org.nuxeo.ecm.platform.video.listener.VideoChangedListener listener as an async/postcommit one to 040 * ensure the Blob is present in the DocumentModel, because is not on the synchronised documentImported event. 041 * 042 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a> 043 * @since 7.10 044 */ 045public class ShowcaseVideoImportedListener implements PostCommitEventListener { 046 047 private static final Log log = LogFactory.getLog(ShowcaseVideoImportedListener.class); 048 049 @Override 050 public void handleEvent(EventBundle eventBundle) { 051 if (eventBundle.containsEventName(DOCUMENT_IMPORTED)) { 052 eventBundle.forEach(this::handle); 053 } 054 } 055 056 protected void handle(Event event) { 057 if (!(event.getContext() instanceof DocumentEventContext)) { 058 return; 059 } 060 DocumentEventContext ctx = (DocumentEventContext) event.getContext(); 061 DocumentModel doc = ctx.getSourceDocument(); 062 if (doc.hasFacet(HAS_VIDEO_PREVIEW_FACET) && !doc.isProxy()) { 063 Property origVideoProperty = doc.getProperty("file:content"); 064 065 Blob video = (Blob) origVideoProperty.getValue(); 066 updateVideoInfo(doc, video); 067 068 // only trigger the event if we really have a video 069 if (video != null) { 070 Event trigger = ctx.newEvent(VIDEO_CHANGED_EVENT); 071 EventService eventService = Framework.getLocalService(EventService.class); 072 eventService.fireEvent(trigger); 073 } 074 } 075 } 076 077 protected void updateVideoInfo(DocumentModel doc, Blob video) { 078 try { 079 VideoHelper.updateVideoInfo(doc, video); 080 } catch (NuxeoException e) { 081 // may happen if ffmpeg is not installed 082 log.error(String.format("Unable to retrieve video info: %s", e.getMessage())); 083 log.debug(e, e); 084 } 085 } 086}