}
// Go ahead and try to create the FilesystemKeyValueStore. This is a simple operation, so, if it's going
// to fail, then it's better to fail now rather than after spending a lot of effort creating the photo hash
// and thumbnails.
final FilesystemKeyValueStore keyValueStore = getFilesystemKeyValueStore();
// Attempt to parse the JSON metadata
final FluxtreamCapturePhoto.PhotoUploadMetadata metadata;
try {
metadata = gson.fromJson(jsonMetadata, FluxtreamCapturePhoto.PhotoUploadMetadata.class);
}
catch (Exception e) {
final String message = "Photo upload failed because an Exception occurred while trying to parse the photo metadata";
LOG.error("FluxtreamCapturePhotoStore.saveOrUpdatePhoto(): " + message);
throw new InvalidDataException(message);
}
// Validate the JSON metadata
if (metadata == null || !metadata.isValid()) {
final String message = "Photo upload failed because the JSON metadata is null or invalid";
LOG.error("FluxtreamCapturePhotoStore.saveOrUpdatePhoto(): " + message);
throw new InvalidDataException(message);
}
// Create the FluxtreamCapturePhoto (this validates the photo, generates the hash and thumbnails, etc.)
final FluxtreamCapturePhoto photo;
try {
photo = new FluxtreamCapturePhoto(guestId, photoBytes, metadata);
}
catch (UnsupportedImageFormatException e) {
LOG.error("FluxtreamCapturePhotoStore.saveOrUpdatePhoto(): Photo upload failed because an UnsupportedOperationException occurred while trying to create the FluxtreamCapturePhoto");
throw e;
}
catch (Exception e) {
final String message = "Photo upload failed because an Exception occurred while trying to create the FluxtreamCapturePhoto";
LOG.error("FluxtreamCapturePhotoStore.saveOrUpdatePhoto(): " + message, e);
throw new InvalidDataException(message, e);
}
// Now that we have the key-value store created and everything appears to be valid, we can go ahead and
// insert the photo into the photo key-value store, but only if the key doesn't already exist
final String photoStoreKey = photo.getPhotoStoreKey();
if (!keyValueStore.hasKey(photoStoreKey)) {
if (!keyValueStore.set(photoStoreKey, photo.getPhotoBytes())) {
final String message = "Photo upload failed because the photo could not be saved to the key-value store";
LOG.error("FluxtreamCapturePhotoStore.saveOrUpdatePhoto(): " + message);
throw new StorageException(message);
}
}
// The photo is in the key-value store, so try to save or update to the DB
final PhotoCreatorOrModifier photoCreatorOrModifier = new PhotoCreatorOrModifier(photo);
final FluxtreamCapturePhotoFacet photoFacet;
try {
photoFacet = apiDataService.createOrReadModifyWrite(FluxtreamCapturePhotoFacet.class, photoCreatorOrModifier.getFacetFinderQuery(), photoCreatorOrModifier, apiKeyId);
}
catch (Exception e) {
final String message = "Photo upload failed because an Exception occurred while writing the photo to the database";
LOG.error("FluxtreamCapturePhotoStore.saveOrUpdatePhoto(): " + message, e);
throw new StorageException(message, e);
}
if (photoFacet == null) {
// attempt to remove the photo from the key-value store
keyValueStore.delete(photoStoreKey);
final String message = "Upload failed because the ApiDataService failed to save the facet and returned null";
LOG.error("FluxtreamCapturePhotoStore.saveOrUpdatePhoto(): " + message);
throw new StorageException(message);
}