001/*
002 *  Copyright 2014 Anyware Services
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 */
016package org.ametys.plugins.flipbook;
017
018import java.io.InputStream;
019import java.util.Arrays;
020import java.util.Iterator;
021import java.util.List;
022
023import org.apache.commons.codec.digest.DigestUtils;
024import org.apache.tika.io.IOUtils;
025
026import org.ametys.cms.repository.Content;
027import org.ametys.plugins.repository.metadata.BinaryMetadata;
028import org.ametys.plugins.repository.metadata.CompositeMetadata;
029import org.ametys.plugins.repository.metadata.CompositeMetadata.MetadataType;
030
031/**
032 * Implementation of a {@link AbstractConvertDocument2ImagesComponent} for a binary metadata
033 */
034public class ConvertMetadata2ImagesComponent extends AbstractConvertDocument2ImagesComponent
035{
036    /** Avalon ROLE. */
037    public static final String ROLE = ConvertMetadata2ImagesComponent.class.getName();
038    
039    /**
040     * Put the file in cache
041     * @param content the content
042     * @param metadataPath the path of the metadata
043     * @param siteName the name of the site
044     * @return The absolute cache path
045     * @throws Exception if an error occurs while caching the file
046     */
047    public String doCache(Content content, String metadataPath, String siteName) throws Exception
048    {
049        CompositeMetadata metadata = content.getMetadataHolder();
050        
051        InputStream is = null;
052        InputStream is2 = null;
053        BinaryMetadata binary = _getBinaryMetadata(metadataPath, metadata);
054
055        String cachePath = _getCacheDirectory(metadataPath, binary.getFilename(), content.getName(), siteName);
056        try
057        {
058            is = binary.getInputStream();
059            String md5sum = DigestUtils.md5Hex(is);
060            
061            is2 = binary.getInputStream();
062            return cache(cachePath, md5sum, is2, content.getName(), content.getId(), binary.getMimeType());
063        }
064        finally
065        {
066            IOUtils.closeQuietly(is);
067            IOUtils.closeQuietly(is2);
068        }
069    }
070    
071    private BinaryMetadata _getBinaryMetadata(String metadataPath, CompositeMetadata metadata)
072    {
073        List<String> pathElements = Arrays.asList(metadataPath.split("/"));        
074        Iterator<String> it = pathElements.iterator();
075        
076        CompositeMetadata metadataBis = metadata;
077        BinaryMetadata binary = null;
078        while (it.hasNext())
079        {
080            String pathElement = it.next();
081            
082            if (it.hasNext())
083            {
084                // not the last segment : it is a composite
085                metadataBis = metadataBis.getCompositeMetadata(pathElement);
086            }
087            else
088            {
089                if (metadataBis.getType(pathElement) != MetadataType.BINARY)
090                {
091                    throw new UnsupportedOperationException("Only binary metadata are allowed");
092                }
093                
094                binary = metadataBis.getBinaryMetadata(pathElement);
095            }
096        }
097        
098        return binary;
099    }
100    
101    private String _getCacheDirectory(String metadataPath, String fileName, String contentName, String siteName)
102    {
103        StringBuilder buff = new StringBuilder();
104        buff.append("/");
105        buff.append(siteName);
106        buff.append("/contents/");
107        buff.append(contentName);
108        buff.append("/metadatas/");
109        buff.append(metadataPath);
110        buff.append("/");
111        buff.append(fileName);
112        
113        return buff.toString();
114    }
115}