Search in sources :

Example 1 with TempPhotosData

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);
}
Also used : AlbumEntry(com.google.gdata.data.photos.AlbumEntry) TempPhotosData(org.dataportabilityproject.spi.transfer.types.TempPhotosData) PlainTextConstruct(com.google.gdata.data.PlainTextConstruct) URL(java.net.URL) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with TempPhotosData

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);
}
Also used : TempPhotosData(org.dataportabilityproject.spi.transfer.types.TempPhotosData) ImportResult(org.dataportabilityproject.spi.transfer.provider.ImportResult) FlickrException(com.flickr4java.flickr.FlickrException) Photoset(com.flickr4java.flickr.photosets.Photoset) Auth(com.flickr4java.flickr.auth.Auth) PhotoModel(org.dataportabilityproject.types.transfer.models.photos.PhotoModel) PhotoAlbum(org.dataportabilityproject.types.transfer.models.photos.PhotoAlbum) IOException(java.io.IOException)

Example 3 with TempPhotosData

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);
}
Also used : PhotosContainerResource(org.dataportabilityproject.types.transfer.models.photos.PhotosContainerResource) TempPhotosData(org.dataportabilityproject.spi.transfer.types.TempPhotosData) ImportResult(org.dataportabilityproject.spi.transfer.provider.ImportResult) TokenSecretAuthData(org.dataportabilityproject.types.transfer.auth.TokenSecretAuthData) BufferedInputStream(java.io.BufferedInputStream) Photoset(com.flickr4java.flickr.photosets.Photoset) Token(org.scribe.model.Token) UploadMetaData(com.flickr4java.flickr.uploader.UploadMetaData) UUID(java.util.UUID) Test(org.junit.Test)

Example 4 with TempPhotosData

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());
}
Also used : TempPhotosData(org.dataportabilityproject.spi.transfer.types.TempPhotosData) HashMap(java.util.HashMap) SmugMugResponse(org.dataportabilityproject.transfer.smugmug.photos.model.SmugMugResponse) SmugMugAlbumResponse(org.dataportabilityproject.transfer.smugmug.photos.model.SmugMugAlbumResponse) JsonHttpContent(com.google.api.client.http.json.JsonHttpContent) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) HttpContent(com.google.api.client.http.HttpContent) JsonHttpContent(com.google.api.client.http.json.JsonHttpContent) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

TempPhotosData (org.dataportabilityproject.spi.transfer.types.TempPhotosData)4 Photoset (com.flickr4java.flickr.photosets.Photoset)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ImportResult (org.dataportabilityproject.spi.transfer.provider.ImportResult)2 FlickrException (com.flickr4java.flickr.FlickrException)1 Auth (com.flickr4java.flickr.auth.Auth)1 UploadMetaData (com.flickr4java.flickr.uploader.UploadMetaData)1 HttpContent (com.google.api.client.http.HttpContent)1 JsonHttpContent (com.google.api.client.http.json.JsonHttpContent)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 PlainTextConstruct (com.google.gdata.data.PlainTextConstruct)1 AlbumEntry (com.google.gdata.data.photos.AlbumEntry)1 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 UUID (java.util.UUID)1 SmugMugAlbumResponse (org.dataportabilityproject.transfer.smugmug.photos.model.SmugMugAlbumResponse)1 SmugMugResponse (org.dataportabilityproject.transfer.smugmug.photos.model.SmugMugResponse)1 TokenSecretAuthData (org.dataportabilityproject.types.transfer.auth.TokenSecretAuthData)1