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.plugins.workspaces.documents.actions; 017 018import java.util.HashMap; 019import java.util.List; 020import java.util.Map; 021 022import org.apache.avalon.framework.parameters.Parameters; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 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; 030 031import org.ametys.core.cocoon.JSonReader; 032import org.ametys.core.observation.AbstractNotifierAction; 033import org.ametys.plugins.explorer.resources.AddOrUpdateResource; 034import org.ametys.plugins.workspaces.documents.WorkspaceExplorerResourceDAO; 035import org.ametys.plugins.workspaces.project.ProjectManager; 036import org.ametys.plugins.workspaces.project.objects.Project; 037 038/** 039 * Action for adding or updating a resource file. 040 * This Action is needed as vue dropzone needs an url to do a POST request, so we can not call directly the callable function from the front 041 * Due to how vuedropzone works, files are uploaded one at a time 042 */ 043public class AddOrUpdateResourceAction extends AbstractNotifierAction 044{ 045 /** resource operation helper */ 046 protected AddOrUpdateResource _addOrUpdateResource; 047 048 /** The documents module DAO */ 049 protected WorkspaceExplorerResourceDAO _workspaceExplorerResourceDAO; 050 051 /** Project manager */ 052 protected ProjectManager _projectManager; 053 054 @Override 055 public void service(ServiceManager serviceManager) throws ServiceException 056 { 057 super.service(serviceManager); 058 _addOrUpdateResource = (AddOrUpdateResource) serviceManager.lookup(AddOrUpdateResource.ROLE); 059 _workspaceExplorerResourceDAO = (WorkspaceExplorerResourceDAO) serviceManager.lookup(WorkspaceExplorerResourceDAO.ROLE); 060 _projectManager = (ProjectManager) serviceManager.lookup(ProjectManager.ROLE); 061 } 062 063 @Override 064 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 065 { 066 Request request = ObjectModelHelper.getRequest(objectModel); 067 068 Part part = (Part) request.get("filename"); 069 String parentId = request.getParameter("collection"); 070 String projectName = request.getParameter("projectName"); 071 if (projectName != null && !projectName.isEmpty()) 072 { 073 Long storageSpaceLimit = _workspaceExplorerResourceDAO.getStorageSpaceLimit(); 074 Project project = _projectManager.getProject(projectName); 075 Long usedStorageSpace = _workspaceExplorerResourceDAO.getUsedStorageSpaceByProject(project); 076 077 // Check if the project has a maximum space defined and if the file to upload exceed the maximum space 078 if (storageSpaceLimit != null && usedStorageSpace + part.getSize() > storageSpaceLimit) 079 { 080 Map<String, Object> result = new HashMap<>(); 081 result.put("success", false); 082 result.put("message", Map.of(part.getFileName(), "exceed-max-size")); 083 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 084 return EMPTY_MAP; 085 } 086 } 087 088 boolean unzip = Boolean.valueOf(request.getParameter("unzip")); 089 String rawMode = parameters.getParameter("mode", "add"); 090 091 if (unzip) 092 { 093 rawMode = "add-unzip"; 094 } 095 096 _addOrUpdateResource.addOrUpdateResources(parentId, rawMode, unzip, List.of(part)); 097 098 return EMPTY_MAP; 099 } 100}