in src/main/java/com/googlesource/gerrit/plugins/imagare/PostImage.java [113:144]
private ImageInfo storeImage(ProjectState ps, String imageData, String fileName)
throws RestApiException, IOException, PermissionBackendException, NoSuchProjectException {
Matcher m = imageDataPattern.matcher(imageData);
if (m.matches()) {
String receivedMimeType = m.group(1);
String encoding = m.group(2);
String encodedContent = m.group(3);
if (fileName == null) {
int pos = receivedMimeType.indexOf('/');
if (pos > 0 && pos + 1 < receivedMimeType.length()) {
fileName = "img." + receivedMimeType.substring(pos + 1);
} else {
throw new BadRequestException("bad mime type: " + receivedMimeType);
}
}
if ("base64".equals(encoding)) {
byte[] content = Base64.decode(encodedContent);
MimeType mimeType = registry.getMimeType("img." + receivedMimeType, content);
if (!"image".equals(mimeType.getMediaType())) {
throw new MethodNotAllowedException("no image");
}
if (!receivedMimeType.equals(mimeType.toString())) {
throw new BadRequestException("incorrect mime type");
}
return new ImageInfo(storeImage(ps, content, fileName));
}
throw new MethodNotAllowedException("unsupported encoding");
}
throw new BadRequestException("invalid image data");
}