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.ResourceNotFoundException; 027import org.apache.cocoon.acting.ServiceableAction; 028import org.apache.cocoon.environment.Redirector; 029import org.apache.cocoon.environment.SourceResolver; 030import org.apache.commons.lang3.StringUtils; 031 032import org.ametys.cms.data.ExplorerFile; 033import org.ametys.cms.data.File; 034import org.ametys.cms.repository.Content; 035import org.ametys.cms.transformation.xslt.ResolveURIComponent; 036import org.ametys.core.user.CurrentUserProvider; 037import org.ametys.core.user.UserIdentity; 038import org.ametys.plugins.userdirectory.UserDirectoryHelper; 039import org.ametys.plugins.userdirectory.transformation.xslt.ProfileImageResolverHelper; 040 041/** 042 * Get the internal uri of a user's avatar 043 * 044 */ 045public class GetProfileImageUriAction extends ServiceableAction 046{ 047 /** The current user provider */ 048 protected CurrentUserProvider _currentUserProvider; 049 050 /** The user directory helper */ 051 protected UserDirectoryHelper _userDirectoryHelper; 052 053 @Override 054 public void service(ServiceManager smanager) throws ServiceException 055 { 056 super.service(manager); 057 _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE); 058 _userDirectoryHelper = (UserDirectoryHelper) smanager.lookup(UserDirectoryHelper.ROLE); 059 } 060 061 062 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 063 { 064 Map<String, String> result = new HashMap<>(); 065 066 UserIdentity user = _getUser(parameters); 067 068 int size = parameters.getParameterAsInteger("size", 0); 069 boolean download = parameters.getParameterAsBoolean("download", false); 070 String lang = parameters.getParameter("lang"); 071 072 result.put("imageUri", _getProfileImageUri(user, lang, size, download)); 073 074 return result; 075 } 076 077 /** 078 * Get the profile image uri 079 * @param user the user identity. Can be null. 080 * @param lang the language 081 * @param cropSize the image dimension. The image will be cropped. 082 * @param download true if the uri is for download purposes. 083 * @return the profile image uri 084 * @throws ResourceNotFoundException if no image for user was found 085 */ 086 protected String _getProfileImageUri(UserIdentity user, String lang, int cropSize, boolean download) throws ResourceNotFoundException 087 { 088 if (user == null) 089 { 090 throw new ResourceNotFoundException("Unable to get a profile image for a null user"); 091 } 092 else 093 { 094 String imageUri = _getProfileImageUriFromUserContentIfExists(user, lang, cropSize, download); 095 if (imageUri == null) 096 { 097 imageUri = _getDefaultImageUri(user, lang, cropSize, download); 098 } 099 100 return imageUri; 101 } 102 } 103 104 private String _getProfileImageUriFromUserContentIfExists(UserIdentity user, String lang, int cropSize, boolean download) 105 { 106 Content userContent = _userDirectoryHelper.getUserContent(user, lang); 107 if (userContent != null && userContent.hasValue(ProfileImageResolverHelper.USER_CONTENT_IMAGE_PATH)) 108 { 109 File imgFile = userContent.getValue(ProfileImageResolverHelper.USER_CONTENT_IMAGE_PATH); 110 if (imgFile instanceof ExplorerFile explorerFile) 111 { 112 return ResolveURIComponent.resolveCroppedImage("explorer", explorerFile.getResourceId(), cropSize, cropSize, download, false, true); 113 } 114 else 115 { 116 // Binary 117 String imgUri = ProfileImageResolverHelper.USER_CONTENT_IMAGE_PATH + "?contentId=" + userContent.getId(); 118 return ResolveURIComponent.resolveCroppedImage("attribute", imgUri, cropSize, cropSize, download, false, true); 119 } 120 } 121 122 return null; 123 } 124 125 /** 126 * Get the default image uri for a user 127 * @param user the user identity 128 * @param lang the content language 129 * @param cropSize the image dimension 130 * @param download true if the uri is for download purposes. 131 * @return the image uri 132 */ 133 protected String _getDefaultImageUri(UserIdentity user, String lang, int cropSize, boolean download) 134 { 135 StringBuilder sb = new StringBuilder("cocoon://_plugins/core-ui/user/"); 136 sb.append(user.getPopulationId()) 137 .append("/") 138 .append(user.getLogin()) 139 .append("/image_") 140 .append(cropSize); 141 142 List<String> params = new ArrayList<>(); 143 if (download) 144 { 145 params.add("download=true"); 146 } 147 148 if (params.size() > 0) 149 { 150 sb.append("?").append(String.join("&", params)); 151 } 152 153 return sb.toString(); 154 } 155 156 /** 157 * Get the user from parameters 158 * @param parameters the sitemap parameters 159 * @return the user identity 160 */ 161 protected UserIdentity _getUser(Parameters parameters) 162 { 163 String login = parameters.getParameter("login", StringUtils.EMPTY); 164 String populationId = parameters.getParameter("populationId", StringUtils.EMPTY); 165 // Default to current user if login not provided, except for the default source for which a login is not needed. 166 if (StringUtils.isEmpty(login) || StringUtils.isEmpty(populationId)) 167 { 168 return _currentUserProvider.getUser(); 169 } 170 else 171 { 172 return new UserIdentity(login, populationId); 173 } 174 } 175}