use of org.dataportabilityproject.spi.transfer.types.TempPhotosData in project data-transfer-project by google.
the class GooglePhotosImporter method importSingleAlbum.
@VisibleForTesting
void importSingleAlbum(UUID jobId, TokensAndUrlAuthData authData, PhotoAlbum inputAlbum) throws IOException, ServiceException {
// Set up album
AlbumEntry outputAlbum = new AlbumEntry();
outputAlbum.setTitle(new PlainTextConstruct("copy of " + inputAlbum.getName()));
outputAlbum.setDescription(new PlainTextConstruct(inputAlbum.getDescription()));
// Upload album
AlbumEntry insertedEntry = getOrCreatePhotosService(authData).insert(new URL(ALBUM_POST_URL), outputAlbum);
// Put new album ID in job store so photos can be assigned to the correct album
TempPhotosData photoMappings = jobStore.findData(TempPhotosData.class, jobId);
if (photoMappings == null) {
photoMappings = new TempPhotosData(jobId);
jobStore.create(jobId, photoMappings);
}
photoMappings.addAlbumId(inputAlbum.getId(), insertedEntry.getGphotoId());
jobStore.update(jobId, photoMappings);
}
use of org.dataportabilityproject.spi.transfer.types.TempPhotosData in project data-transfer-project by google.
the class FlickrPhotosImporter method importItem.
@Override
public ImportResult importItem(UUID jobId, AuthData authData, PhotosContainerResource data) {
Auth auth;
try {
auth = FlickrUtils.getAuth(authData, flickr);
} catch (FlickrException e) {
return new ImportResult(ImportResult.ResultType.ERROR, "Error authorizing Flickr Auth: " + e.getErrorMessage());
}
RequestContext.getRequestContext().setAuth(auth);
// Store any album data in the cache because Flickr only allows you to create an album with a
// photo in it, so we have to wait for the first photo to create the album
TempPhotosData tempPhotosData = jobStore.findData(TempPhotosData.class, jobId);
if (tempPhotosData == null) {
tempPhotosData = new TempPhotosData(jobId);
jobStore.create(jobId, tempPhotosData);
}
for (PhotoAlbum album : data.getAlbums()) {
tempPhotosData.addAlbum(CACHE_ALBUM_METADATA_PREFIX + album.getId(), album);
}
jobStore.update(jobId, tempPhotosData);
for (PhotoModel photo : data.getPhotos()) {
try {
String photoId = uploadPhoto(photo);
String oldAlbumId = photo.getAlbumId();
TempPhotosData tempData = jobStore.findData(TempPhotosData.class, jobId);
String newAlbumId = tempData.lookupNewAlbumId(oldAlbumId);
if (Strings.isNullOrEmpty(newAlbumId)) {
// This means that we havent created the new album yet, create the photoset
PhotoAlbum album = tempData.lookupAlbum(CACHE_ALBUM_METADATA_PREFIX + oldAlbumId);
Photoset photoset = photosetsInterface.create(COPY_PREFIX + album.getName(), album.getDescription(), photoId);
tempData.addAlbumId(oldAlbumId, photoset.getId());
} else {
// We've already created a new album, add the photo to the new album
photosetsInterface.addPhoto(newAlbumId, photoId);
}
jobStore.update(jobId, tempData);
} catch (FlickrException | IOException e) {
// TODO: figure out retries
return new ImportResult(ImportResult.ResultType.ERROR, "Error importing item: " + e.getMessage());
}
}
return new ImportResult(ImportResult.ResultType.OK);
}
use of org.dataportabilityproject.spi.transfer.types.TempPhotosData in project data-transfer-project by google.
the class FlickrPhotosImporterTest method importStoresAlbumInJobStore.
@Test
public void importStoresAlbumInJobStore() throws FlickrException, IOException {
UUID jobId = UUID.randomUUID();
PhotosContainerResource photosContainerResource = new PhotosContainerResource(Collections.singletonList(PHOTO_ALBUM), Collections.singletonList(PHOTO_MODEL));
// Setup Mock
when(user.getId()).thenReturn("userId");
when(authInterface.checkToken(any(Token.class))).thenReturn(auth);
when(flickr.getPhotosetsInterface()).thenReturn(photosetsInterface);
when(flickr.getUploader()).thenReturn(uploader);
when(flickr.getAuthInterface()).thenReturn(authInterface);
when(imageStreamProvider.get(FETCHABLE_URL)).thenReturn(bufferedInputStream);
when(uploader.upload(any(BufferedInputStream.class), any(UploadMetaData.class))).thenReturn(FLICKR_PHOTO_ID);
String flickrAlbumTitle = FlickrPhotosImporter.COPY_PREFIX + ALBUM_NAME;
Photoset photoset = FlickrTestUtils.initializePhotoset(FLICKR_ALBUM_ID, ALBUM_DESCRIPTION, FLICKR_PHOTO_ID);
when(photosetsInterface.create(flickrAlbumTitle, ALBUM_DESCRIPTION, FLICKR_PHOTO_ID)).thenReturn(photoset);
// Run test
FlickrPhotosImporter importer = new FlickrPhotosImporter(flickr, jobStore, imageStreamProvider);
ImportResult result = importer.importItem(jobId, new TokenSecretAuthData("token", "secret"), photosContainerResource);
// Verify that the image stream provider got the correct URL and that the correct info was uploaded
verify(imageStreamProvider).get(FETCHABLE_URL);
ArgumentCaptor<UploadMetaData> uploadMetaDataArgumentCaptor = ArgumentCaptor.forClass(UploadMetaData.class);
verify(uploader).upload(eq(bufferedInputStream), uploadMetaDataArgumentCaptor.capture());
UploadMetaData actualUploadMetaData = uploadMetaDataArgumentCaptor.getValue();
assertThat(actualUploadMetaData.getTitle()).isEqualTo(FlickrPhotosImporter.COPY_PREFIX + PHOTO_TITLE);
assertThat(actualUploadMetaData.getDescription()).isEqualTo(PHOTO_DESCRIPTION);
// Verify the photosets interface got the command to create the correct album
verify(photosetsInterface).create(flickrAlbumTitle, ALBUM_DESCRIPTION, FLICKR_PHOTO_ID);
// Check contents of JobStore
TempPhotosData tempPhotosData = jobStore.findData(TempPhotosData.class, jobId);
assertThat(tempPhotosData).isNotNull();
String expectedAlbumKey = FlickrPhotosImporter.CACHE_ALBUM_METADATA_PREFIX + ALBUM_ID;
assertThat(tempPhotosData.lookupAlbum(expectedAlbumKey)).isNotNull();
assertThat(tempPhotosData.lookupAlbum(expectedAlbumKey)).isEqualTo(PHOTO_ALBUM);
assertThat(tempPhotosData.lookupNewAlbumId(ALBUM_ID)).isEqualTo(FLICKR_ALBUM_ID);
}
use of org.dataportabilityproject.spi.transfer.types.TempPhotosData in project data-transfer-project by google.
the class SmugMugPhotosImporter method importSingleAlbum.
@VisibleForTesting
void importSingleAlbum(UUID jobId, String folder, PhotoAlbum inputAlbum) throws IOException {
// Set up album
Map<String, String> json = new HashMap<>();
String niceName = "Copy-" + inputAlbum.getName().replace(' ', '-');
json.put("UrlName", niceName);
// Allow conflicting names to be changed
json.put("AutoRename", "true");
json.put("Name", "Copy of " + inputAlbum.getName());
// All imported content is private by default.
json.put("Privacy", "Private");
HttpContent content = new JsonHttpContent(new JacksonFactory(), json);
// Upload album
SmugMugResponse<SmugMugAlbumResponse> response = smugMugInterface.postRequest(folder + "!albums", content, ImmutableMap.of(), new TypeReference<SmugMugResponse<SmugMugAlbumResponse>>() {
});
checkState(response.getResponse() != null, "Response is null");
checkState(response.getResponse().getAlbum() != null, "Album is null");
// Put new album ID in job store so photos can be assigned to correct album
// TODO(olsona): thread safety!
TempPhotosData tempPhotosData = jobStore.findData(TempPhotosData.class, jobId);
if (tempPhotosData == null) {
tempPhotosData = new TempPhotosData(jobId);
jobStore.create(jobId, tempPhotosData);
}
tempPhotosData.addAlbumId(inputAlbum.getId(), response.getResponse().getAlbum().getAlbumKey());
}
Aggregations