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