001/* 002 * (C) Copyright 2015 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 * Nuxeo 018 */ 019 020package org.nuxeo.ecm.showcase.content; 021 022import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_IMPORTED; 023import static org.nuxeo.ecm.platform.video.VideoConstants.HAS_VIDEO_PREVIEW_FACET; 024import static org.nuxeo.ecm.platform.video.VideoConstants.VIDEO_CHANGED_EVENT; 025 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.nuxeo.ecm.core.api.Blob; 029import org.nuxeo.ecm.core.api.DocumentModel; 030import org.nuxeo.ecm.core.api.NuxeoException; 031import org.nuxeo.ecm.core.api.model.Property; 032import org.nuxeo.ecm.core.event.Event; 033import org.nuxeo.ecm.core.event.EventBundle; 034import org.nuxeo.ecm.core.event.EventService; 035import org.nuxeo.ecm.core.event.PostCommitEventListener; 036import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 037import org.nuxeo.ecm.platform.video.VideoHelper; 038import org.nuxeo.runtime.api.Framework; 039 040/** 041 * Override default org.nuxeo.ecm.platform.video.listener.VideoChangedListener listener as an async/postcommit one to 042 * ensure the Blob is present in the DocumentModel, because is not on the synchronised documentImported event. 043 * 044 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a> 045 * @since 7.10 046 */ 047public class ShowcaseVideoImportedListener implements PostCommitEventListener { 048 049 private static final Log log = LogFactory.getLog(ShowcaseVideoImportedListener.class); 050 051 @Override 052 public void handleEvent(EventBundle eventBundle) { 053 if (eventBundle.containsEventName(DOCUMENT_IMPORTED)) { 054 eventBundle.forEach(this::handle); 055 } 056 } 057 058 protected void handle(Event event) { 059 if (!(event.getContext() instanceof DocumentEventContext)) { 060 return; 061 } 062 DocumentEventContext ctx = (DocumentEventContext) event.getContext(); 063 DocumentModel doc = ctx.getSourceDocument(); 064 if (doc.hasFacet(HAS_VIDEO_PREVIEW_FACET) && !doc.isProxy()) { 065 Property origVideoProperty = doc.getProperty("file:content"); 066 067 Blob video = (Blob) origVideoProperty.getValue(); 068 updateVideoInfo(doc, video); 069 070 // only trigger the event if we really have a video 071 if (video != null) { 072 Event trigger = ctx.newEvent(VIDEO_CHANGED_EVENT); 073 EventService eventService = Framework.getService(EventService.class); 074 eventService.fireEvent(trigger); 075 } 076 } 077 } 078 079 protected void updateVideoInfo(DocumentModel doc, Blob video) { 080 try { 081 VideoHelper.updateVideoInfo(doc, video); 082 } catch (NuxeoException e) { 083 // may happen if ffmpeg is not installed 084 log.error(String.format("Unable to retrieve video info: %s", e.getMessage())); 085 log.debug(e, e); 086 } 087 } 088}