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.skinfactory.skins;
017
018import java.io.File;
019import java.util.HashMap;
020import java.util.LinkedHashMap;
021import java.util.Map;
022
023import org.apache.avalon.framework.parameters.Parameters;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Redirector;
026import org.apache.cocoon.environment.Request;
027import org.apache.cocoon.environment.SourceResolver;
028import org.apache.cocoon.servlet.multipart.Part;
029import org.apache.cocoon.servlet.multipart.PartOnDisk;
030import org.apache.cocoon.servlet.multipart.RejectedPart;
031
032import org.ametys.core.cocoon.JSonReader;
033import org.ametys.web.skin.actions.UploadSkinAction;
034
035/**
036 * This action receive a form with the "importfile" zip file as an exported model.
037 */
038public class UploadModelAction extends UploadSkinAction
039{
040    @Override
041    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
042    {
043        Request request = ObjectModelHelper.getRequest(objectModel);
044        
045        Map<String, Object> result = new LinkedHashMap<>();
046
047        try
048        {
049            // Checking file
050            Part part = (Part) request.get("importfile");
051            if (part instanceof RejectedPart || part == null)
052            {
053                result.put("success", false);
054                result.put("error", "rejected");
055            }
056            else
057            {
058                // Getting file
059                PartOnDisk uploadedFilePart = (PartOnDisk) part;
060                File uploadedFile = uploadedFilePart.getFile();
061                
062                String filename = (String) uploadedFilePart.getHeaders().get("filename");
063                String modelName = filename.substring(0, filename.length() - 4);
064                
065                if (getLogger().isDebugEnabled())
066                {
067                    getLogger().debug("Uploading model" + modelName);
068                }
069    
070                // Unzipping in tmp
071                File dir = _unzip(uploadedFile);
072                
073                // Filter files
074                _filter(dir, dir.getAbsolutePath(), true);
075                
076                result.put("skinDir", dir.getName());
077                result.put("tempDir", dir.getParentFile().getName());
078                result.put("skinName", modelName);
079                result.put("success", "true");
080            }
081        }
082        catch (Exception e)
083        {
084            getLogger().error("Unable to add model", e);
085            result.put("success", false);
086            
087            Map<String, String> ex = new HashMap<>();
088            ex.put("message", e.getMessage());
089
090            result.put("error", ex);
091        }
092        
093        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
094        
095        return result;    
096    }
097}