001/* 002 * (C) Copyright 2006-2016 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 * Tiago Cardoso <[email protected]> 018 */ 019package org.nuxeo.ecm.platform.threed.adapter; 020 021import org.nuxeo.ecm.core.api.Blob; 022import org.nuxeo.ecm.core.api.Blobs; 023import org.nuxeo.ecm.core.api.DocumentModel; 024import org.nuxeo.ecm.platform.preview.adapter.AbstractPreviewer; 025import org.nuxeo.ecm.platform.preview.adapter.MimeTypePreviewer; 026import org.nuxeo.ecm.platform.preview.api.PreviewException; 027import org.nuxeo.ecm.platform.threed.ThreeDDocument; 028import org.nuxeo.ecm.platform.threed.TransmissionThreeD; 029import org.nuxeo.ecm.platform.threed.service.AutomaticLOD; 030import org.nuxeo.ecm.platform.threed.service.ThreeDService; 031import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper; 032import org.nuxeo.runtime.api.Framework; 033 034import java.util.ArrayList; 035import java.util.List; 036 037/* 038 * 3D content mimetype previewer. 039 * 040 * @since 8.4 041 */ 042public class ThreeDPreviewer extends AbstractPreviewer implements MimeTypePreviewer { 043 044 public List<Blob> getPreview(Blob blob, DocumentModel dm) throws PreviewException { 045 ThreeDDocument threeDDocument = dm.getAdapter(ThreeDDocument.class); 046 ThreeDService threeDService = Framework.getService(ThreeDService.class); 047 if (threeDDocument.getTransmissionThreeDs().isEmpty()) { 048 return new ArrayList<>(); 049 } 050 AutomaticLOD previewLOD = threeDService.getAutomaticLODs().iterator().next(); 051 return buildPreview(threeDDocument.getTransmissionThreeD(previewLOD.getName()), dm); 052 } 053 054 protected List<Blob> buildPreview(TransmissionThreeD transmissionThreeD, DocumentModel dm) { 055 List<Blob> blobResults = new ArrayList<>(); 056 String basePath = VirtualHostHelper.getContextPathProperty(); 057 StringBuffer html = new StringBuffer(); 058 html.append("<html><head>"); 059 html.append("<title>" + getPreviewTitle(dm) + "</title>"); 060 html.append(String.format( 061 "<script src=\"%s/platform-3d/bower_components/webcomponentsjs/webcomponents-lite.min.js\"></script>", 062 basePath)); 063 html.append(String.format("<link rel=\"import\" href=\"%s/platform-3d/nuxeo-3d-viewer.html\">", basePath)); 064 html.append(String.format("<nuxeo-3d-viewer src=\"%s\" loader=\"complete\">", 065 transmissionThreeD.getBlob().getFilename())); 066 blobResults.add(transmissionThreeD.getBlob()); 067 html.append("</nuxeo-3d-viewer>"); 068 html.append("</body>"); 069 Blob mainBlob = Blobs.createBlob(html.toString(), "text/html", null, "index.html"); 070 blobResults.add(0, mainBlob); 071 return blobResults; 072 } 073 074}