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 * Nelson Silva 016 */ 017 018package org.nuxeo.ecm.media.publishing.upload; 019 020import org.apache.commons.logging.Log; 021import org.apache.commons.logging.LogFactory; 022import org.nuxeo.ecm.core.api.CoreSession; 023import org.nuxeo.ecm.core.api.DocumentModel; 024import org.nuxeo.ecm.core.api.IdRef; 025import org.nuxeo.ecm.core.api.NuxeoException; 026import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner; 027import org.nuxeo.ecm.core.api.VersioningOption; 028import org.nuxeo.ecm.core.api.event.DocumentEventCategories; 029import org.nuxeo.ecm.core.api.event.DocumentEventTypes; 030import org.nuxeo.ecm.core.event.Event; 031import org.nuxeo.ecm.core.event.EventProducer; 032import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 033import org.nuxeo.ecm.core.versioning.VersioningService; 034import org.nuxeo.ecm.core.work.AbstractWork; 035import org.nuxeo.ecm.media.publishing.MediaPublishingConstants; 036import org.nuxeo.ecm.media.publishing.MediaPublishingProvider; 037import org.nuxeo.ecm.media.publishing.adapter.PublishableMedia; 038import org.nuxeo.runtime.api.Framework; 039 040import java.io.IOException; 041import java.util.HashMap; 042import java.util.Map; 043 044/** 045 * Work for asynchronous media upload. 046 * 047 * @since 7.3 048 */ 049public class MediaPublishingUploadWork extends AbstractWork { 050 public static final String CATEGORY_VIDEO_UPLOAD = "mediaPublishingUpload"; 051 052 private final String serviceId; 053 private final MediaPublishingProvider service; 054 private CoreSession loginSession; 055 private String account; 056 private Map<String, String> options; 057 058 private static final Log log = LogFactory.getLog(MediaPublishingUploadWork.class); 059 060 public MediaPublishingUploadWork(String serviceId, MediaPublishingProvider service, String repositoryName, 061 String docId, CoreSession loginSession, String account, Map<String, String> options) { 062 super(getIdFor(repositoryName, docId, serviceId)); 063 this.serviceId = serviceId; 064 this.service = service; 065 this.loginSession = loginSession; 066 this.account = account; 067 this.options = options; 068 setDocument(repositoryName, docId); 069 } 070 071 public static String getIdFor(String repositoryName, String docId, String provider) { 072 return "media_" + provider + "_upload_" + repositoryName + "_" + docId; 073 } 074 075 @Override 076 public String getCategory() { 077 return CATEGORY_VIDEO_UPLOAD; 078 } 079 080 @Override 081 public String getTitle() { 082 return "Video Upload: " + docId; 083 } 084 085 @Override 086 public void work() { 087 final IdRef idRef = new IdRef(docId); 088 new UnrestrictedSessionRunner(repositoryName) { 089 @Override 090 public void run() { 091 final DocumentModel doc = session.getDocument(idRef); 092 PublishableMedia media = doc.getAdapter(PublishableMedia.class); 093 094 MediaPublishingProgressListener listener = new MediaPublishingProgressListener() { 095 @Override 096 public void onStart() { 097 setProgress(Progress.PROGRESS_0_PC); 098 } 099 100 @Override 101 public void onProgress(double progress) { 102 setProgress(new Progress(new Float(progress))); 103 } 104 105 @Override 106 public void onComplete() { 107 setProgress(Progress.PROGRESS_100_PC); 108 } 109 110 @Override 111 public void onError() { 112 setStatus("Error"); 113 } 114 }; 115 try { 116 String mediaId = service.upload(media, listener, account, options); 117 Map<String, Object> entry = new HashMap<>(); 118 entry.put(MediaPublishingConstants.ID_PROPERTY_NAME, mediaId); 119 entry.put(MediaPublishingConstants.PROVIDER_PROPERTY_NAME, serviceId); 120 entry.put(MediaPublishingConstants.ACCOUNT_PROPERTY_NAME, account); 121 media.putProvider(entry); 122 123 // We don't want to erase the current version 124 doc.putContextData(VersioningService.VERSIONING_OPTION, VersioningOption.NONE); 125 doc.putContextData(VersioningService.DISABLE_AUTO_CHECKOUT, Boolean.TRUE); 126 127 // Track media publication in document history 128 DocumentEventContext ctx = new DocumentEventContext(loginSession, loginSession.getPrincipal(), doc); 129 ctx.setComment("Published to " + serviceId); 130 ctx.setCategory(DocumentEventCategories.EVENT_DOCUMENT_CATEGORY); 131 132 EventProducer evtProducer = Framework.getService(EventProducer.class); 133 Event event = ctx.newEvent(DocumentEventTypes.DOCUMENT_PUBLISHED); 134 evtProducer.fireEvent(event); 135 doc.getCoreSession().saveDocument(doc); 136 session.save(); 137 } catch (IOException e) { 138 throw new NuxeoException("Failed to upload media", e); 139 } 140 141 } 142 }.runUnrestricted(); 143 144 } 145}