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.util.ArrayList;
020import java.util.Collections;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024
025import org.apache.avalon.framework.parameters.Parameters;
026import org.apache.cocoon.acting.ServiceableAction;
027import org.apache.cocoon.environment.Redirector;
028import org.apache.cocoon.environment.SourceResolver;
029import org.apache.commons.io.FileUtils;
030import org.apache.commons.lang3.StringUtils;
031
032import org.ametys.core.util.URIUtils;
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 = parameters.getParameter("path");
046        String sizePattern = parameters.getParameter("sizePattern");
047        
048        File imageFolder = FileUtils.getFile(RuntimeConfig.getInstance().getAmetysHome(), "flipbook", path, "pages");
049        
050        // List the image files.
051        if (imageFolder.exists())
052        {
053            List<File> imageFiles = new ArrayList<>(FileUtils.listFiles(imageFolder, new String[] {"png"}, false));
054            
055            String firstPagePath = null;
056            if (!imageFiles.isEmpty())
057            {
058                Collections.sort(imageFiles);
059                
060                if (imageFiles.get(0).canWrite()) // Do not continue if the image is beeing generated at this time 
061                {
062                    firstPagePath = imageFiles.get(0).getAbsolutePath();
063                    firstPagePath = StringUtils.substringBeforeLast(firstPagePath, ".") + sizePattern + "." + StringUtils.substringAfterLast(firstPagePath, "."); 
064        
065                    Map<String, String> result = new HashMap<>();
066                    result.put("first-page-path", URIUtils.encodePath(firstPagePath));
067        
068                    return result;
069                }
070            }
071        }
072        
073        return null;
074    }
075}