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;
019
020import org.apache.commons.codec.digest.DigestUtils;
021import org.apache.tika.io.IOUtils;
022
023import org.ametys.core.util.FilenameUtils;
024import org.ametys.plugins.explorer.resources.Resource;
025import org.ametys.web.explorer.ResourceHelper;
026
027/**
028 * Implementation of a {@link AbstractConvertDocument2ImagesComponent} for a {@link Resource}
029 */
030public class ConvertResource2ImagesComponent extends AbstractConvertDocument2ImagesComponent
031{   
032    /** Avalon ROLE. */
033    public static final String ROLE = ConvertResource2ImagesComponent.class.getName();
034    
035    /**
036     * Put the file in cache
037     * @param resource the resource to cache
038     * @param siteName the name of the site
039     * @return The absolute cache path
040     * @throws Exception if an error occurs while caching the file
041     */
042    public String doCache(Resource resource, String siteName) throws Exception
043    {
044        // Ensure the base folder exists.
045        String cachePath = _getCacheDirectory(resource, siteName);
046        
047        InputStream is = null;
048        InputStream is2 = null;
049        try
050        {
051            is = resource.getInputStream();
052            String md5sum = DigestUtils.md5Hex(is);
053            
054            is2 = resource.getInputStream();
055            return cache(cachePath, md5sum, is2, resource.getName(), resource.getId(), resource.getMimeType());
056        }
057        finally
058        {
059            IOUtils.closeQuietly(is);
060            IOUtils.closeQuietly(is2);
061        }
062    }
063    
064    private String _getCacheDirectory(Resource resource, String siteName)
065    {
066        
067        String fullPath = resource.getPath();
068        if (fullPath.startsWith(ResourceHelper.SHARED_RESOURCE_PATH))
069        {
070            // Shared resources.
071            return "/shared-resources" + FilenameUtils.encodePath(fullPath.substring(ResourceHelper.SHARED_RESOURCE_PATH.length()));
072        }
073
074        StringBuilder buff = new StringBuilder();
075
076        // Non-shared resources.
077        buff.append("/");
078        buff.append(siteName);
079        buff.append("/resources");
080        
081        buff.append(FilenameUtils.encodePath(resource.getResourcePath()));
082        
083        return buff.toString();
084    }
085}