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.artisteer.actions;
017
018import java.io.File;
019import java.io.FileNotFoundException;
020import java.io.IOException;
021import java.nio.charset.StandardCharsets;
022import java.util.List;
023import java.util.regex.Pattern;
024
025import org.apache.commons.io.FileUtils;
026
027import org.ametys.web.skin.actions.UploadSkinAction;
028
029/**
030 * This action receive a form with the "importfile" zip file as an artisteer exported skin.
031 * Replace existing skin 
032 */
033public class UploadArtisteerSkinAction extends UploadSkinAction
034{
035    private static final Pattern __UNVALUED_RULE_PATTERN = Pattern.compile("^.*\\{[A-Z][a-zA-Z]*\\}.*$", Pattern.MULTILINE);
036    
037    private static final List<String> __FILE_NAME_TO_DELETE = List.of("style.ie6.css",
038                                                                      "style.ie7.css",
039                                                                      "style.css", 
040                                                                      "swfobject.js",
041                                                                      "container.swf",
042                                                                      "expressInstall.swf",
043                                                                      "images_artisteer");
044    
045    @Override
046    protected void _filter(File skinDir, String absoluteSkinPath, boolean isModel) throws IOException
047    {
048        super._filter(skinDir, absoluteSkinPath, isModel);
049        
050        File[] files = skinDir.listFiles();
051        for (File file : files)
052        {
053            if (__FILE_NAME_TO_DELETE.contains(file.getName()))
054            {
055                FileUtils.deleteQuietly(file);
056            }
057            else if ("images".equals(file.getAbsolutePath().substring(absoluteSkinPath.length() + 1)) && file.isDirectory())
058            {
059                FileUtils.moveDirectory(file, new File(skinDir, "resources/img"));
060            }
061            else if ("favicon.ico".equals(file.getName()) && !file.isDirectory())
062            {
063                if (new File(skinDir, "resources/img").exists())
064                {
065                    FileUtils.moveFile(file, new File(skinDir, "resources/img/favicon.ico"));
066                }
067                else
068                {
069                    FileUtils.moveFile(file, new File(skinDir, "images/favicon.ico"));
070                }
071            }
072            
073            if (file.getName().endsWith(".css") && file.exists())
074            {
075                _removeUnvaluedLines(file);
076            }
077        }
078    }
079    
080    private void _removeUnvaluedLines(File cssFile) throws FileNotFoundException, IOException
081    {
082        // Remove unvalued lines generated by Artisteer such as "border: 1px solid {HoverTextColor}"
083        String content = FileUtils.readFileToString(cssFile, StandardCharsets.UTF_8);
084        content = __UNVALUED_RULE_PATTERN.matcher(content).replaceAll("");
085        FileUtils.write(cssFile, content, StandardCharsets.UTF_8);
086    }
087}