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