001/*
002 *  Copyright 2012 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.upload;
017
018import java.io.IOException;
019import java.io.InputStream;
020import java.util.HashMap;
021import java.util.LinkedHashMap;
022import java.util.Map;
023
024import org.apache.avalon.framework.parameters.Parameters;
025import org.apache.cocoon.acting.Action;
026import org.apache.cocoon.environment.ObjectModelHelper;
027import org.apache.cocoon.environment.Redirector;
028import org.apache.cocoon.environment.Request;
029import org.apache.cocoon.environment.SourceResolver;
030import org.apache.cocoon.servlet.multipart.Part;
031import org.apache.commons.lang.exception.ExceptionUtils;
032
033import org.ametys.core.cocoon.JSonReader;
034import org.ametys.core.upload.Upload;
035import org.ametys.core.upload.UploadManager;
036import org.ametys.core.util.cocoon.AbstractCurrentUserProviderServiceableAction;
037
038/**
039 * {@link Action} for uploading a file and store it using the {@link UploadManager}.
040 */
041public class UploadAction extends AbstractCurrentUserProviderServiceableAction
042{
043    private UploadManager _uploadManager;
044    
045    @Override
046    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
047    {
048        Request request = ObjectModelHelper.getRequest(objectModel);
049
050        //Lazy initialize the upload manager because it cannot be found if the config is not complete  
051        if (_uploadManager == null)
052        {
053            _uploadManager = (UploadManager) manager.lookup(UploadManager.ROLE);
054        }
055        
056        Part partUploaded = (Part) request.get("file");
057        Map<String, Object> result = new LinkedHashMap<>();
058
059        if (partUploaded == null)
060        {
061            throw new Exception("Missing request parameter file");
062        }
063
064        if (partUploaded.isRejected())
065        {
066            result.put("success", false);
067            result.put("error", "rejected");
068        }
069        else
070        {
071            Upload upload = null;
072    
073            try (InputStream is = partUploaded.getInputStream())
074            {
075                upload = _uploadManager.storeUpload(_getCurrentUser(), partUploaded.getFileName(), is);
076                
077                result.put("success", true);
078                result.put("id", upload.getId());
079                result.put("filename", upload.getFilename());
080                result.put("size", upload.getLength());
081                result.put("viewHref", _getUrlForView(upload));
082                result.put("downloadHref", _getUrlForDownload(upload));
083            }
084            catch (IOException e)
085            {
086                getLogger().error("Unable to store uploaded file: " + partUploaded, e);
087                
088                result.put("success", false);
089                
090                Map<String, String> ex = new HashMap<>();
091                ex.put("message", e.getMessage());
092                ex.put("stacktrace", ExceptionUtils.getFullStackTrace(e));
093
094                result.put("error", ex);
095            }
096        }
097        
098        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
099
100        return EMPTY_MAP;
101    }
102    
103    /**
104     * Get the url for view the uploaded file
105     * @param upload The file uploaded
106     * @return The url for view
107     */
108    protected String _getUrlForView (Upload upload)
109    {
110        return "/plugins/core/upload/file?id=" + upload.getId();
111    }
112    
113    /**
114     * Get the url for download the uploaded file
115     * @param upload The file uploaded
116     * @return The url for view
117     */
118    protected String _getUrlForDownload (Upload upload)
119    {
120        return "/plugins/core/upload/file?id=" + upload.getId() + "&download=true";
121    }
122}