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;
}
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);
}
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");
}
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);
}
Aggregations