001/*
002 *  Copyright 2011 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.File;
019import java.io.UnsupportedEncodingException;
020import java.net.URLDecoder;
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.avalon.framework.parameters.Parameters;
028import org.apache.cocoon.acting.ServiceableAction;
029import org.apache.cocoon.environment.Redirector;
030import org.apache.cocoon.environment.SourceResolver;
031import org.apache.commons.io.FileUtils;
032
033import org.ametys.runtime.servlet.RuntimeConfig;
034
035/**
036 * Get the first page image for thumbnail
037 */
038public class GetFirstPageAction extends ServiceableAction
039{
040    /** Request attribute from path to images' directory */
041    public static final String IMAGES_DIRECTORY_PATH_REQUEST_ATTR = "images-base-directory";
042    
043    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
044    {
045        String path = _decodePath(parameters.getParameter("path"));
046        File imageFolder = FileUtils.getFile(RuntimeConfig.getInstance().getAmetysHome(), "flipbook", path, "pages");
047        
048        // List the image files.
049        if (imageFolder.exists())
050        {
051            List<File> imageFiles = new ArrayList<>(FileUtils.listFiles(imageFolder, new String[] {"png"}, false));
052            
053            String firstPagePath = null;
054            if (!imageFiles.isEmpty())
055            {
056                Collections.sort(imageFiles);
057                
058                if (imageFiles.get(0).canWrite()) // Do not continue if the image is beeing generated at this time 
059                {
060                    firstPagePath = imageFiles.get(0).getAbsolutePath();
061        
062                    Map<String, String> result = new HashMap<>();
063                    result.put("first-page-path", firstPagePath);
064        
065                    return result;
066                }
067            }
068        }
069        
070        return null;
071    }
072    
073    /**
074     * Decode the resource path
075     * @param path the resource path
076     * @return the decoded resource path
077     * @throws UnsupportedEncodingException if UTF-8 encoding is not supported
078     */
079    protected String _decodePath (String path) throws UnsupportedEncodingException
080    {
081        StringBuffer sb = new StringBuffer();
082        
083        String[] parts = path.split("/");
084        for (String part : parts)
085        {
086            sb.append("/");
087            sb.append(URLDecoder.decode(part, "utf-8"));
088        }
089        return sb.toString();
090    }
091}