Allow to include image size into avatar URL

For some avatar providers the preferred image size can be specified.
If the image size is not provided to the avatar provider it returns
the avatar image with the default size and the browser will do the
scaling. If the default image size is not square the scaling looks
ugly.

Change-Id: I77755bfe44bbd4856f6d459c6cf60b990112058e
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/avatars/external/ExternalUrlAvatarProvider.java b/src/main/java/com/googlesource/gerrit/plugins/avatars/external/ExternalUrlAvatarProvider.java
index a9d0119..cdabba5 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/avatars/external/ExternalUrlAvatarProvider.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/avatars/external/ExternalUrlAvatarProvider.java
@@ -37,12 +37,14 @@
   private final boolean ssl;
   private String externalAvatarUrl;
   private String avatarChangeUrl;
+  private String sizeParameter;
 
   @Inject
   ExternalUrlAvatarProvider(@CanonicalWebUrl @Nullable String canonicalUrl,
       @GerritServerConfig Config cfg) {
     externalAvatarUrl = cfg.getString("avatar", null, "url");
     avatarChangeUrl = cfg.getString("avatar", null, "changeUrl");
+    sizeParameter = cfg.getString("avatar", null, "sizeParameter");
     ssl = canonicalUrl != null && canonicalUrl.startsWith("https://");
   }
 
@@ -70,7 +72,14 @@
     if (ssl && externalAvatarUrl.startsWith("http://")) {
       externalAvatarUrl = externalAvatarUrl.replace("http://", "https://");
     }
-    return replaceInUrl(externalAvatarUrl, forUser.getUserName());
+    StringBuilder avatarUrl = new StringBuilder();
+    avatarUrl.append(replaceInUrl(externalAvatarUrl, forUser.getUserName()));
+    if (imageSize > 0 && sizeParameter != null) {
+      avatarUrl.append("?");
+      avatarUrl.append(sizeParameter.replaceAll("\\$\\{size\\}",
+          Integer.toString(imageSize)));
+    }
+    return avatarUrl.toString();
   }
 
   @Override
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index 8d81d3c..9344499 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -8,6 +8,8 @@
   will then be replaced by the `username`. Required.
 * `avatar.changeUrl` - the URL shown in Gerrit's user settings to tell
   the user, where the avatar can be changed. Optional.
+* `avatar.sizeParameter` - URL parameter with `${size}` placeholder to
+  forward the preferred image size to the avatar provider. Optional.
 
 Example (to be added to `etc/gerrit.config`):
 
@@ -15,6 +17,7 @@
     [avatar]
         url = http://example.org/avatars/%s.jpg
         changeUrl = http://example.org/account.html
+        sizeParameter = s=${size}x${size}
 ```
 
 Please note that `http://` URLs will be automatically rewritten to