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 *
019 */
020package org.nuxeo.ecm.platform.video.adapter;
021
022import static org.nuxeo.ecm.platform.video.VideoConstants.VIDEO_FACET;
023
024import java.io.File;
025import java.io.IOException;
026
027import org.nuxeo.common.utils.FileUtils;
028import org.nuxeo.ecm.core.api.Blob;
029import org.nuxeo.ecm.core.api.Blobs;
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.NuxeoException;
033import org.nuxeo.ecm.core.api.thumbnail.ThumbnailFactory;
034import org.nuxeo.ecm.platform.picture.api.adapters.PictureResourceAdapter;
035import org.nuxeo.ecm.platform.types.adapter.TypeInfo;
036
037/**
038 * Movie thumbnail factory
039 *
040 * @since 5.7
041 */
042public class ThumbnailVideoFactory implements ThumbnailFactory {
043
044    @Override
045    public Blob getThumbnail(DocumentModel doc, CoreSession session) {
046        if (!doc.hasFacet(VIDEO_FACET)) {
047            throw new NuxeoException("Document is not a video");
048        }
049        // Choose the nuxeo default thumbnail of the picture views (screenshots
050        // of the video taken during creation)
051        PictureResourceAdapter picResAdapter = doc.getAdapter(PictureResourceAdapter.class);
052        Blob thumbnailView = picResAdapter.getPictureFromTitle("Small");
053        if (thumbnailView == null) {
054            // try Thumbnail view
055            thumbnailView = picResAdapter.getPictureFromTitle("Thumbnail");
056            if (thumbnailView == null) {
057                TypeInfo docType = doc.getAdapter(TypeInfo.class);
058                try {
059                    return Blobs.createBlob(FileUtils.getResourceFileFromContext("nuxeo.war" + File.separator
060                            + docType.getBigIcon()));
061                } catch (IOException e) {
062                    throw new NuxeoException(e);
063                }
064            }
065        }
066        return thumbnailView;
067    }
068
069    @Override
070    public Blob computeThumbnail(DocumentModel doc, CoreSession session) {
071        return null;
072    }
073}