Search in sources :

Example 1 with PhotosContainerResource

use of org.datatransferproject.types.common.models.photos.PhotosContainerResource in project data-transfer-project by google.

the class FacebookPhotosExporter method export.

@Override
public ExportResult<PhotosContainerResource> export(UUID jobId, TokensAndUrlAuthData authData, Optional<ExportInformation> exportInformation) throws CopyExceptionWithFailureReason {
    Preconditions.checkNotNull(authData);
    if (!exportInformation.isPresent()) {
        // Start by getting the list of albums to export
        return exportAlbums(authData, Optional.empty());
    }
    StringPaginationToken paginationToken = (StringPaginationToken) exportInformation.get().getPaginationData();
    ContainerResource containerResource = exportInformation.get().getContainerResource();
    boolean containerResourcePresent = containerResource != null;
    boolean paginationDataPresent = paginationToken != null;
    if (!containerResourcePresent && paginationDataPresent && paginationToken.getToken().startsWith(ALBUM_TOKEN_PREFIX)) {
        // Continue exporting albums
        return exportAlbums(authData, Optional.of(paginationToken));
    } else if (containerResourcePresent && containerResource instanceof PhotosContainerResource) {
        // We have had albums specified from the front end so process them for import
        PhotosContainerResource photosContainerResource = (PhotosContainerResource) containerResource;
        Preconditions.checkNotNull(photosContainerResource.getAlbums());
        ContinuationData continuationData = new ContinuationData(null);
        for (PhotoAlbum album : photosContainerResource.getAlbums()) {
            continuationData.addContainerResource(new IdOnlyContainerResource(album.getId()));
        }
        return new ExportResult<>(ExportResult.ResultType.CONTINUE, photosContainerResource, continuationData);
    } else if (containerResourcePresent && containerResource instanceof IdOnlyContainerResource) {
        // Export photos
        return exportPhotos(jobId, authData, (IdOnlyContainerResource) containerResource, Optional.ofNullable(paginationToken));
    } else {
        throw new IllegalStateException(String.format("Invalid state passed into FacebookPhotosExporter. ExportInformation: %s", exportInformation));
    }
}
Also used : PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) ContainerResource(org.datatransferproject.types.common.models.ContainerResource) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken)

Example 2 with PhotosContainerResource

use of org.datatransferproject.types.common.models.photos.PhotosContainerResource in project data-transfer-project by google.

the class FacebookPhotosExporter method exportAlbums.

private ExportResult<PhotosContainerResource> exportAlbums(TokensAndUrlAuthData authData, Optional<StringPaginationToken> paginationData) throws CopyExceptionWithFailureReason {
    Optional<String> paginationToken = stripTokenPrefix(paginationData, ALBUM_TOKEN_PREFIX);
    // Get albums
    Connection<Album> connection = getOrCreatePhotosInterface(authData).getAlbums(paginationToken);
    PaginationData nextPageData = null;
    String token = connection.getAfterCursor();
    if (!Strings.isNullOrEmpty(token)) {
        nextPageData = new StringPaginationToken(ALBUM_TOKEN_PREFIX + token);
    }
    ContinuationData continuationData = new ContinuationData(nextPageData);
    List<Album> albums = connection.getData();
    if (albums.isEmpty()) {
        return new ExportResult<>(ExportResult.ResultType.END, null, null);
    }
    ArrayList<PhotoAlbum> exportAlbums = new ArrayList<>();
    for (Album album : albums) {
        exportAlbums.add(new PhotoAlbum(album.getId(), album.getName(), album.getDescription()));
        continuationData.addContainerResource(new IdOnlyContainerResource(album.getId()));
    }
    return new ExportResult<>(ExportResult.ResultType.CONTINUE, new PhotosContainerResource(exportAlbums, null), continuationData);
}
Also used : PaginationData(org.datatransferproject.types.common.PaginationData) ArrayList(java.util.ArrayList) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) Album(com.restfb.types.Album) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 3 with PhotosContainerResource

use of org.datatransferproject.types.common.models.photos.PhotosContainerResource in project data-transfer-project by google.

the class FacebookPhotosExporter method exportPhotos.

private ExportResult<PhotosContainerResource> exportPhotos(UUID jobId, TokensAndUrlAuthData authData, IdOnlyContainerResource containerResource, Optional<StringPaginationToken> paginationData) throws CopyExceptionWithFailureReason {
    Optional<String> paginationToken = stripTokenPrefix(paginationData, PHOTO_TOKEN_PREFIX);
    String albumId = containerResource.getId();
    try {
        Connection<Photo> photoConnection = getOrCreatePhotosInterface(authData).getPhotos(albumId, paginationToken);
        List<Photo> photos = photoConnection.getData();
        if (photos.isEmpty()) {
            return new ExportResult<>(ExportResult.ResultType.END, null);
        }
        ArrayList<PhotoModel> exportPhotos = new ArrayList<>();
        for (Photo photo : photos) {
            final String url = photo.getImages().get(0).getSource();
            final String fbid = photo.getId();
            if (null == url || url.isEmpty()) {
                monitor.severe(() -> String.format("Source was missing or empty for photo %s", fbid));
                continue;
            }
            boolean photoWasGarbage;
            try {
                photoWasGarbage = modifyExifAndStorePhoto(jobId, photo, url, photo.getId());
            } catch (IOException e) {
                monitor.info(() -> String.format("Error while modifying exif or storing photo %s", fbid), e);
                photoWasGarbage = true;
            }
            if (photoWasGarbage) {
                continue;
            }
            exportPhotos.add(new PhotoModel(String.format("%s.jpg", photo.getId()), // store and the url is too long for that.
            photo.getId(), photo.getName(), "image/jpg", photo.getId(), albumId, true, photo.getCreatedTime()));
        }
        String token = photoConnection.getAfterCursor();
        if (Strings.isNullOrEmpty(token)) {
            return new ExportResult<>(ExportResult.ResultType.END, new PhotosContainerResource(null, exportPhotos));
        } else {
            PaginationData nextPageData = new StringPaginationToken(PHOTO_TOKEN_PREFIX + token);
            ContinuationData continuationData = new ContinuationData(nextPageData);
            return new ExportResult<>(ExportResult.ResultType.CONTINUE, new PhotosContainerResource(null, exportPhotos), continuationData);
        }
    } catch (FacebookGraphException e) {
        String message = e.getMessage();
        // In such case, we should skip this object and continue with the rest of the transfer.
        if (message != null && message.contains("code 100, subcode 33")) {
            monitor.info(() -> "Cannot find photos to export, skipping to the next bunch", e);
            return new ExportResult<>(ExportResult.ResultType.END, null);
        }
        throw e;
    }
}
Also used : PaginationData(org.datatransferproject.types.common.PaginationData) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) ArrayList(java.util.ArrayList) Photo(com.restfb.types.Photo) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) IOException(java.io.IOException) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) FacebookGraphException(com.restfb.exception.FacebookGraphException) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken)

Example 4 with PhotosContainerResource

use of org.datatransferproject.types.common.models.photos.PhotosContainerResource in project data-transfer-project by google.

the class BackblazePhotosImporterTest method testImportPhoto.

@Test
public void testImportPhoto() throws Exception {
    String dataId = "dataId";
    String title = "title";
    String photoUrl = "photoUrl";
    String albumName = "albumName";
    String albumId = "albumId";
    String response = "response";
    UUID jobId = UUID.randomUUID();
    PhotoModel photoModel = new PhotoModel(title, photoUrl, "", "", dataId, albumId, false, null);
    ArrayList<PhotoModel> photos = new ArrayList<>();
    photos.add(photoModel);
    PhotosContainerResource data = mock(PhotosContainerResource.class);
    when(data.getPhotos()).thenReturn(photos);
    when(executor.getCachedValue(albumId)).thenReturn(albumName);
    HttpURLConnection connection = mock(HttpURLConnection.class);
    when(connection.getInputStream()).thenReturn(IOUtils.toInputStream("photo content", "UTF-8"));
    when(streamProvider.getConnection(photoUrl)).thenReturn(connection);
    when(client.uploadFile(eq("Photo Transfer/albumName/dataId.jpg"), any())).thenReturn(response);
    when(clientFactory.getOrCreateB2Client(jobId, authData)).thenReturn(client);
    BackblazePhotosImporter sut = new BackblazePhotosImporter(monitor, dataStore, streamProvider, clientFactory);
    sut.importItem(jobId, executor, authData, data);
    ArgumentCaptor<Callable<String>> importCapture = ArgumentCaptor.forClass(Callable.class);
    verify(executor, times(1)).executeAndSwallowIOExceptions(eq(String.format("%s-%s", albumId, dataId)), eq(title), importCapture.capture());
    String actual = importCapture.getValue().call();
    assertEquals(response, actual);
}
Also used : PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) HttpURLConnection(java.net.HttpURLConnection) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) ArrayList(java.util.ArrayList) UUID(java.util.UUID) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 5 with PhotosContainerResource

use of org.datatransferproject.types.common.models.photos.PhotosContainerResource in project data-transfer-project by google.

the class BackblazePhotosImporterTest method testImportAlbum.

@Test
public void testImportAlbum() throws Exception {
    String albumId = "albumId";
    PhotoAlbum album = new PhotoAlbum(albumId, "", "");
    ArrayList<PhotoAlbum> albums = new ArrayList<>();
    albums.add(album);
    PhotosContainerResource data = mock(PhotosContainerResource.class);
    when(data.getAlbums()).thenReturn(albums);
    BackblazePhotosImporter sut = new BackblazePhotosImporter(monitor, dataStore, streamProvider, clientFactory);
    sut.importItem(UUID.randomUUID(), executor, authData, data);
    verify(executor, times(1)).executeAndSwallowIOExceptions(eq(albumId), eq("Caching album name for album 'albumId'"), any());
}
Also used : PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) ArrayList(java.util.ArrayList) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) Test(org.junit.Test)

Aggregations

PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)57 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)37 Test (org.junit.Test)37 IdOnlyContainerResource (org.datatransferproject.types.common.models.IdOnlyContainerResource)29 PhotoModel (org.datatransferproject.types.common.models.photos.PhotoModel)29 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)27 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)21 ArrayList (java.util.ArrayList)18 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)18 UUID (java.util.UUID)14 PaginationData (org.datatransferproject.types.common.PaginationData)12 IOException (java.io.IOException)10 ExportInformation (org.datatransferproject.types.common.ExportInformation)9 ImportResult (org.datatransferproject.spi.transfer.provider.ImportResult)8 IntPaginationToken (org.datatransferproject.types.common.IntPaginationToken)8 InputStreamWrapper (org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore.InputStreamWrapper)6 ContainerResource (org.datatransferproject.types.common.models.ContainerResource)6 TokensAndUrlAuthData (org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData)6 Photoset (com.flickr4java.flickr.photosets.Photoset)5 ImmutableList (com.google.common.collect.ImmutableList)5