package com.faforever.api.data.listeners;

import com.faforever.api.config.FafApiProperties;
import com.faforever.api.data.domain.Avatar;
import org.springframework.stereotype.Component;

import javax.inject.Inject;
import javax.persistence.PostLoad;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

@Component
public class AvatarEnricherListener {

  private static FafApiProperties fafApiProperties;

  @Inject
  public void init(FafApiProperties fafApiProperties) {
    AvatarEnricherListener.fafApiProperties = fafApiProperties;
  }

  @PostLoad
  public void enrich(Avatar avatar) throws UnsupportedEncodingException {
    String encodedFileName = URLEncoder.encode(avatar.getFilename(), StandardCharsets.UTF_8.toString());
    String url = String.format(fafApiProperties.getAvatar().getDownloadUrlFormat(), encodedFileName);
    avatar.setUrl(url);
  }

}

@@ -179,16 +179,62 @@
 mponent;
+%0Aimport org.springframework.web.util.UriUtils;
 %0A%0Aimport
@@ -294,81 +294,8 @@
 ad;%0A
-import java.io.UnsupportedEncodingException;%0Aimport java.net.URLEncoder;%0A
 impo
@@ -623,150 +623,9 @@
 ar) 
-throws UnsupportedEncodingException %7B%0A    String encodedFileName = URLEncoder.encode(avatar.getFilename(), StandardCharsets.UTF_8.toString());
+%7B
 %0A   
@@ -709,23 +709,28 @@
 (), 
-encoded
+avatar.get
 File
-N
+n
 ame
+()
 );%0A 
@@ -746,19 +746,64 @@
 .setUrl(
-url
+UriUtils.encodePath(url, StandardCharsets.UTF_8)
 );%0A  %7D%0A%0A

Fix avatar url encoding (whitespace was encoded with + rather than %20)