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.plugins.userdirectory.action; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.parameters.Parameters; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.cocoon.acting.ServiceableAction; 027import org.apache.cocoon.environment.Redirector; 028import org.apache.cocoon.environment.SourceResolver; 029import org.apache.commons.lang3.StringUtils; 030 031import org.ametys.cms.data.ExplorerFile; 032import org.ametys.cms.data.File; 033import org.ametys.cms.repository.Content; 034import org.ametys.cms.transformation.xslt.ResolveURIComponent; 035import org.ametys.core.user.CurrentUserProvider; 036import org.ametys.core.user.UserIdentity; 037import org.ametys.plugins.userdirectory.UserDirectoryHelper; 038 039/** 040 * Get the internal uri of a user's avatar 041 * 042 */ 043public class GetProfileImageUriAction extends ServiceableAction 044{ 045 /** Attribute path for user's image */ 046 public static final String USER_CONTENT_IMAGE_PATH = "illustration/image"; 047 048 private CurrentUserProvider _currentUserProvider; 049 private UserDirectoryHelper _userDirectoryHelper; 050 051 @Override 052 public void service(ServiceManager smanager) throws ServiceException 053 { 054 super.service(manager); 055 _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE); 056 _userDirectoryHelper = (UserDirectoryHelper) smanager.lookup(UserDirectoryHelper.ROLE); 057 } 058 059 060 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 061 { 062 Map<String, String> result = new HashMap<>(); 063 064 UserIdentity user = _getUser(parameters); 065 066 int size = parameters.getParameterAsInteger("size", 0); 067 int maxSize = parameters.getParameterAsInteger("maxSize", 0); 068 boolean download = parameters.getParameterAsBoolean("download", false); 069 String lang = parameters.getParameter("lang"); 070 071 if (user == null) 072 { 073 result.put("imageUri", "skin://resources/img/avatar.png"); 074 } 075 else 076 { 077 Content userContent = _userDirectoryHelper.getUserContent(user, lang); 078 if (userContent != null && userContent.hasValue(USER_CONTENT_IMAGE_PATH)) 079 { 080 File fileValue = userContent.getValue(USER_CONTENT_IMAGE_PATH); 081 if (fileValue instanceof ExplorerFile) 082 { 083 result.put("imageUri", ResolveURIComponent.resolveCroppedImage("explorer", ((ExplorerFile) fileValue).getResourceId(), size, size, false, false, true)); 084 } 085 else 086 { 087 // Binary 088 String imgUri = USER_CONTENT_IMAGE_PATH + "?contentId=" + userContent.getId(); 089 result.put("imageUri", ResolveURIComponent.resolveCroppedImage("attribute", imgUri, size, size, false, false, true)); 090 } 091 } 092 else 093 { 094 StringBuilder sb = new StringBuilder("cocoon://_plugins/core-ui/user/"); 095 sb.append(user.getPopulationId()) 096 .append("/") 097 .append(user.getLogin()) 098 .append("/image_") 099 .append(size); 100 101 List<String> params = new ArrayList<>(); 102 if (download) 103 { 104 params.add("download=true"); 105 } 106 if (maxSize > 0) 107 { 108 params.add("maxSize=" + maxSize); 109 } 110 111 if (params.size() > 0) 112 { 113 sb.append("?").append(String.join("&", params)); 114 } 115 result.put("imageUri", sb.toString()); 116 } 117 } 118 119 return result; 120 } 121 122 123 private UserIdentity _getUser(Parameters parameters) 124 { 125 String login = parameters.getParameter("login", StringUtils.EMPTY); 126 String populationId = parameters.getParameter("populationId", StringUtils.EMPTY); 127 // Default to current user if login not provided, except for the default source for which a login is not needed. 128 if (StringUtils.isEmpty(login) || StringUtils.isEmpty(populationId)) 129 { 130 return _currentUserProvider.getUser(); 131 } 132 else 133 { 134 return new UserIdentity(login, populationId); 135 } 136 } 137}