001/*
002 * (C) Copyright 2006-2008 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 *     Alexandre Russel
018 *     Andre Justo
019 *
020 * $Id$
021 */
022
023package org.nuxeo.ecm.platform.preview.adapter;
024
025import java.util.ArrayList;
026import java.util.List;
027
028import org.nuxeo.ecm.core.api.Blob;
029import org.nuxeo.ecm.core.api.Blobs;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.platform.preview.api.PreviewException;
032import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
033
034/**
035 * @author Alexandre Russel
036 */
037public class ImagePreviewer extends AbstractPreviewer implements MimeTypePreviewer {
038
039    public List<Blob> getPreview(Blob blob, DocumentModel dm) throws PreviewException {
040        List<Blob> blobResults = new ArrayList<>();
041        String basePath = VirtualHostHelper.getContextPathProperty();
042        StringBuffer html = new StringBuffer();
043        html.append("<html><head>");
044        html.append("<title>" + getPreviewTitle(dm) + "</title>");
045        html.append(String.format("<script src=\"%s/bower_components/webcomponentsjs/webcomponents-lite.js\"></script>", basePath));
046        html.append(String.format("<link rel=\"import\" href=\"%s/viewers/nuxeo-image-viewer.vulcanized.html\">", basePath));
047        html.append("<style>");
048        html.append("nuxeo-image-viewer {");
049        html.append("height: 100%; }");
050        html.append("</style>");
051        html.append("</head><body>");
052        html.append("<nuxeo-image-viewer src=\"image\" controls responsive></nuxeo-image-viewer>");
053        html.append("</body>");
054        Blob mainBlob = Blobs.createBlob(html.toString(), "text/html", null, "index.html");
055        blob.setFilename("image");
056        blobResults.add(mainBlob);
057        blobResults.add(blob);
058        return blobResults;
059    }
060}