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; 024 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.nuxeo.ecm.core.api.Blob; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.core.api.NuxeoException; 030import org.nuxeo.ecm.core.api.model.Property; 031import org.nuxeo.ecm.core.event.Event; 032import org.nuxeo.ecm.core.event.EventBundle; 033import org.nuxeo.ecm.core.event.PostCommitEventListener; 034import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 035import org.nuxeo.ecm.core.work.api.WorkManager; 036import org.nuxeo.ecm.platform.video.VideoHelper; 037import org.nuxeo.ecm.platform.video.service.VideoInfoWork; 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 scheduleAsyncProcessing(doc); 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 087 protected void scheduleAsyncProcessing(DocumentModel doc) { 088 WorkManager workManager = Framework.getService(WorkManager.class); 089 VideoInfoWork work = new VideoInfoWork(doc.getRepositoryName(), doc.getId()); 090 log.debug(String.format("Scheduling work: video info of document %s.", doc)); 091 workManager.schedule(work, true); 092 } 093}