Search in sources :

Example 1 with TempPhotosData

use of org.datatransferproject.spi.transfer.types.TempPhotosData 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 TempPhotosData

use of org.datatransferproject.spi.transfer.types.TempPhotosData 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 TempPhotosData

use of org.datatransferproject.spi.transfer.types.TempPhotosData in project data-transfer-project by google.

the class GooglePhotosExporterTest method onlyExportAlbumlessPhoto.

@Test
public /* Tests that when there is no album information passed along to exportPhotos, only albumless
  photos are exported.
  */
void onlyExportAlbumlessPhoto() throws IOException, InvalidTokenException, PermissionDeniedException {
    // Set up - two photos will be returned by a media item search without an album id, but one of
    // them will have already been put into the list of contained photos
    String containedPhotoUri = "contained photo uri";
    String containedPhotoId = "contained photo id";
    GoogleMediaItem containedPhoto = setUpSinglePhoto(containedPhotoUri, containedPhotoId);
    String albumlessPhotoUri = "albumless photo uri";
    String albumlessPhotoId = "albumless photo id";
    GoogleMediaItem albumlessPhoto = setUpSinglePhoto(albumlessPhotoUri, albumlessPhotoId);
    MediaItemSearchResponse mediaItemSearchResponse = mock(MediaItemSearchResponse.class);
    when(photosInterface.listMediaItems(eq(Optional.empty()), eq(Optional.empty()))).thenReturn(mediaItemSearchResponse);
    when(mediaItemSearchResponse.getMediaItems()).thenReturn(new GoogleMediaItem[] { containedPhoto, albumlessPhoto });
    when(mediaItemSearchResponse.getNextPageToken()).thenReturn(null);
    TempPhotosData tempPhotosData = new TempPhotosData(uuid);
    tempPhotosData.addContainedPhotoId(containedPhotoId);
    InputStream stream = GooglePhotosExporter.convertJsonToInputStream(tempPhotosData);
    when(jobStore.getStream(uuid, "tempPhotosData")).thenReturn(new InputStreamWrapper(stream));
    // Run test
    ExportResult<PhotosContainerResource> result = googlePhotosExporter.exportPhotos(null, Optional.empty(), Optional.empty(), uuid);
    // Check results
    assertThat(result.getExportedData().getPhotos().stream().map(PhotoModel::getFetchableUrl).collect(Collectors.toList())).containsExactly(// download
    albumlessPhotoUri + "=d");
}
Also used : PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) TempPhotosData(org.datatransferproject.spi.transfer.types.TempPhotosData) InputStreamWrapper(org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore.InputStreamWrapper) InputStream(java.io.InputStream) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) GoogleMediaItem(org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem) MediaItemSearchResponse(org.datatransferproject.datatransfer.google.mediaModels.MediaItemSearchResponse) Test(org.junit.Test)

Example 4 with TempPhotosData

use of org.datatransferproject.spi.transfer.types.TempPhotosData 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

InputStream (java.io.InputStream)4 GoogleMediaItem (org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem)4 TempPhotosData (org.datatransferproject.spi.transfer.types.TempPhotosData)4 MediaItemSearchResponse (org.datatransferproject.datatransfer.google.mediaModels.MediaItemSearchResponse)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Test (org.junit.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1 AlbumListResponse (org.datatransferproject.datatransfer.google.mediaModels.AlbumListResponse)1 GoogleAlbum (org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum)1 InputStreamWrapper (org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore.InputStreamWrapper)1 PhotoModel (org.datatransferproject.types.common.models.photos.PhotoModel)1 PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)1