Now a 404 not found status is returned if the avatar has not be set for the user instead of an empty image (zero bytes lenght)

This commit is contained in:
Mauro Mugnaini 2020-09-04 18:10:13 +02:00
parent 51dd368717
commit ff6985ea48
4 changed files with 12 additions and 7 deletions

View File

@ -52,7 +52,7 @@ public abstract class AbstractAvatarResource {
protected StreamingOutput fetchUserImage(RealmModel realm, UserModel user) {
AvatarStorageProvider asp = getAvatarStorageProvider();
InputStream is = asp.loadAvatarImage(realm, user);
return output -> IOUtils.copy(is, output);
return is != null ? output -> IOUtils.copy(is, output) : null;
}
}

View File

@ -7,12 +7,14 @@ import javax.ws.rs.Consumes;
import javax.ws.rs.ForbiddenException;
import javax.ws.rs.GET;
import javax.ws.rs.NotAuthorizedException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import javax.ws.rs.core.UriInfo;
import org.jboss.resteasy.annotations.cache.NoCache;
@ -62,7 +64,11 @@ public class AvatarResource extends AbstractAvatarResource {
throw new NotAuthorizedException("Bearer");
}
logger.debugf("Getting avatar for user %s in realm %s", auth.getUser(), auth.getSession().getRealm());
return Response.ok(fetchUserImage(auth.getSession().getRealm(), auth.getUser())).build();
StreamingOutput so = fetchUserImage(auth.getSession().getRealm(), auth.getUser());
if (so == null) {
throw new NotFoundException("Avatar image not found");
}
return Response.ok(so).build();
}
@POST

View File

@ -1,6 +1,5 @@
package org.gcube.keycloak.avatar.storage.file;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@ -78,7 +77,7 @@ public class FileAvatarStorageProvider implements AvatarStorageProvider {
return new FileInputStream(avatarFile);
} catch (FileNotFoundException e) {
logger.debugf("Avatar file not found for user '%s' in realm '%s'", user.getUsername(), realm.getName());
return new ByteArrayInputStream(new byte[0]);
return null;
}
}

View File

@ -49,10 +49,10 @@ public class UserAttributeAvatarStorageProvider implements AvatarStorageProvider
}
private InputStream toBinaryStream(String base64EncodedImage) {
byte[] decoded = base64EncodedImage != null ? Base64.getDecoder().decode(base64EncodedImage) : new byte[0];
return new ByteArrayInputStream(decoded);
return base64EncodedImage != null ?
new ByteArrayInputStream(Base64.getDecoder().decode(base64EncodedImage)) : null;
}
@Override
public void deleteAvatarImage(RealmModel realm, UserModel user) {
logger.debugf("Removing avatar image user's attribute: %s", userAttribute);