001/*
002 * (C) Copyright 2014 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-2.1.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 *     Vladimir Pasquier <vpasquier@nuxeo.com>
016 */
017package org.nuxeo.ecm.automation.io.services.enricher;
018
019import java.io.IOException;
020
021import org.codehaus.jackson.JsonGenerator;
022import org.nuxeo.ecm.core.api.Blob;
023import org.nuxeo.ecm.core.api.DocumentModel;
024import org.nuxeo.ecm.core.api.thumbnail.ThumbnailAdapter;
025import org.nuxeo.ecm.platform.ui.web.io.ThumbnailJsonEnricher;
026import org.nuxeo.ecm.platform.ui.web.tag.fn.DocumentModelFunctions;
027import org.nuxeo.runtime.api.Framework;
028
029/**
030 * This contributor adds a document Thumbnail Download URL
031 *
032 * @since 5.9.5
033 * @deprecated This enricher was migrated to {@link ThumbnailJsonEnricher}
034 */
035@Deprecated
036public class ThumbnailContentEnricher extends AbstractContentEnricher {
037
038    public static final String THUMBNAIL_URL_LABEL = "url";
039
040    public static final String THUMBNAIL_CONTENT_ID = "thumbnail";
041
042    public static final String THUMB_THUMBNAIL = "thumb:thumbnail";
043
044    public static final String DOWNLOAD_THUMBNAIL = "downloadThumbnail";
045
046    @Override
047    public void enrich(JsonGenerator jg, RestEvaluationContext ec) throws IOException {
048        DocumentModel doc = ec.getDocumentModel();
049        ThumbnailAdapter thumbnailAdapter = doc.getAdapter(ThumbnailAdapter.class);
050        jg.writeStartObject();
051        if (thumbnailAdapter != null) {
052            Blob thumbnail = thumbnailAdapter.getThumbnail(doc.getCoreSession());
053            if (thumbnail != null) {
054                String url = DocumentModelFunctions.fileUrl(Framework.getProperty("nuxeo.url"), DOWNLOAD_THUMBNAIL, doc,
055                        THUMB_THUMBNAIL, thumbnail.getFilename());
056                jg.writeStringField(THUMBNAIL_URL_LABEL, url);
057            } else {
058                writeEmptyThumbnail(jg);
059            }
060        } else {
061            writeEmptyThumbnail(jg);
062        }
063        jg.writeEndObject();
064        jg.flush();
065    }
066
067    private void writeEmptyThumbnail(JsonGenerator jg) throws IOException {
068        jg.writeStringField(THUMBNAIL_URL_LABEL, null);
069    }
070
071}