001/* 002 * Copyright 2020 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.cms.data; 017 018import java.util.Collections; 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; 029 030import org.ametys.plugins.repository.AmetysObjectResolver; 031import org.ametys.plugins.repository.data.ametysobject.ModelLessDataAwareAmetysObject; 032 033/** 034 * Cocoon action for retriebing a {@link Binary}. 035 */ 036public class GetBinaryAction extends ServiceableAction 037{ 038 private AmetysObjectResolver _resolver; 039 040 @Override 041 public void service(ServiceManager serviceManager) throws ServiceException 042 { 043 super.service(serviceManager); 044 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 045 } 046 047 public Map<String, String> act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 048 { 049 Request request = ObjectModelHelper.getRequest(objectModel); 050 051 String id = parameters.getParameter("objectId", null); 052 String path = parameters.getParameter("path", null); 053 054 int height = parameters.getParameterAsInteger("height", 0); 055 int width = parameters.getParameterAsInteger("width", 0); 056 int maxHeight = parameters.getParameterAsInteger("maxHeight", 0); 057 int maxWidth = parameters.getParameterAsInteger("maxWidth", 0); 058 059 ModelLessDataAwareAmetysObject object = _resolver.resolveById(id); 060 Binary binary = object.getValue(path); 061 062 request.setAttribute(Binary.class.getName(), binary); 063 064 String filename = binary.getFilename(); 065 066 String baseName = org.apache.commons.io.FilenameUtils.getBaseName(filename); 067 String extension = org.apache.commons.io.FilenameUtils.getExtension(filename); 068 069 String uriArgument; 070 071 if (height > 0 && width > 0) 072 { 073 uriArgument = '_' + String.valueOf(height) + 'x' + String.valueOf(width); 074 } 075 else if (maxHeight > 0 && maxWidth > 0) 076 { 077 uriArgument = "_max" + String.valueOf(maxHeight) + 'x' + String.valueOf(maxWidth); 078 } 079 else 080 { 081 uriArgument = ""; 082 } 083 084 return Collections.singletonMap("filename", baseName + uriArgument + (extension.isEmpty() ? "" : "." + extension)); 085 } 086}