use of org.datatransferproject.types.common.models.photos.PhotoModel in project data-transfer-project by google.
the class GooglePhotosImporterTest method importPhotoInTempStore.
@Test
public void importPhotoInTempStore() throws Exception {
PhotoModel photoModel = new PhotoModel(PHOTO_TITLE, IMG_URI, PHOTO_DESCRIPTION, JPEG_MEDIA_TYPE, "oldPhotoID1", OLD_ALBUM_ID, true);
Mockito.when(googlePhotosInterface.uploadPhotoContent(any())).thenReturn("token1");
JobStore jobStore = Mockito.mock(LocalJobStore.class);
Mockito.when(jobStore.getStream(any(), any())).thenReturn(new TemporaryPerJobDataStore.InputStreamWrapper(new ByteArrayInputStream("TestingBytes".getBytes())));
Mockito.doNothing().when(jobStore).removeData(any(), anyString());
GooglePhotosImporter googlePhotosImporter = new GooglePhotosImporter(null, jobStore, null, null, googlePhotosInterface, null, null, 1.0);
BatchMediaItemResponse batchMediaItemResponse = new BatchMediaItemResponse(new NewMediaItemResult[] { buildMediaItemResult("token1", Code.OK_VALUE) });
Mockito.when(googlePhotosInterface.createPhotos(any(NewMediaItemUpload.class))).thenReturn(batchMediaItemResponse);
UUID jobId = UUID.randomUUID();
long length = googlePhotosImporter.importPhotoBatch(jobId, Mockito.mock(TokensAndUrlAuthData.class), Lists.newArrayList(photoModel), executor, NEW_ALBUM_ID);
assertTrue(executor.isKeyCached(String.format("%s-%s", OLD_ALBUM_ID, "oldPhotoID1")));
Mockito.verify(jobStore, Mockito.times(1)).removeData(any(), anyString());
Mockito.verify(jobStore, Mockito.times(1)).getStream(any(), anyString());
}
use of org.datatransferproject.types.common.models.photos.PhotoModel in project data-transfer-project by google.
the class GooglePhotosImporterTest method importTwoPhotos.
@Test
public void importTwoPhotos() throws Exception {
PhotoModel photoModel1 = new PhotoModel(PHOTO_TITLE, IMG_URI, PHOTO_DESCRIPTION, JPEG_MEDIA_TYPE, "oldPhotoID1", OLD_ALBUM_ID, false);
PhotoModel photoModel2 = new PhotoModel(PHOTO_TITLE, IMG_URI, PHOTO_DESCRIPTION, JPEG_MEDIA_TYPE, "oldPhotoID2", OLD_ALBUM_ID, false);
Mockito.when(googlePhotosInterface.uploadPhotoContent(any())).thenReturn("token1", "token2");
BatchMediaItemResponse batchMediaItemResponse = new BatchMediaItemResponse(new NewMediaItemResult[] { buildMediaItemResult("token1", Code.OK_VALUE), buildMediaItemResult("token2", Code.OK_VALUE) });
Mockito.when(googlePhotosInterface.createPhotos(any(NewMediaItemUpload.class))).thenReturn(batchMediaItemResponse);
long length = googlePhotosImporter.importPhotoBatch(UUID.randomUUID(), Mockito.mock(TokensAndUrlAuthData.class), Lists.newArrayList(photoModel1, photoModel2), executor, NEW_ALBUM_ID);
// Two photos of 32L each imported
assertEquals(64L, length);
assertTrue(executor.isKeyCached(String.format("%s-%s", OLD_ALBUM_ID, "oldPhotoID1")));
assertTrue(executor.isKeyCached(String.format("%s-%s", OLD_ALBUM_ID, "oldPhotoID2")));
}
use of org.datatransferproject.types.common.models.photos.PhotoModel in project data-transfer-project by google.
the class FlickrPhotosImporter method importItem.
@Override
public ImportResult importItem(UUID jobId, IdempotentImportExecutor idempotentExecutor, AuthData authData, PhotosContainerResource data) throws Exception, IOException {
Auth auth;
try {
auth = FlickrUtils.getAuth(authData, flickr);
} catch (FlickrException e) {
return new ImportResult(e);
}
RequestContext.getRequestContext().setAuth(auth);
Preconditions.checkArgument(data.getAlbums() != null || data.getPhotos() != null, "Error: There is no data to import");
if (data.getAlbums() != null) {
storeAlbums(jobId, data.getAlbums());
}
if (data.getPhotos() != null) {
for (PhotoModel photo : data.getPhotos()) {
try {
importSinglePhoto(idempotentExecutor, jobId, photo);
} catch (FlickrException e) {
if (e.getMessage().contains("Upload limit reached")) {
throw new DestinationMemoryFullException("Flickr destination memory reached", e);
} else if (e.getMessage().contains("Photo already in set")) {
// uploaded
continue;
}
throw new IOException(e);
}
}
}
return new ImportResult(ImportResult.ResultType.OK);
}
use of org.datatransferproject.types.common.models.photos.PhotoModel 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);
}
use of org.datatransferproject.types.common.models.photos.PhotoModel 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));
}
Aggregations