use of org.datatransferproject.datatransfer.google.mediaModels.NewMediaItemUpload in project data-transfer-project by google.
the class GooglePhotosImporter method importPhotoBatch.
long importPhotoBatch(UUID jobId, TokensAndUrlAuthData authData, List<PhotoModel> photos, IdempotentImportExecutor executor, String albumId) throws Exception {
final ArrayList<NewMediaItem> mediaItems = new ArrayList<>();
final HashMap<String, PhotoModel> uploadTokenToDataId = new HashMap<>();
final HashMap<String, Long> uploadTokenToLength = new HashMap<>();
// this however, seems to require knowledge of the total file size.
for (PhotoModel photo : photos) {
try {
Pair<InputStream, Long> inputStreamBytesPair = getInputStreamForUrl(jobId, photo.getFetchableUrl(), photo.isInTempStore());
try (InputStream s = inputStreamBytesPair.getFirst()) {
String uploadToken = getOrCreatePhotosInterface(jobId, authData).uploadPhotoContent(s);
mediaItems.add(new NewMediaItem(cleanDescription(photo.getDescription()), uploadToken));
uploadTokenToDataId.put(uploadToken, photo);
uploadTokenToLength.put(uploadToken, inputStreamBytesPair.getSecond());
}
try {
if (photo.isInTempStore()) {
jobStore.removeData(jobId, photo.getFetchableUrl());
}
} catch (Exception e) {
// Swallow the exception caused by Remove data so that existing flows continue
monitor.info(() -> format("%s: Exception swallowed in removeData call for localPath %s", jobId, photo.getFetchableUrl()), e);
}
} catch (IOException e) {
executor.executeAndSwallowIOExceptions(IdempotentImportExecutorHelper.getPhotoIdempotentId(photo), photo.getTitle(), () -> {
throw e;
});
}
}
if (mediaItems.isEmpty()) {
// Either we were not passed in any videos or we failed upload on all of them.
return 0L;
}
long totalBytes = 0L;
NewMediaItemUpload uploadItem = new NewMediaItemUpload(albumId, mediaItems);
try {
BatchMediaItemResponse photoCreationResponse = getOrCreatePhotosInterface(jobId, authData).createPhotos(uploadItem);
Preconditions.checkNotNull(photoCreationResponse);
NewMediaItemResult[] mediaItemResults = photoCreationResponse.getResults();
Preconditions.checkNotNull(mediaItemResults);
for (NewMediaItemResult mediaItem : mediaItemResults) {
PhotoModel photo = uploadTokenToDataId.get(mediaItem.getUploadToken());
totalBytes += processMediaResult(mediaItem, IdempotentImportExecutorHelper.getPhotoIdempotentId(photo), executor, photo.getTitle(), uploadTokenToLength.get(mediaItem.getUploadToken()));
uploadTokenToDataId.remove(mediaItem.getUploadToken());
}
if (!uploadTokenToDataId.isEmpty()) {
for (PhotoModel photo : uploadTokenToDataId.values()) {
executor.executeAndSwallowIOExceptions(IdempotentImportExecutorHelper.getPhotoIdempotentId(photo), photo.getTitle(), () -> {
throw new IOException("Photo was missing from results list.");
});
}
}
} catch (IOException e) {
if (e.getMessage() != null && e.getMessage().contains("The remaining storage in the user's account is not enough")) {
throw new DestinationMemoryFullException("Google destination storage full", e);
} else {
throw e;
}
}
return totalBytes;
}
Aggregations