001/*
002 *  Copyright 2019 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.core.ui.parameter.files;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.acting.ServiceableAction;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Redirector;
027import org.apache.cocoon.environment.Request;
028import org.apache.cocoon.environment.SourceResolver;
029import org.apache.cocoon.servlet.multipart.Part;
030import org.apache.excalibur.source.impl.FileSource;
031
032import org.ametys.core.cocoon.JSonReader;
033import org.ametys.core.file.FileHelper;
034
035/**
036 * Create or update a file
037 */
038public class AddOrUpdateFileAction extends ServiceableAction
039{
040    private org.apache.excalibur.source.SourceResolver _sResolver;
041    private FileHelper _fileHelper;
042    
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        _sResolver = (org.apache.excalibur.source.SourceResolver) smanager.lookup(org.apache.excalibur.source.SourceResolver.ROLE);
047        _fileHelper = (FileHelper) smanager.lookup(FileHelper.ROLE);
048    }
049    
050    @Override
051    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
052    {
053        Map<String, Object> result = new HashMap<>();
054        
055        Request request = ObjectModelHelper.getRequest(objectModel);
056        String filePath = request.getParameter("parentPath");
057        String mode = parameters.getParameter("mode", "add");
058        
059        FileSource paramsDir = (FileSource) _sResolver.resolveURI("context://WEB-INF/param");
060        FileSource parentDir = (FileSource) _sResolver.resolveURI("context://WEB-INF/param" + (filePath.length() > 0 ? "/" + filePath : ""));
061        
062        Part part = (Part) request.get("filename");
063        
064        result.putAll(_fileHelper.addOrUpdateFile(part, parentDir, mode, false));
065        
066        if (result.containsKey("uri"))
067        {
068            String fileUrl = (String) result.get("uri");
069            String path = fileUrl.substring(paramsDir.getURI().length());
070            result.put("path", path.endsWith("/") ? path.substring(0, path.length() - 1) : path);
071            String parentPath = parentDir.getURI().substring(paramsDir.getURI().length());
072            result.put("parentPath", parentPath.endsWith("/") ? parentPath.substring(0, parentPath.length() - 1) : parentPath);
073        }
074        
075        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
076        return EMPTY_MAP;
077    }
078}