Search in sources :

Example 1 with AlbumListResponse

use of org.datatransferproject.datatransfer.google.mediaModels.AlbumListResponse in project data-transfer-project by google.

the class GooglePhotosExporter method exportAlbums.

/**
 * Note: not all accounts have albums to return. In that case, we just return an empty list of
 * albums instead of trying to iterate through a null list.
 */
@VisibleForTesting
ExportResult<PhotosContainerResource> exportAlbums(TokensAndUrlAuthData authData, Optional<PaginationData> paginationData, UUID jobId) throws IOException, InvalidTokenException, PermissionDeniedException {
    Optional<String> paginationToken = Optional.empty();
    if (paginationData.isPresent()) {
        String token = ((StringPaginationToken) paginationData.get()).getToken();
        Preconditions.checkArgument(token.startsWith(ALBUM_TOKEN_PREFIX), "Invalid pagination token " + token);
        paginationToken = Optional.of(token.substring(ALBUM_TOKEN_PREFIX.length()));
    }
    AlbumListResponse albumListResponse;
    albumListResponse = getOrCreatePhotosInterface(authData).listAlbums(paginationToken);
    PaginationData nextPageData;
    String token = albumListResponse.getNextPageToken();
    List<PhotoAlbum> albums = new ArrayList<>();
    GoogleAlbum[] googleAlbums = albumListResponse.getAlbums();
    if (Strings.isNullOrEmpty(token)) {
        nextPageData = new StringPaginationToken(PHOTO_TOKEN_PREFIX);
    } else {
        nextPageData = new StringPaginationToken(ALBUM_TOKEN_PREFIX + token);
    }
    ContinuationData continuationData = new ContinuationData(nextPageData);
    if (googleAlbums != null && googleAlbums.length > 0) {
        for (GoogleAlbum googleAlbum : googleAlbums) {
            // Add album info to list so album can be recreated later
            PhotoAlbum photoAlbum = new PhotoAlbum(googleAlbum.getId(), googleAlbum.getTitle(), null);
            albums.add(photoAlbum);
            monitor.debug(() -> String.format("%s: Google Photos exporting album: %s", jobId, photoAlbum.getId()));
            // Add album id to continuation data
            continuationData.addContainerResource(new IdOnlyContainerResource(googleAlbum.getId()));
        }
    }
    ResultType resultType = ResultType.CONTINUE;
    PhotosContainerResource containerResource = new PhotosContainerResource(albums, null);
    return new ExportResult<>(resultType, containerResource, continuationData);
}
Also used : AlbumListResponse(org.datatransferproject.datatransfer.google.mediaModels.AlbumListResponse) PaginationData(org.datatransferproject.types.common.PaginationData) ArrayList(java.util.ArrayList) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) GoogleAlbum(org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with AlbumListResponse

use of org.datatransferproject.datatransfer.google.mediaModels.AlbumListResponse in project data-transfer-project by google.

the class GooglePhotosExporterTest method exportAlbumFirstSet.

@Test
public void exportAlbumFirstSet() throws IOException, InvalidTokenException, PermissionDeniedException {
    setUpSingleAlbum();
    when(albumListResponse.getNextPageToken()).thenReturn(ALBUM_TOKEN);
    // Run test
    ExportResult<PhotosContainerResource> result = googlePhotosExporter.exportAlbums(null, Optional.empty(), uuid);
    // Check results
    // Verify correct methods were called
    verify(photosInterface).listAlbums(Optional.empty());
    verify(albumListResponse).getAlbums();
    // Check pagination token
    ContinuationData continuationData = result.getContinuationData();
    StringPaginationToken paginationToken = (StringPaginationToken) continuationData.getPaginationData();
    assertThat(paginationToken.getToken()).isEqualTo(ALBUM_TOKEN_PREFIX + ALBUM_TOKEN);
    // Check albums field of container
    Collection<PhotoAlbum> actualAlbums = result.getExportedData().getAlbums();
    assertThat(actualAlbums.stream().map(PhotoAlbum::getId).collect(Collectors.toList())).containsExactly(ALBUM_ID);
    // Check photos field of container (should be empty, even though there is a photo in the
    // original album)
    Collection<PhotoModel> actualPhotos = result.getExportedData().getPhotos();
    assertThat(actualPhotos).isEmpty();
    // Should be one container in the resource list
    List<ContainerResource> actualResources = continuationData.getContainerResources();
    assertThat(actualResources.stream().map(a -> ((IdOnlyContainerResource) a).getId()).collect(Collectors.toList())).containsExactly(ALBUM_ID);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ALBUM_TOKEN_PREFIX(org.datatransferproject.datatransfer.google.photos.GooglePhotosExporter.ALBUM_TOKEN_PREFIX) GoogleMediaItem(org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) PermissionDeniedException(org.datatransferproject.spi.transfer.types.PermissionDeniedException) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) AlbumListResponse(org.datatransferproject.datatransfer.google.mediaModels.AlbumListResponse) MediaMetadata(org.datatransferproject.datatransfer.google.mediaModels.MediaMetadata) TemporaryPerJobDataStore(org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore) ArgumentCaptor(org.mockito.ArgumentCaptor) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) InputStreamWrapper(org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore.InputStreamWrapper) GoogleCredentialFactory(org.datatransferproject.datatransfer.google.common.GoogleCredentialFactory) Before(org.junit.Before) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) ContainerResource(org.datatransferproject.types.common.models.ContainerResource) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) PaginationData(org.datatransferproject.types.common.PaginationData) Truth.assertThat(com.google.common.truth.Truth.assertThat) Photo(org.datatransferproject.datatransfer.google.mediaModels.Photo) Collectors(java.util.stream.Collectors) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) Mockito.verify(org.mockito.Mockito.verify) PHOTO_TOKEN_PREFIX(org.datatransferproject.datatransfer.google.photos.GooglePhotosExporter.PHOTO_TOKEN_PREFIX) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Monitor(org.datatransferproject.api.launcher.Monitor) MediaItemSearchResponse(org.datatransferproject.datatransfer.google.mediaModels.MediaItemSearchResponse) TempPhotosData(org.datatransferproject.spi.transfer.types.TempPhotosData) Optional(java.util.Optional) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) GoogleAlbum(org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum) InvalidTokenException(org.datatransferproject.spi.transfer.types.InvalidTokenException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) InputStream(java.io.InputStream) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) ContainerResource(org.datatransferproject.types.common.models.ContainerResource) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) Test(org.junit.Test)

Example 3 with AlbumListResponse

use of org.datatransferproject.datatransfer.google.mediaModels.AlbumListResponse in project data-transfer-project by google.

the class GooglePhotosExporterTest method setup.

@Before
public void setup() throws IOException, InvalidTokenException, PermissionDeniedException {
    GoogleCredentialFactory credentialFactory = mock(GoogleCredentialFactory.class);
    jobStore = mock(TemporaryPerJobDataStore.class);
    when(jobStore.getStream(any(), anyString())).thenReturn(mock(InputStreamWrapper.class));
    photosInterface = mock(GooglePhotosInterface.class);
    albumListResponse = mock(AlbumListResponse.class);
    mediaItemSearchResponse = mock(MediaItemSearchResponse.class);
    Monitor monitor = mock(Monitor.class);
    googlePhotosExporter = new GooglePhotosExporter(credentialFactory, jobStore, new JacksonFactory(), photosInterface, monitor);
    when(photosInterface.listAlbums(any(Optional.class))).thenReturn(albumListResponse);
    when(photosInterface.listMediaItems(any(Optional.class), any(Optional.class))).thenReturn(mediaItemSearchResponse);
    verifyNoInteractions(credentialFactory);
}
Also used : TemporaryPerJobDataStore(org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore) AlbumListResponse(org.datatransferproject.datatransfer.google.mediaModels.AlbumListResponse) InputStreamWrapper(org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore.InputStreamWrapper) Monitor(org.datatransferproject.api.launcher.Monitor) Optional(java.util.Optional) GoogleCredentialFactory(org.datatransferproject.datatransfer.google.common.GoogleCredentialFactory) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) MediaItemSearchResponse(org.datatransferproject.datatransfer.google.mediaModels.MediaItemSearchResponse) Before(org.junit.Before)

Example 4 with AlbumListResponse

use of org.datatransferproject.datatransfer.google.mediaModels.AlbumListResponse in project data-transfer-project by google.

the class GoogleVideosExporterTest method setup.

@Before
public void setup() throws IOException {
    GoogleCredentialFactory credentialFactory = mock(GoogleCredentialFactory.class);
    jobStore = mock(TemporaryPerJobDataStore.class);
    videosInterface = mock(GoogleVideosInterface.class);
    albumListResponse = mock(AlbumListResponse.class);
    mediaItemSearchResponse = mock(MediaItemSearchResponse.class);
    googleVideosExporter = new GoogleVideosExporter(credentialFactory, videosInterface);
    when(videosInterface.listVideoItems(any(Optional.class))).thenReturn(mediaItemSearchResponse);
    verifyNoInteractions(credentialFactory);
}
Also used : TemporaryPerJobDataStore(org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore) AlbumListResponse(org.datatransferproject.datatransfer.google.mediaModels.AlbumListResponse) Optional(java.util.Optional) GoogleCredentialFactory(org.datatransferproject.datatransfer.google.common.GoogleCredentialFactory) MediaItemSearchResponse(org.datatransferproject.datatransfer.google.mediaModels.MediaItemSearchResponse) Before(org.junit.Before)

Example 5 with AlbumListResponse

use of org.datatransferproject.datatransfer.google.mediaModels.AlbumListResponse 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

AlbumListResponse (org.datatransferproject.datatransfer.google.mediaModels.AlbumListResponse)5 MediaItemSearchResponse (org.datatransferproject.datatransfer.google.mediaModels.MediaItemSearchResponse)4 Optional (java.util.Optional)3 GoogleCredentialFactory (org.datatransferproject.datatransfer.google.common.GoogleCredentialFactory)3 GoogleAlbum (org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum)3 TemporaryPerJobDataStore (org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore)3 Before (org.junit.Before)3 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 InputStream (java.io.InputStream)2 Monitor (org.datatransferproject.api.launcher.Monitor)2 GoogleMediaItem (org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem)2 InputStreamWrapper (org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore.InputStreamWrapper)2 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)2 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)2 TempPhotosData (org.datatransferproject.spi.transfer.types.TempPhotosData)2 PaginationData (org.datatransferproject.types.common.PaginationData)2 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)2 IdOnlyContainerResource (org.datatransferproject.types.common.models.IdOnlyContainerResource)2 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)2