Search in sources :

Example 41 with PhotoModel

use of org.datatransferproject.types.common.models.photos.PhotoModel 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]);
}
Also used : PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) ExportInformation(org.datatransferproject.types.common.ExportInformation) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) TokensAndUrlAuthData(org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) Test(org.junit.Test)

Example 42 with PhotoModel

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

the class GooglePhotosExporter method exportPhotos.

@VisibleForTesting
ExportResult<PhotosContainerResource> exportPhotos(TokensAndUrlAuthData authData, Optional<IdOnlyContainerResource> albumData, Optional<PaginationData> paginationData, UUID jobId) throws IOException, InvalidTokenException, PermissionDeniedException {
    Optional<String> albumId = Optional.empty();
    if (albumData.isPresent()) {
        albumId = Optional.of(albumData.get().getId());
    }
    Optional<String> paginationToken = getPhotosPaginationToken(paginationData);
    MediaItemSearchResponse mediaItemSearchResponse = getOrCreatePhotosInterface(authData).listMediaItems(albumId, paginationToken);
    PaginationData nextPageData = null;
    if (!Strings.isNullOrEmpty(mediaItemSearchResponse.getNextPageToken())) {
        nextPageData = new StringPaginationToken(PHOTO_TOKEN_PREFIX + mediaItemSearchResponse.getNextPageToken());
    }
    ContinuationData continuationData = new ContinuationData(nextPageData);
    PhotosContainerResource containerResource = null;
    GoogleMediaItem[] mediaItems = mediaItemSearchResponse.getMediaItems();
    if (mediaItems != null && mediaItems.length > 0) {
        List<PhotoModel> photos = convertPhotosList(albumId, mediaItems, jobId);
        containerResource = new PhotosContainerResource(null, photos);
    }
    ResultType resultType = ResultType.CONTINUE;
    if (nextPageData == null) {
        resultType = ResultType.END;
    }
    return new ExportResult<>(resultType, containerResource, continuationData);
}
Also used : PaginationData(org.datatransferproject.types.common.PaginationData) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) MediaItemSearchResponse(org.datatransferproject.datatransfer.google.mediaModels.MediaItemSearchResponse) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) GoogleMediaItem(org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 43 with PhotoModel

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

the class GooglePhotosExporter method exportPhotosContainer.

private ExportResult<PhotosContainerResource> exportPhotosContainer(PhotosContainerResource container, TokensAndUrlAuthData authData) throws IOException, InvalidTokenException, PermissionDeniedException {
    ImmutableList.Builder<PhotoAlbum> albumBuilder = ImmutableList.builder();
    ImmutableList.Builder<PhotoModel> photosBuilder = ImmutableList.builder();
    List<IdOnlyContainerResource> subResources = new ArrayList<>();
    for (PhotoAlbum album : container.getAlbums()) {
        GoogleAlbum googleAlbum = getOrCreatePhotosInterface(authData).getAlbum(album.getId());
        albumBuilder.add(new PhotoAlbum(googleAlbum.getId(), googleAlbum.getTitle(), null));
        // Adding subresources tells the framework to recall export to get all the photos
        subResources.add(new IdOnlyContainerResource(googleAlbum.getId()));
    }
    for (PhotoModel photo : container.getPhotos()) {
        GoogleMediaItem googleMediaItem = getOrCreatePhotosInterface(authData).getMediaItem(photo.getDataId());
        photosBuilder.add(convertToPhotoModel(Optional.empty(), googleMediaItem));
    }
    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);
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) ArrayList(java.util.ArrayList) 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) GoogleMediaItem(org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem) GoogleAlbum(org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 44 with PhotoModel

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

the class GooglePhotosImporter method importPhotoBatch.

long importPhotoBatch(UUID jobId, TokensAndUrlAuthData authData, List<PhotoModel> photos, IdempotentImportExecutor executor, String albumId) throws Exception {
    final ArrayList<NewMediaItem> mediaItems = new ArrayList<>();
    final HashMap<String, PhotoModel> uploadTokenToDataId = new HashMap<>();
    final HashMap<String, Long> uploadTokenToLength = new HashMap<>();
    // this however, seems to require knowledge of the total file size.
    for (PhotoModel photo : photos) {
        try {
            Pair<InputStream, Long> inputStreamBytesPair = getInputStreamForUrl(jobId, photo.getFetchableUrl(), photo.isInTempStore());
            try (InputStream s = inputStreamBytesPair.getFirst()) {
                String uploadToken = getOrCreatePhotosInterface(jobId, authData).uploadPhotoContent(s);
                mediaItems.add(new NewMediaItem(cleanDescription(photo.getDescription()), uploadToken));
                uploadTokenToDataId.put(uploadToken, photo);
                uploadTokenToLength.put(uploadToken, inputStreamBytesPair.getSecond());
            }
            try {
                if (photo.isInTempStore()) {
                    jobStore.removeData(jobId, photo.getFetchableUrl());
                }
            } catch (Exception e) {
                // Swallow the exception caused by Remove data so that existing flows continue
                monitor.info(() -> format("%s: Exception swallowed in removeData call for localPath %s", jobId, photo.getFetchableUrl()), e);
            }
        } catch (IOException e) {
            executor.executeAndSwallowIOExceptions(IdempotentImportExecutorHelper.getPhotoIdempotentId(photo), photo.getTitle(), () -> {
                throw e;
            });
        }
    }
    if (mediaItems.isEmpty()) {
        // Either we were not passed in any videos or we failed upload on all of them.
        return 0L;
    }
    long totalBytes = 0L;
    NewMediaItemUpload uploadItem = new NewMediaItemUpload(albumId, mediaItems);
    try {
        BatchMediaItemResponse photoCreationResponse = getOrCreatePhotosInterface(jobId, authData).createPhotos(uploadItem);
        Preconditions.checkNotNull(photoCreationResponse);
        NewMediaItemResult[] mediaItemResults = photoCreationResponse.getResults();
        Preconditions.checkNotNull(mediaItemResults);
        for (NewMediaItemResult mediaItem : mediaItemResults) {
            PhotoModel photo = uploadTokenToDataId.get(mediaItem.getUploadToken());
            totalBytes += processMediaResult(mediaItem, IdempotentImportExecutorHelper.getPhotoIdempotentId(photo), executor, photo.getTitle(), uploadTokenToLength.get(mediaItem.getUploadToken()));
            uploadTokenToDataId.remove(mediaItem.getUploadToken());
        }
        if (!uploadTokenToDataId.isEmpty()) {
            for (PhotoModel photo : uploadTokenToDataId.values()) {
                executor.executeAndSwallowIOExceptions(IdempotentImportExecutorHelper.getPhotoIdempotentId(photo), photo.getTitle(), () -> {
                    throw new IOException("Photo was missing from results list.");
                });
            }
        }
    } catch (IOException e) {
        if (e.getMessage() != null && e.getMessage().contains("The remaining storage in the user's account is not enough")) {
            throw new DestinationMemoryFullException("Google destination storage full", e);
        } else {
            throw e;
        }
    }
    return totalBytes;
}
Also used : BatchMediaItemResponse(org.datatransferproject.datatransfer.google.mediaModels.BatchMediaItemResponse) HashMap(java.util.HashMap) InputStream(java.io.InputStream) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) DestinationMemoryFullException(org.datatransferproject.spi.transfer.types.DestinationMemoryFullException) ArrayList(java.util.ArrayList) NewMediaItemResult(org.datatransferproject.datatransfer.google.mediaModels.NewMediaItemResult) NewMediaItemUpload(org.datatransferproject.datatransfer.google.mediaModels.NewMediaItemUpload) IOException(java.io.IOException) DestinationMemoryFullException(org.datatransferproject.spi.transfer.types.DestinationMemoryFullException) PermissionDeniedException(org.datatransferproject.spi.transfer.types.PermissionDeniedException) IOException(java.io.IOException) InvalidTokenException(org.datatransferproject.spi.transfer.types.InvalidTokenException) NewMediaItem(org.datatransferproject.datatransfer.google.mediaModels.NewMediaItem)

Aggregations

PhotoModel (org.datatransferproject.types.common.models.photos.PhotoModel)44 PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)29 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)24 Test (org.junit.Test)22 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)17 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)16 ArrayList (java.util.ArrayList)14 UUID (java.util.UUID)14 IOException (java.io.IOException)12 IdOnlyContainerResource (org.datatransferproject.types.common.models.IdOnlyContainerResource)12 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)10 TokensAndUrlAuthData (org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 InputStream (java.io.InputStream)6 PaginationData (org.datatransferproject.types.common.PaginationData)6 Collection (java.util.Collection)5 List (java.util.List)5 Optional (java.util.Optional)5 Collectors (java.util.stream.Collectors)5 BatchMediaItemResponse (org.datatransferproject.datatransfer.google.mediaModels.BatchMediaItemResponse)5