001/*
002 * (C) Copyright 2010 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.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 *     "<a href=\"mailto:bjalon@nuxeo.com\">Benjamin JALON</a>"
016 */
017package org.nuxeo.ecm.platform.video;
018
019import java.io.Serializable;
020import java.util.Calendar;
021import java.util.Map;
022
023import javax.faces.context.ExternalContext;
024import javax.faces.context.FacesContext;
025import javax.servlet.http.HttpServletRequest;
026
027import org.jboss.seam.annotations.In;
028import org.jboss.seam.annotations.Install;
029import org.jboss.seam.annotations.Name;
030import org.jboss.seam.core.Interpolator;
031import org.jboss.seam.faces.FacesMessages;
032import org.nuxeo.common.utils.UserAgentMatcher;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.platform.ui.web.tag.fn.DocumentModelFunctions;
035import org.nuxeo.ecm.platform.video.service.VideoService;
036
037/**
038 * @author "<a href=\"mailto:bjalon@nuxeo.com\">Benjamin JALON</a>"
039 */
040@Name("videoActions")
041@Install(precedence = Install.FRAMEWORK)
042public class VideoActions implements Serializable {
043
044    private static final long serialVersionUID = 1L;
045
046    @In(create = true, required = false)
047    protected FacesMessages facesMessages;
048
049    @In(create = true)
050    protected Map<String, String> messages;
051
052    @In(create = true)
053    protected VideoService videoService;
054
055    public String getURLForPlayer(DocumentModel doc) {
056        return DocumentModelFunctions.bigFileUrl(doc, "file:content", "");
057    }
058
059    public String getTranscodedVideoURL(DocumentModel doc, String name) {
060        TranscodedVideo transcodedVideo = getTranscodedVideo(doc, name);
061        if (transcodedVideo == null) {
062            return null;
063        }
064
065        String blobPropertyName = transcodedVideo.getBlobPropertyName();
066        return DocumentModelFunctions.bigFileUrl(doc, blobPropertyName, transcodedVideo.getBlob().getFilename());
067    }
068
069    public TranscodedVideo getTranscodedVideo(DocumentModel doc, String name) {
070        VideoDocument videoDocument = doc.getAdapter(VideoDocument.class);
071        return videoDocument.getTranscodedVideo(name);
072    }
073
074    public String getURLForStaticPreview(DocumentModel videoDoc) {
075        String lastModification = "" + (((Calendar) videoDoc.getPropertyValue("dc:modified")).getTimeInMillis());
076        return DocumentModelFunctions.fileUrl("downloadPicture", videoDoc, "StaticPlayerView:content", lastModification);
077    }
078
079    public VideoConversionStatus getVideoConversionStatus(DocumentModel doc, String conversionName) {
080        return videoService.getProgressStatus(doc.getRepositoryName(), doc.getId(), conversionName);
081    }
082
083    public String getStatusMessageFor(VideoConversionStatus status) {
084        if (status == null) {
085            return "";
086        }
087        String i18nMessageTemplate = messages.get(status.getMessage());
088        if (i18nMessageTemplate == null) {
089            return "";
090        } else {
091            return Interpolator.instance().interpolate(i18nMessageTemplate, status.positionInQueue, status.queueSize);
092        }
093    }
094
095    public void launchConversion(DocumentModel doc, String conversionName) {
096        videoService.launchConversion(doc, conversionName);
097    }
098
099    public boolean isSafariHTML5() {
100        return UserAgentMatcher.isSafari5(getUserAgent());
101    }
102
103    public boolean isChromeHTML5() {
104        return UserAgentMatcher.isChrome(getUserAgent());
105    }
106
107    public boolean isFirefoxHTML5() {
108        return UserAgentMatcher.isFirefox4OrMore(getUserAgent());
109    }
110
111    protected String getUserAgent() {
112        ExternalContext econtext = FacesContext.getCurrentInstance().getExternalContext();
113        HttpServletRequest request = (HttpServletRequest) econtext.getRequest();
114        return request.getHeader("User-Agent");
115    }
116}