Search in sources :

Example 6 with MediaItemSearchResponse

use of org.datatransferproject.datatransfer.google.mediaModels.MediaItemSearchResponse 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);
}
Also used : PaginationData(org.datatransferproject.types.common.PaginationData) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) MediaItemSearchResponse(org.datatransferproject.datatransfer.google.mediaModels.MediaItemSearchResponse) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) GoogleMediaItem(org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 7 with MediaItemSearchResponse

use of org.datatransferproject.datatransfer.google.mediaModels.MediaItemSearchResponse 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);
}
Also used : TempPhotosData(org.datatransferproject.spi.transfer.types.TempPhotosData) AlbumListResponse(org.datatransferproject.datatransfer.google.mediaModels.AlbumListResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) GoogleMediaItem(org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem) GoogleAlbum(org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum) MediaItemSearchResponse(org.datatransferproject.datatransfer.google.mediaModels.MediaItemSearchResponse) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

MediaItemSearchResponse (org.datatransferproject.datatransfer.google.mediaModels.MediaItemSearchResponse)7 GoogleMediaItem (org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 InputStream (java.io.InputStream)3 Optional (java.util.Optional)3 AlbumListResponse (org.datatransferproject.datatransfer.google.mediaModels.AlbumListResponse)3 TempPhotosData (org.datatransferproject.spi.transfer.types.TempPhotosData)3 GoogleCredentialFactory (org.datatransferproject.datatransfer.google.common.GoogleCredentialFactory)2 TemporaryPerJobDataStore (org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore)2 InputStreamWrapper (org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore.InputStreamWrapper)2 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)2 ResultType (org.datatransferproject.spi.transfer.provider.ExportResult.ResultType)2 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)2 PaginationData (org.datatransferproject.types.common.PaginationData)2 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)2 PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)2 Before (org.junit.Before)2 Test (org.junit.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1