throw NotFoundException.withInternalMessage("Attachment id unknown: " + id);
}
BlobKey key = metadata.getBlobKey();
// TODO(danilatos): Factor out some of this code into a separate method so that
// thumbnails can be eagerly created at upload time.
ThumbnailData thumbnail = thumbnailDirectory.getWithoutTx(key);
if (thumbnail == null) {
log.info("Generating and storing thumbnail for " + key);
ImageMetadata thumbDimensions = metadata.getThumbnail();
if (thumbDimensions == null) {
// TODO(danilatos): Provide a default thumbnail
throw NotFoundException.withInternalMessage("No thumbnail available for attachment " + id);
}
byte[] thumbnailBytes = rawService.getResizedImageBytes(key,
thumbDimensions.getWidth(), thumbDimensions.getHeight());
thumbnail = new ThumbnailData(key, thumbnailBytes);
if (thumbnailBytes.length > maxThumbnailSavedSizeBytes) {
log.warning("Thumbnail for " + key + " too large to store " +
"(" + thumbnailBytes.length + " bytes)");
// TODO(danilatos): Cache this condition in memcache.
throw NotFoundException.withInternalMessage("Thumbnail too large for attachment " + id);
}
thumbnailDirectory.getOrAdd(thumbnail);
} else {
log.info("Using already stored thumbnail for " + key);
}
// TODO(danilatos): Other headers for mime type, fileName + "Thumbnail", etc?
resp.getOutputStream().write(thumbnail.getBytes());
return null;
}