Search in sources :

Example 1 with GoogleMediaItem

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

the class GooglePhotosExporter method convertPhotosList.

private List<PhotoModel> convertPhotosList(Optional<String> albumId, GoogleMediaItem[] mediaItems, UUID jobId) throws IOException {
    List<PhotoModel> photos = new ArrayList<>(mediaItems.length);
    TempPhotosData tempPhotosData = null;
    InputStream stream = jobStore.getStream(jobId, createCacheKey()).getStream();
    if (stream != null) {
        tempPhotosData = new ObjectMapper().readValue(stream, TempPhotosData.class);
        stream.close();
    }
    for (GoogleMediaItem mediaItem : mediaItems) {
        if (mediaItem.getMediaMetadata().getPhoto() != null) {
            // TODO: address videos
            boolean shouldUpload = albumId.isPresent();
            if (tempPhotosData != null) {
                shouldUpload = shouldUpload || !tempPhotosData.isContainedPhotoId(mediaItem.getId());
            }
            if (shouldUpload) {
                PhotoModel photoModel = convertToPhotoModel(albumId, mediaItem);
                photos.add(photoModel);
                monitor.debug(() -> String.format("%s: Google exporting photo: %s", jobId, photoModel.getDataId()));
            }
        }
    }
    return photos;
}
Also used : TempPhotosData(org.datatransferproject.spi.transfer.types.TempPhotosData) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) ArrayList(java.util.ArrayList) GoogleMediaItem(org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with GoogleMediaItem

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

the class GooglePhotosExporterTest method populateContainedPhotosList.

@Test
public void populateContainedPhotosList() throws IOException, InvalidTokenException, PermissionDeniedException {
    // Set up an album with two photos
    setUpSingleAlbum();
    when(albumListResponse.getNextPageToken()).thenReturn(null);
    MediaItemSearchResponse albumMediaResponse = mock(MediaItemSearchResponse.class);
    GoogleMediaItem firstPhoto = setUpSinglePhoto(IMG_URI, PHOTO_ID);
    String secondUri = "second uri";
    String secondId = "second id";
    GoogleMediaItem secondPhoto = setUpSinglePhoto(secondUri, secondId);
    when(photosInterface.listMediaItems(eq(Optional.of(ALBUM_ID)), any(Optional.class))).thenReturn(albumMediaResponse);
    when(albumMediaResponse.getMediaItems()).thenReturn(new GoogleMediaItem[] { firstPhoto, secondPhoto });
    when(albumMediaResponse.getNextPageToken()).thenReturn(null);
    // Run test
    googlePhotosExporter.populateContainedPhotosList(uuid, null);
    // Check contents of job store
    ArgumentCaptor<InputStream> inputStreamArgumentCaptor = ArgumentCaptor.forClass(InputStream.class);
    verify(jobStore).create(eq(uuid), eq("tempPhotosData"), inputStreamArgumentCaptor.capture());
    TempPhotosData tempPhotosData = new ObjectMapper().readValue(inputStreamArgumentCaptor.getValue(), TempPhotosData.class);
    assertThat(tempPhotosData.lookupContainedPhotoIds()).containsExactly(PHOTO_ID, secondId);
}
Also used : TempPhotosData(org.datatransferproject.spi.transfer.types.TempPhotosData) Optional(java.util.Optional) InputStream(java.io.InputStream) GoogleMediaItem(org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MediaItemSearchResponse(org.datatransferproject.datatransfer.google.mediaModels.MediaItemSearchResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with GoogleMediaItem

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

the class GoogleVideosExporterTest method exportSingleVideo.

@Test
public void exportSingleVideo() throws IOException {
    when(albumListResponse.getNextPageToken()).thenReturn(null);
    GoogleMediaItem mediaItem = setUpSingleVideo(VIDEO_URI, VIDEO_ID);
    when(mediaItemSearchResponse.getMediaItems()).thenReturn(new GoogleMediaItem[] { mediaItem });
    when(mediaItemSearchResponse.getNextPageToken()).thenReturn(VIDEO_TOKEN);
    // Run test
    ExportResult<VideosContainerResource> result = googleVideosExporter.exportVideos(null, Optional.empty());
    // Verify correct methods were called
    verify(videosInterface).listVideoItems(Optional.empty());
    verify(mediaItemSearchResponse).getMediaItems();
    // Check pagination
    ContinuationData continuationData = result.getContinuationData();
    StringPaginationToken paginationToken = (StringPaginationToken) continuationData.getPaginationData();
    assertThat(paginationToken.getToken()).isEqualTo(VIDEO_TOKEN);
    // Check videos field of container
    Collection<VideoModel> actualVideos = result.getExportedData().getVideos();
    URI video_uri_object = null;
    try {
        video_uri_object = new URI(VIDEO_URI + "=dv");
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    assertThat(actualVideos.stream().map(VideoModel::getContentUrl).collect(Collectors.toList())).containsExactly(video_uri_object);
    // Since albums are not supported atm, this should be null
    assertThat(actualVideos.stream().map(VideoModel::getAlbumId).collect(Collectors.toList())).containsExactly((Object) null);
}
Also used : VideosContainerResource(org.datatransferproject.types.common.models.videos.VideosContainerResource) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) GoogleMediaItem(org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem) URISyntaxException(java.net.URISyntaxException) VideoModel(org.datatransferproject.types.common.models.videos.VideoModel) URI(java.net.URI) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) Test(org.junit.Test)

Example 4 with GoogleMediaItem

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

the class GoogleVideosExporter method exportVideos.

@VisibleForTesting
ExportResult<VideosContainerResource> exportVideos(TokensAndUrlAuthData authData, Optional<StringPaginationToken> paginationData) throws IOException {
    Optional<String> paginationToken = paginationData.map(StringPaginationToken::getToken);
    MediaItemSearchResponse mediaItemSearchResponse = getOrCreateVideosInterface(authData).listVideoItems(paginationToken);
    PaginationData nextPageData = null;
    if (!Strings.isNullOrEmpty(mediaItemSearchResponse.getNextPageToken())) {
        nextPageData = new StringPaginationToken(mediaItemSearchResponse.getNextPageToken());
    }
    ContinuationData continuationData = new ContinuationData(nextPageData);
    VideosContainerResource containerResource = null;
    GoogleMediaItem[] mediaItems = mediaItemSearchResponse.getMediaItems();
    if (mediaItems != null && mediaItems.length > 0) {
        List<VideoModel> videos = convertVideosList(mediaItems);
        containerResource = new VideosContainerResource(null, videos);
    }
    ResultType resultType = ResultType.CONTINUE;
    if (nextPageData == null) {
        resultType = ResultType.END;
    }
    return new ExportResult<>(resultType, containerResource, continuationData);
}
Also used : PaginationData(org.datatransferproject.types.common.PaginationData) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) VideoModel(org.datatransferproject.types.common.models.videos.VideoModel) MediaItemSearchResponse(org.datatransferproject.datatransfer.google.mediaModels.MediaItemSearchResponse) VideosContainerResource(org.datatransferproject.types.common.models.videos.VideosContainerResource) 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 5 with GoogleMediaItem

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

the class GooglePhotosExporterTest method exportPhotoSubsequentSet.

@Test
public void exportPhotoSubsequentSet() throws IOException, InvalidTokenException, PermissionDeniedException {
    setUpSingleAlbum();
    when(albumListResponse.getNextPageToken()).thenReturn(null);
    GoogleMediaItem mediaItem = setUpSinglePhoto(IMG_URI, PHOTO_ID);
    when(mediaItemSearchResponse.getMediaItems()).thenReturn(new GoogleMediaItem[] { mediaItem });
    when(mediaItemSearchResponse.getNextPageToken()).thenReturn(null);
    StringPaginationToken inputPaginationToken = new StringPaginationToken(PHOTO_TOKEN_PREFIX + PHOTO_TOKEN);
    IdOnlyContainerResource idOnlyContainerResource = new IdOnlyContainerResource(ALBUM_ID);
    // Run test
    ExportResult<PhotosContainerResource> result = googlePhotosExporter.exportPhotos(null, Optional.of(idOnlyContainerResource), Optional.of(inputPaginationToken), uuid);
    // Check results
    // Verify correct methods were called
    verify(photosInterface).listMediaItems(Optional.of(ALBUM_ID), Optional.of(PHOTO_TOKEN));
    verify(mediaItemSearchResponse).getMediaItems();
    // Check pagination token
    ContinuationData continuationData = result.getContinuationData();
    PaginationData paginationToken = continuationData.getPaginationData();
    assertNull(paginationToken);
}
Also used : PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) PaginationData(org.datatransferproject.types.common.PaginationData) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) GoogleMediaItem(org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) Test(org.junit.Test)

Aggregations

GoogleMediaItem (org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem)13 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)6 MediaItemSearchResponse (org.datatransferproject.datatransfer.google.mediaModels.MediaItemSearchResponse)5 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)5 PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)5 Test (org.junit.Test)5 InputStream (java.io.InputStream)4 TempPhotosData (org.datatransferproject.spi.transfer.types.TempPhotosData)4 PhotoModel (org.datatransferproject.types.common.models.photos.PhotoModel)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)3 PaginationData (org.datatransferproject.types.common.PaginationData)3 IdOnlyContainerResource (org.datatransferproject.types.common.models.IdOnlyContainerResource)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArrayList (java.util.ArrayList)2 GoogleAlbum (org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum)2 MediaMetadata (org.datatransferproject.datatransfer.google.mediaModels.MediaMetadata)2 ResultType (org.datatransferproject.spi.transfer.provider.ExportResult.ResultType)2 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)2