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