Search in sources :

Example 16 with PhotosContainerResource

use of org.datatransferproject.types.common.models.photos.PhotosContainerResource in project data-transfer-project by google.

the class FacebookPhotosExporterTest method testSpecifiedAlbums.

@Test
public void testSpecifiedAlbums() throws CopyExceptionWithFailureReason {
    ExportResult<PhotosContainerResource> result = facebookPhotosExporter.export(uuid, new TokensAndUrlAuthData("accessToken", null, null), Optional.of(new ExportInformation(new StringPaginationToken(PHOTO_TOKEN_PREFIX), new PhotosContainerResource(Lists.newArrayList(new PhotoAlbum(ALBUM_ID, ALBUM_NAME, ALBUM_DESCRIPTION)), new ArrayList<>()))));
    assertEquals(ExportResult.ResultType.CONTINUE, result.getType());
    PhotosContainerResource exportedData = result.getExportedData();
    assertEquals(1, exportedData.getAlbums().size());
    assertEquals(new PhotoAlbum(ALBUM_ID, ALBUM_NAME, ALBUM_DESCRIPTION), exportedData.getAlbums().toArray()[0]);
    assertNull((result.getContinuationData().getPaginationData()));
    assertThat(result.getContinuationData().getContainerResources()).contains(new IdOnlyContainerResource(ALBUM_ID));
}
Also used : PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) ExportInformation(org.datatransferproject.types.common.ExportInformation) TokensAndUrlAuthData(org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData) ArrayList(java.util.ArrayList) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) Test(org.junit.Test)

Example 17 with PhotosContainerResource

use of org.datatransferproject.types.common.models.photos.PhotosContainerResource in project data-transfer-project by google.

the class FlickrPhotosExporter method exportPhotosContainer.

private ExportResult<PhotosContainerResource> exportPhotosContainer(PhotosContainerResource container) {
    ImmutableList.Builder<PhotoAlbum> albumBuilder = ImmutableList.builder();
    ImmutableList.Builder<PhotoModel> photosBuilder = ImmutableList.builder();
    List<IdOnlyContainerResource> subResources = new ArrayList<>();
    try {
        for (PhotoAlbum album : container.getAlbums()) {
            Photoset photoset = photosetsInterface.getInfo(album.getId());
            // Saving data to the album allows the target service to recreate the album structure
            albumBuilder.add(new PhotoAlbum(photoset.getId(), photoset.getTitle(), photoset.getDescription()));
            // Adding subresources tells the framework to recall export to get all the photos
            subResources.add(new IdOnlyContainerResource(photoset.getId()));
        }
        for (PhotoModel photo : container.getPhotos()) {
            Photo p = photosInterface.getInfo(photo.getDataId(), null);
            photosBuilder.add(toCommonPhoto(p, null));
        }
    } catch (FlickrException e) {
        return new ExportResult<>(e);
    }
    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);
}
Also used : FlickrException(com.flickr4java.flickr.FlickrException) ImmutableList(com.google.common.collect.ImmutableList) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) ArrayList(java.util.ArrayList) Photo(com.flickr4java.flickr.photos.Photo) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) Photoset(com.flickr4java.flickr.photosets.Photoset) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 18 with PhotosContainerResource

use of org.datatransferproject.types.common.models.photos.PhotosContainerResource in project data-transfer-project by google.

the class FlickrPhotosExporter method getPhotos.

private ExportResult<PhotosContainerResource> getPhotos(IdOnlyContainerResource resource, PaginationData paginationData) {
    String photoSetId = resource.getId();
    int page = paginationData == null ? 1 : ((IntPaginationToken) paginationData).getStart();
    PhotoList<Photo> photoSetList;
    try {
        if (Strings.isNullOrEmpty(photoSetId)) {
            RequestContext.getRequestContext().setExtras(EXTRAS);
            perUserRateLimiter.acquire();
            photoSetList = photosInterface.getNotInSet(PHOTO_PER_PAGE, page);
            RequestContext.getRequestContext().setExtras(ImmutableList.of());
        } else {
            perUserRateLimiter.acquire();
            photoSetList = photosetsInterface.getPhotos(photoSetId, ImmutableSet.copyOf(EXTRAS), 0, PHOTO_PER_PAGE, page);
        }
    } catch (FlickrException e) {
        return new ExportResult<>(e);
    }
    boolean hasMore = photoSetList.getPage() != photoSetList.getPages() && !photoSetList.isEmpty();
    Collection<PhotoModel> photos = photoSetList.stream().map(p -> toCommonPhoto(p, photoSetId)).collect(Collectors.toList());
    PaginationData newPage = null;
    if (hasMore) {
        newPage = new IntPaginationToken(page + 1);
    }
    // Get result type
    ResultType resultType = ResultType.CONTINUE;
    if (newPage == null) {
        resultType = ResultType.END;
    }
    PhotosContainerResource photosContainerResource = new PhotosContainerResource(null, photos);
    return new ExportResult<>(resultType, photosContainerResource, new ContinuationData(newPage));
}
Also used : FlickrException(com.flickr4java.flickr.FlickrException) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) Photoset(com.flickr4java.flickr.photosets.Photoset) AppCredentials(org.datatransferproject.types.transfer.auth.AppCredentials) REST(com.flickr4java.flickr.REST) TransferServiceConfig(org.datatransferproject.types.transfer.serviceconfig.TransferServiceConfig) RateLimiter(com.google.common.util.concurrent.RateLimiter) ArrayList(java.util.ArrayList) ExportInformation(org.datatransferproject.types.common.ExportInformation) Photo(com.flickr4java.flickr.photos.Photo) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) IntPaginationToken(org.datatransferproject.types.common.IntPaginationToken) Flickr(com.flickr4java.flickr.Flickr) PhotoList(com.flickr4java.flickr.photos.PhotoList) Auth(com.flickr4java.flickr.auth.Auth) ImmutableSet(com.google.common.collect.ImmutableSet) PhotosInterface(com.flickr4java.flickr.photos.PhotosInterface) Collection(java.util.Collection) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) Exporter(org.datatransferproject.spi.transfer.provider.Exporter) UUID(java.util.UUID) PaginationData(org.datatransferproject.types.common.PaginationData) Collectors(java.util.stream.Collectors) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) Photosets(com.flickr4java.flickr.photosets.Photosets) List(java.util.List) AuthData(org.datatransferproject.types.transfer.auth.AuthData) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) VisibleForTesting(com.google.common.annotations.VisibleForTesting) RequestContext(com.flickr4java.flickr.RequestContext) PhotosetsInterface(com.flickr4java.flickr.photosets.PhotosetsInterface) IntPaginationToken(org.datatransferproject.types.common.IntPaginationToken) PaginationData(org.datatransferproject.types.common.PaginationData) FlickrException(com.flickr4java.flickr.FlickrException) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) Photo(com.flickr4java.flickr.photos.Photo) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 19 with PhotosContainerResource

use of org.datatransferproject.types.common.models.photos.PhotosContainerResource 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 20 with PhotosContainerResource

use of org.datatransferproject.types.common.models.photos.PhotosContainerResource 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)

Aggregations

PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)57 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)37 Test (org.junit.Test)37 IdOnlyContainerResource (org.datatransferproject.types.common.models.IdOnlyContainerResource)29 PhotoModel (org.datatransferproject.types.common.models.photos.PhotoModel)29 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)27 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)21 ArrayList (java.util.ArrayList)18 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)18 UUID (java.util.UUID)14 PaginationData (org.datatransferproject.types.common.PaginationData)12 IOException (java.io.IOException)10 ExportInformation (org.datatransferproject.types.common.ExportInformation)9 ImportResult (org.datatransferproject.spi.transfer.provider.ImportResult)8 IntPaginationToken (org.datatransferproject.types.common.IntPaginationToken)8 InputStreamWrapper (org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore.InputStreamWrapper)6 ContainerResource (org.datatransferproject.types.common.models.ContainerResource)6 TokensAndUrlAuthData (org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData)6 Photoset (com.flickr4java.flickr.photosets.Photoset)5 ImmutableList (com.google.common.collect.ImmutableList)5