use of org.datatransferproject.types.common.models.photos.PhotosContainerResource in project data-transfer-project by google.
the class FlickrPhotosExporterTest method exportPhotosFromPhotoset.
@Test
public void exportPhotosFromPhotoset() throws FlickrException {
// set up auth, flickr service
when(user.getId()).thenReturn("userId");
when(authInterface.checkToken(any(Token.class))).thenReturn(auth);
when(flickr.getPhotosetsInterface()).thenReturn(photosetsInterface);
when(flickr.getPhotosInterface()).thenReturn(photosInterface);
when(flickr.getAuthInterface()).thenReturn(authInterface);
// getting photos from a set with id photosetsId and page 1
int page = 1;
String photosetsId = "photosetsId";
ExportInformation exportInformation = new ExportInformation(null, new IdOnlyContainerResource(photosetsId));
// make lots of photos and add them to PhotoList (also adding pagination information)
int numPhotos = 4;
PhotoList<Photo> photosList = new PhotoList<>();
for (int i = 0; i < numPhotos; i++) {
photosList.add(FlickrTestUtils.initializePhoto("title" + 1, "url" + i, "description" + i, MEDIA_TYPE));
}
photosList.setPage(page);
photosList.setPages(page + 1);
when(photosetsInterface.getPhotos(anyString(), anySet(), anyInt(), anyInt(), anyInt())).thenReturn(photosList);
// run test
FlickrPhotosExporter exporter = new FlickrPhotosExporter(flickr, TransferServiceConfig.getDefaultInstance());
ExportResult<PhotosContainerResource> result = exporter.export(UUID.randomUUID(), new TokenSecretAuthData("token", "secret"), Optional.of(exportInformation));
assertThat(result.getExportedData().getPhotos().size()).isEqualTo(numPhotos);
assertThat(result.getExportedData().getAlbums()).isEmpty();
ContinuationData continuationData = (ContinuationData) result.getContinuationData();
assertThat(continuationData.getContainerResources()).isEmpty();
assertThat(((IntPaginationToken) continuationData.getPaginationData()).getStart()).isEqualTo(page + 1);
}
use of org.datatransferproject.types.common.models.photos.PhotosContainerResource in project data-transfer-project by google.
the class FacebookPhotosExporterTest method testExportPhoto.
@Test
public void testExportPhoto() throws CopyExceptionWithFailureReason {
ExportResult<PhotosContainerResource> result = facebookPhotosExporter.export(uuid, new TokensAndUrlAuthData("accessToken", null, null), Optional.of(new ExportInformation(null, new IdOnlyContainerResource(ALBUM_ID))));
assertEquals(ExportResult.ResultType.END, result.getType());
PhotosContainerResource exportedData = result.getExportedData();
assertEquals(1, exportedData.getPhotos().size());
assertEquals(new PhotoModel(PHOTO_ID + ".jpg", PHOTO_ID, PHOTO_NAME, "image/jpg", PHOTO_ID, ALBUM_ID, false, PHOTO_TIME), exportedData.getPhotos().toArray()[0]);
}
use of org.datatransferproject.types.common.models.photos.PhotosContainerResource in project data-transfer-project by google.
the class FlickrPhotosExporter method export.
@Override
public ExportResult<PhotosContainerResource> export(UUID jobId, AuthData authData, Optional<ExportInformation> exportInformation) {
Auth auth;
try {
auth = FlickrUtils.getAuth(authData, flickr);
} catch (FlickrException e) {
return new ExportResult<>(e);
}
RequestContext.getRequestContext().setAuth(auth);
// in that container instead of the whole user library
if (exportInformation.isPresent() && exportInformation.get().getContainerResource() instanceof PhotosContainerResource) {
return exportPhotosContainer((PhotosContainerResource) exportInformation.get().getContainerResource());
}
PaginationData paginationData = exportInformation.isPresent() ? exportInformation.get().getPaginationData() : null;
IdOnlyContainerResource resource = exportInformation.isPresent() ? (IdOnlyContainerResource) exportInformation.get().getContainerResource() : null;
if (resource != null) {
return getPhotos(resource, paginationData);
} else {
return getAlbums(paginationData, auth);
}
}
use of org.datatransferproject.types.common.models.photos.PhotosContainerResource in project data-transfer-project by google.
the class FlickrPhotosExporter method getAlbums.
private ExportResult<PhotosContainerResource> getAlbums(PaginationData paginationData, Auth auth) {
ImmutableList.Builder<PhotoAlbum> albumBuilder = ImmutableList.builder();
List<IdOnlyContainerResource> subResources = new ArrayList<>();
int page = paginationData == null ? 1 : ((IntPaginationToken) paginationData).getStart();
Photosets photoSetList;
try {
perUserRateLimiter.acquire();
photoSetList = photosetsInterface.getList(auth.getUser().getId(), PHOTO_SETS_PER_PAGE, page, PHOTOSET_EXTRAS);
} catch (FlickrException e) {
return new ExportResult<>(e);
}
for (Photoset photoSet : photoSetList.getPhotosets()) {
// 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()));
}
PaginationData newPage = null;
boolean hasMore = photoSetList.getPage() != photoSetList.getPages() && !photoSetList.getPhotosets().isEmpty();
if (hasMore) {
newPage = new IntPaginationToken(page + 1);
} else {
// No more albums to get, add a resource for albumless items
subResources.add(new IdOnlyContainerResource(""));
}
PhotosContainerResource photosContainerResource = new PhotosContainerResource(albumBuilder.build(), null);
ContinuationData continuationData = new ContinuationData(newPage);
subResources.forEach(resource -> continuationData.addContainerResource(resource));
// Get result type
ResultType resultType = ResultType.CONTINUE;
if (newPage == null) {
resultType = ResultType.END;
}
return new ExportResult<>(resultType, photosContainerResource, continuationData);
}
use of org.datatransferproject.types.common.models.photos.PhotosContainerResource in project data-transfer-project by google.
the class GooglePhotosExporter method export.
@Override
public ExportResult<PhotosContainerResource> export(UUID jobId, TokensAndUrlAuthData authData, Optional<ExportInformation> exportInformation) throws IOException, InvalidTokenException, PermissionDeniedException {
if (!exportInformation.isPresent()) {
// Make list of photos contained in albums so they are not exported twice later on
populateContainedPhotosList(jobId, authData);
return exportAlbums(authData, Optional.empty(), jobId);
} else if (exportInformation.get().getContainerResource() instanceof PhotosContainerResource) {
// in that container instead of the whole user library
return exportPhotosContainer((PhotosContainerResource) exportInformation.get().getContainerResource(), authData);
}
/*
* Use the export information to determine whether this export call should export albums or
* photos.
*
* Albums are exported if and only if the export information doesn't hold an album
* already, and the pagination token begins with the album prefix. There must be a pagination
* token for album export since this is isn't the first export operation performed (if it was,
* there wouldn't be any export information at all).
*
* Otherwise, photos are exported. If photos are exported, there may or may not be pagination
* information, and there may or may not be album information. If there is no container
* resource, that means that we're exporting albumless photos and a pagination token must be
* present. The beginning step of exporting albumless photos is indicated by a pagination token
* containing only PHOTO_TOKEN_PREFIX with no token attached, in order to differentiate this
* case for the first step of export (no export information at all).
*/
StringPaginationToken paginationToken = (StringPaginationToken) exportInformation.get().getPaginationData();
IdOnlyContainerResource idOnlyContainerResource = (IdOnlyContainerResource) exportInformation.get().getContainerResource();
boolean containerResourcePresent = idOnlyContainerResource != null;
boolean paginationDataPresent = paginationToken != null;
if (!containerResourcePresent && paginationDataPresent && paginationToken.getToken().startsWith(ALBUM_TOKEN_PREFIX)) {
return exportAlbums(authData, Optional.of(paginationToken), jobId);
} else {
return exportPhotos(authData, Optional.ofNullable(idOnlyContainerResource), Optional.ofNullable(paginationToken), jobId);
}
}
Aggregations