@Override
public BlobResource persist(BlobResource entity) throws Exception {
BlobResource result = null;
if (entity != null){
logger.info("Bytes to write: " + entity.getBytes().length);
logger.info("Mime type: " + entity.getMimeType());
if (entity.getBytes() != null){
try {
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(entity.getMimeType());
String path = file.getFullPath();
file = new AppEngineFile(path);
boolean lock = true;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
// This time we write to the channel directly
writeChannel.write(ByteBuffer.wrap(entity.getBytes()));
writeChannel.closeFinally();
BlobKey blobKey = fileService.getBlobKey(file);
if (blobKey != null){ // Blob is assumed to be written already
entity.setBlobKey(blobKey.getKeyString());
Key<BlobResource> key = put(entity);
try {
result = get(key);
} catch (Exception e) {
throw new BlobResourceException("Error persisting entity");
}
}
logger.info("Blob Key: " + blobKey.getKeyString());
} catch (Exception e) {
e.printStackTrace();
throw new BlobResourceException();
}
} else {
throw new NullValueException();
}
} else {
throw new BlobResourceException();
}
return result;
}