001/*
002 *  Copyright 2010 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.site;
017
018import java.io.File;
019import java.io.FilenameFilter;
020import java.util.Map;
021
022import org.apache.avalon.framework.parameters.Parameters;
023import org.apache.cocoon.environment.Redirector;
024
025/**
026 * Invalidates cached images for a given site.
027 */
028public class InvalidateImagesAction extends InvalidatePageAction
029{
030    @Override
031    public Map act(Redirector redirector, org.apache.cocoon.environment.SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
032    {
033        String path = parameters.getParameter("path");
034        
035        File root = SiteCacheHelper.getRootCache();
036        
037        File[] files = getImages(root, path);
038        
039        if (files != null)
040        {
041            for (File file : files)
042            {
043                int j = path.lastIndexOf('/');
044                String folder = j != -1 ? path.substring(0, j) : "";
045                
046                String imagePath = folder + "/" + file.getName();
047                invalidatePage(root, imagePath);
048            }
049        }
050        
051        return EMPTY_MAP;
052    }
053    
054    /**
055     * Get the list of image files to invalidate
056     * @param root the root cache folder
057     * @param imagePath the image path
058     * @return the image files to invalidate
059     */
060    protected File[] getImages(File root, String imagePath)
061    {
062     // files to delete are like path/to/file_100x200.jpg
063        
064        int j = imagePath.lastIndexOf('/');
065        String folder = j != -1 ? imagePath.substring(0, j) : "";
066        final String fileName = j != -1 ? imagePath.substring(j + 1) : imagePath;
067        
068        int i = fileName.lastIndexOf('.');
069        final String ext = i != -1 ? fileName.substring(i) : "";
070        final String prefix = i != -1 ? fileName.substring(0, i) : fileName;
071        
072        File parentFolder = new File(root, folder);
073        
074        return parentFolder.listFiles(new FilenameFilter()
075        {
076            @Override
077            public boolean accept(File dir, String name)
078            {
079                return name.matches("^" + prefix + "_[0-9]+x[0-9]+" + ext + "$");
080            }
081        });
082    }
083}