001/* 002 * (C) Copyright 2006-2013 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 * Vladimir Pasquier <vpasquier@nuxeo.com> 018 * Laurent Doguin <ldoguin@nuxeo.com> 019 * Nelson Silva <nsilva@nuxeo.com> 020 */ 021package org.nuxeo.ecm.platform.thumbnail.listener; 022 023import static org.nuxeo.ecm.core.api.CoreSession.ALLOW_VERSION_WRITE; 024 025import java.io.IOException; 026import java.io.InputStream; 027import java.io.Serializable; 028import java.util.HashSet; 029import java.util.Set; 030 031import org.apache.logging.log4j.LogManager; 032import org.apache.logging.log4j.Logger; 033import org.nuxeo.ecm.core.api.Blob; 034import org.nuxeo.ecm.core.api.Blobs; 035import org.nuxeo.ecm.core.api.CoreSession; 036import org.nuxeo.ecm.core.api.DocumentModel; 037import org.nuxeo.ecm.core.api.NuxeoException; 038import org.nuxeo.ecm.core.api.VersioningOption; 039import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 040import org.nuxeo.ecm.core.api.thumbnail.ThumbnailAdapter; 041import org.nuxeo.ecm.core.api.versioning.VersioningService; 042import org.nuxeo.ecm.core.blob.BlobManager; 043import org.nuxeo.ecm.core.event.DeletedDocumentModel; 044import org.nuxeo.ecm.core.event.Event; 045import org.nuxeo.ecm.core.event.EventBundle; 046import org.nuxeo.ecm.core.event.PostCommitEventListener; 047import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 048import org.nuxeo.ecm.platform.dublincore.listener.DublinCoreListener; 049import org.nuxeo.ecm.platform.ec.notification.NotificationConstants; 050import org.nuxeo.ecm.platform.thumbnail.ThumbnailConstants; 051import org.nuxeo.runtime.api.Framework; 052 053/** 054 * Thumbnail listener handling creation and update of thumbnail preview on main blob update, for documents holding the 055 * schema "file". 056 * 057 * @since 5.7 058 */ 059public class UpdateThumbnailListener implements PostCommitEventListener { 060 061 /** @since 11.1 **/ 062 private static final Logger log = LogManager.getLogger(UpdateThumbnailListener.class); 063 064 public static final String THUMBNAIL_UPDATED = "thumbnailUpdated"; 065 066 protected void processDoc(CoreSession session, DocumentModel doc) { 067 Blob thumbnailBlob = getManagedThumbnail(doc); 068 if (thumbnailBlob == null) { 069 ThumbnailAdapter thumbnailAdapter = doc.getAdapter(ThumbnailAdapter.class); 070 if (thumbnailAdapter == null) { 071 return; 072 } 073 thumbnailBlob = thumbnailAdapter.computeThumbnail(session); 074 } 075 if (thumbnailBlob != null) { 076 if (!doc.hasFacet(ThumbnailConstants.THUMBNAIL_FACET)) { 077 doc.addFacet(ThumbnailConstants.THUMBNAIL_FACET); 078 } 079 doc.setPropertyValue(ThumbnailConstants.THUMBNAIL_PROPERTY_NAME, (Serializable) thumbnailBlob); 080 } else { 081 if (doc.hasFacet(ThumbnailConstants.THUMBNAIL_FACET)) { 082 doc.setPropertyValue(ThumbnailConstants.THUMBNAIL_PROPERTY_NAME, null); 083 doc.removeFacet(ThumbnailConstants.THUMBNAIL_FACET); 084 } 085 } 086 if (doc.isDirty()) { 087 doc.putContextData(VersioningService.VERSIONING_OPTION, VersioningOption.NONE); 088 doc.putContextData(VersioningService.DISABLE_AUTO_CHECKOUT, Boolean.TRUE); 089 doc.putContextData(DublinCoreListener.DISABLE_DUBLINCORE_LISTENER, Boolean.TRUE); 090 doc.putContextData(NotificationConstants.DISABLE_NOTIFICATION_SERVICE, Boolean.TRUE); 091 doc.putContextData("disableAuditLogger", Boolean.TRUE); 092 if (doc.isVersion()) { 093 doc.putContextData(ALLOW_VERSION_WRITE, Boolean.TRUE); 094 } 095 doc.putContextData(THUMBNAIL_UPDATED, true); 096 session.saveDocument(doc); 097 } 098 } 099 100 private Blob getManagedThumbnail(DocumentModel doc) { 101 BlobHolder bh = doc.getAdapter(BlobHolder.class); 102 if (bh == null) { 103 return null; 104 } 105 Blob blob = bh.getBlob(); 106 if (blob == null) { 107 return null; 108 } 109 BlobManager blobManager = Framework.getService(BlobManager.class); 110 try (InputStream is = blobManager.getThumbnail(blob)) { 111 if (is == null) { 112 return null; 113 } 114 return Blobs.createBlob(is); 115 } catch (IOException e) { 116 throw new NuxeoException("Failed to get managed blob thumbnail", e); 117 } 118 } 119 120 @Override 121 public void handleEvent(EventBundle events) { 122 if (!events.containsEventName(ThumbnailConstants.EventNames.scheduleThumbnailUpdate.name())) { 123 return; 124 } 125 Set<String> processedDocs = new HashSet<>(); 126 for (Event event : events) { 127 if (!ThumbnailConstants.EventNames.scheduleThumbnailUpdate.name().equals(event.getName())) { 128 continue; 129 } 130 DocumentEventContext context = (DocumentEventContext) event.getContext(); 131 DocumentModel doc = context.getSourceDocument(); 132 if (Boolean.TRUE.equals(context.getProperty(ThumbnailConstants.DISABLE_THUMBNAIL_COMPUTATION))) { 133 log.trace("Thumbnail computation is disabled for document {}", doc::getId); 134 continue; 135 } 136 137 if (doc instanceof DeletedDocumentModel) { 138 continue; 139 } 140 if (doc.isProxy()) { 141 continue; 142 } 143 if (processedDocs.contains(doc.getId())) { 144 continue; 145 } 146 CoreSession repo = context.getCoreSession(); 147 processDoc(repo, doc); 148 processedDocs.add(doc.getId()); 149 } 150 } 151}