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 /** The current user provider */ 049 protected CurrentUserProvider _currentUserProvider; 050 051 /** The user directory helper */ 052 protected UserDirectoryHelper _userDirectoryHelper; 053 054 @Override 055 public void service(ServiceManager smanager) throws ServiceException 056 { 057 super.service(manager); 058 _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE); 059 _userDirectoryHelper = (UserDirectoryHelper) smanager.lookup(UserDirectoryHelper.ROLE); 060 } 061 062 063 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 064 { 065 Map<String, String> result = new HashMap<>(); 066 067 UserIdentity user = _getUser(parameters); 068 069 int size = parameters.getParameterAsInteger("size", 0); 070 int maxSize = parameters.getParameterAsInteger("maxSize", 0); 071 boolean download = parameters.getParameterAsBoolean("download", false); 072 String lang = parameters.getParameter("lang"); 073 074 result.put("imageUri", _getProfileImageUri(user, lang, size, maxSize, download)); 075 076 return result; 077 } 078 079 /** 080 * Get the profile image uri 081 * @param user the user identity. Can be null. 082 * @param lang the language 083 * @param size the image dimension 084 * @param maxSize the max image dimension 085 * @param download true if the uri is for download purposes. 086 * @return the profile image uri 087 */ 088 protected String _getProfileImageUri(UserIdentity user, String lang, int size, int maxSize, boolean download) 089 { 090 if (user == null) 091 { 092 return _getDefaultImageUriForAnonymous(size, maxSize, download); 093 } 094 else 095 { 096 String imageUri = _getImageUriFromUserContent(user, lang, size, maxSize, download); 097 if (imageUri == null) 098 { 099 imageUri = _getDefaultImageUri(user, lang, size, maxSize, download); 100 } 101 102 return imageUri; 103 } 104 } 105 106 /** 107 * Get the image uri for anonymous user 108 * @param size the image dimension 109 * @param maxSize the max image dimension 110 * @param download true if the uri is for download purposes. 111 * @return the image uri when user is null 112 */ 113 protected String _getDefaultImageUriForAnonymous(int size, int maxSize, boolean download) 114 { 115 return "skin://resources/img/avatar.png"; 116 } 117 118 /** 119 * Get the image uri from user content if exists 120 * @param user the user identity 121 * @param lang the content language 122 * @param size the image dimension 123 * @param maxSize the max image dimension 124 * @param download true if the uri is for download purposes. 125 * @return the image uri or null if not found 126 */ 127 protected String _getImageUriFromUserContent(UserIdentity user, String lang, int size, int maxSize, boolean download) 128 { 129 Content userContent = _userDirectoryHelper.getUserContent(user, lang); 130 if (userContent != null && userContent.hasValue(USER_CONTENT_IMAGE_PATH)) 131 { 132 File fileValue = userContent.getValue(USER_CONTENT_IMAGE_PATH); 133 if (fileValue instanceof ExplorerFile) 134 { 135 return ResolveURIComponent.resolveCroppedImage("explorer", ((ExplorerFile) fileValue).getResourceId(), size, size, download, false, true); 136 } 137 else 138 { 139 // Binary 140 String imgUri = USER_CONTENT_IMAGE_PATH + "?contentId=" + userContent.getId(); 141 return ResolveURIComponent.resolveCroppedImage("attribute", imgUri, size, size, download, false, true); 142 } 143 } 144 145 return null; 146 } 147 148 /** 149 * Get the default image uri for a user 150 * @param user the user identity 151 * @param lang the content language 152 * @param size the image dimension 153 * @param maxSize the max image dimension 154 * @param download true if the uri is for download purposes. 155 * @return the image uri 156 */ 157 protected String _getDefaultImageUri(UserIdentity user, String lang, int size, int maxSize, boolean download) 158 { 159 StringBuilder sb = new StringBuilder("cocoon://_plugins/core-ui/user/"); 160 sb.append(user.getPopulationId()) 161 .append("/") 162 .append(user.getLogin()) 163 .append("/image_") 164 .append(size); 165 166 List<String> params = new ArrayList<>(); 167 if (download) 168 { 169 params.add("download=true"); 170 } 171 if (maxSize > 0) 172 { 173 params.add("maxSize=" + maxSize); 174 } 175 176 if (params.size() > 0) 177 { 178 sb.append("?").append(String.join("&", params)); 179 } 180 181 return sb.toString(); 182 } 183 184 /** 185 * Get the user from parameters 186 * @param parameters the sitemap parameters 187 * @return the user identity 188 */ 189 protected UserIdentity _getUser(Parameters parameters) 190 { 191 String login = parameters.getParameter("login", StringUtils.EMPTY); 192 String populationId = parameters.getParameter("populationId", StringUtils.EMPTY); 193 // Default to current user if login not provided, except for the default source for which a login is not needed. 194 if (StringUtils.isEmpty(login) || StringUtils.isEmpty(populationId)) 195 { 196 return _currentUserProvider.getUser(); 197 } 198 else 199 { 200 return new UserIdentity(login, populationId); 201 } 202 } 203}