use of org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem in project data-transfer-project by google.
the class GooglePhotosExporter method exportPhotos.
@VisibleForTesting
ExportResult<PhotosContainerResource> exportPhotos(TokensAndUrlAuthData authData, Optional<IdOnlyContainerResource> albumData, Optional<PaginationData> paginationData, UUID jobId) throws IOException, InvalidTokenException, PermissionDeniedException {
Optional<String> albumId = Optional.empty();
if (albumData.isPresent()) {
albumId = Optional.of(albumData.get().getId());
}
Optional<String> paginationToken = getPhotosPaginationToken(paginationData);
MediaItemSearchResponse mediaItemSearchResponse = getOrCreatePhotosInterface(authData).listMediaItems(albumId, paginationToken);
PaginationData nextPageData = null;
if (!Strings.isNullOrEmpty(mediaItemSearchResponse.getNextPageToken())) {
nextPageData = new StringPaginationToken(PHOTO_TOKEN_PREFIX + mediaItemSearchResponse.getNextPageToken());
}
ContinuationData continuationData = new ContinuationData(nextPageData);
PhotosContainerResource containerResource = null;
GoogleMediaItem[] mediaItems = mediaItemSearchResponse.getMediaItems();
if (mediaItems != null && mediaItems.length > 0) {
List<PhotoModel> photos = convertPhotosList(albumId, mediaItems, jobId);
containerResource = new PhotosContainerResource(null, photos);
}
ResultType resultType = ResultType.CONTINUE;
if (nextPageData == null) {
resultType = ResultType.END;
}
return new ExportResult<>(resultType, containerResource, continuationData);
}
use of org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem in project data-transfer-project by google.
the class GooglePhotosExporter method exportPhotosContainer.
private ExportResult<PhotosContainerResource> exportPhotosContainer(PhotosContainerResource container, TokensAndUrlAuthData authData) throws IOException, InvalidTokenException, PermissionDeniedException {
ImmutableList.Builder<PhotoAlbum> albumBuilder = ImmutableList.builder();
ImmutableList.Builder<PhotoModel> photosBuilder = ImmutableList.builder();
List<IdOnlyContainerResource> subResources = new ArrayList<>();
for (PhotoAlbum album : container.getAlbums()) {
GoogleAlbum googleAlbum = getOrCreatePhotosInterface(authData).getAlbum(album.getId());
albumBuilder.add(new PhotoAlbum(googleAlbum.getId(), googleAlbum.getTitle(), null));
// Adding subresources tells the framework to recall export to get all the photos
subResources.add(new IdOnlyContainerResource(googleAlbum.getId()));
}
for (PhotoModel photo : container.getPhotos()) {
GoogleMediaItem googleMediaItem = getOrCreatePhotosInterface(authData).getMediaItem(photo.getDataId());
photosBuilder.add(convertToPhotoModel(Optional.empty(), googleMediaItem));
}
PhotosContainerResource photosContainerResource = new PhotosContainerResource(albumBuilder.build(), photosBuilder.build());
ContinuationData continuationData = new ContinuationData(null);
subResources.forEach(resource -> continuationData.addContainerResource(resource));
return new ExportResult<>(ResultType.CONTINUE, photosContainerResource, continuationData);
}
use of org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem in project data-transfer-project by google.
the class GooglePhotosExporter method populateContainedPhotosList.
/**
* Method for storing a list of all photos that are already contained in albums
*/
@VisibleForTesting
void populateContainedPhotosList(UUID jobId, TokensAndUrlAuthData authData) throws IOException, InvalidTokenException, PermissionDeniedException {
// This method is only called once at the beginning of the transfer, so we can start by
// initializing a new TempPhotosData to be store in the job store.
TempPhotosData tempPhotosData = new TempPhotosData(jobId);
String albumToken = null;
AlbumListResponse albumListResponse;
MediaItemSearchResponse containedMediaSearchResponse;
do {
albumListResponse = getOrCreatePhotosInterface(authData).listAlbums(Optional.ofNullable(albumToken));
if (albumListResponse.getAlbums() != null) {
for (GoogleAlbum album : albumListResponse.getAlbums()) {
String albumId = album.getId();
String photoToken = null;
do {
containedMediaSearchResponse = getOrCreatePhotosInterface(authData).listMediaItems(Optional.of(albumId), Optional.ofNullable(photoToken));
if (containedMediaSearchResponse.getMediaItems() != null) {
for (GoogleMediaItem mediaItem : containedMediaSearchResponse.getMediaItems()) {
tempPhotosData.addContainedPhotoId(mediaItem.getId());
}
}
photoToken = containedMediaSearchResponse.getNextPageToken();
} while (photoToken != null);
}
}
albumToken = albumListResponse.getNextPageToken();
} while (albumToken != null);
// TODO: if we see complaints about objects being too large for JobStore in other places, we
// should consider putting logic in JobStore itself to handle it
InputStream stream = convertJsonToInputStream(tempPhotosData);
jobStore.create(jobId, createCacheKey(), stream);
}
Aggregations