Search in sources :

Example 31 with PhotoModel

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

the class GooglePhotosExporterTest method exportPhotoFirstSet.

@Test
public void exportPhotoFirstSet() throws IOException, InvalidTokenException, PermissionDeniedException {
    setUpSingleAlbum();
    when(albumListResponse.getNextPageToken()).thenReturn(null);
    GoogleMediaItem mediaItem = setUpSinglePhoto(IMG_URI, PHOTO_ID);
    when(mediaItemSearchResponse.getMediaItems()).thenReturn(new GoogleMediaItem[] { mediaItem });
    when(mediaItemSearchResponse.getNextPageToken()).thenReturn(PHOTO_TOKEN);
    IdOnlyContainerResource idOnlyContainerResource = new IdOnlyContainerResource(ALBUM_ID);
    ExportResult<PhotosContainerResource> result = googlePhotosExporter.exportPhotos(null, Optional.of(idOnlyContainerResource), Optional.empty(), uuid);
    // Check results
    // Verify correct methods were called
    verify(photosInterface).listMediaItems(Optional.of(ALBUM_ID), Optional.empty());
    verify(mediaItemSearchResponse).getMediaItems();
    // Check pagination
    ContinuationData continuationData = result.getContinuationData();
    StringPaginationToken paginationToken = (StringPaginationToken) continuationData.getPaginationData();
    assertThat(paginationToken.getToken()).isEqualTo(PHOTO_TOKEN_PREFIX + PHOTO_TOKEN);
    // Check albums field of container (should be empty)
    Collection<PhotoAlbum> actualAlbums = result.getExportedData().getAlbums();
    assertThat(actualAlbums).isEmpty();
    // Check photos field of container
    Collection<PhotoModel> actualPhotos = result.getExportedData().getPhotos();
    assertThat(actualPhotos.stream().map(PhotoModel::getFetchableUrl).collect(Collectors.toList())).containsExactly(// for download
    IMG_URI + "=d");
    assertThat(actualPhotos.stream().map(PhotoModel::getAlbumId).collect(Collectors.toList())).containsExactly(ALBUM_ID);
    assertThat(actualPhotos.stream().map(PhotoModel::getTitle).collect(Collectors.toList())).containsExactly(FILENAME);
}
Also used : PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) GoogleMediaItem(org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) Test(org.junit.Test)

Example 32 with PhotoModel

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

the class ImgurPhotosExporter method requestNonAlbumPhotos.

/**
 * Queries all photos for the account. Chooses photos which are not included to the collection of
 * photos from albums.
 *
 * @param authData authentication information
 * @param paginationData pagination information to use for subsequent calls.
 */
private ExportResult<PhotosContainerResource> requestNonAlbumPhotos(TokensAndUrlAuthData authData, PaginationData paginationData, UUID jobId) throws IOException {
    int page = paginationData == null ? 0 : ((IntPaginationToken) paginationData).getStart();
    String url = format(ALL_PHOTOS_URL_TEMPLATE, page);
    Set<PhotoAlbum> albums = new HashSet<>();
    List<PhotoModel> photos = new ArrayList<>();
    List<Map<String, Object>> items = requestData(authData, url);
    boolean hasMore = (items != null && items.size() != 0);
    for (Map<String, Object> item : items) {
        String photoId = (String) item.get("id");
        // Select photos which are not included to the collection of retrieved album photos
        if (!albumPhotos.contains(photoId)) {
            PhotoModel photoModel = new PhotoModel((String) item.get("name"), (String) item.get("link"), (String) item.get("description"), (String) item.get("type"), (String) item.get("id"), DEFAULT_ALBUM_ID, true);
            photos.add(photoModel);
            InputStream inputStream = getImageAsStream(photoModel.getFetchableUrl());
            jobStore.create(jobId, photoModel.getFetchableUrl(), inputStream);
        }
    }
    if (!containsNonAlbumPhotos && photos.size() > 0) {
        // Add album for non-album photos
        albums.add(new PhotoAlbum(DEFAULT_ALBUM_ID, "Non-album photos", "Contains non-album photos"));
        // Make sure album will not be added multiply times on subsequent calls
        containsNonAlbumPhotos = true;
    }
    PaginationData newPage = null;
    if (hasMore) {
        newPage = new IntPaginationToken(page + 1);
        monitor.info(() -> format("added non-album photos, size: %s", photos.size()));
    }
    PhotosContainerResource photosContainerResource = new PhotosContainerResource(albums, photos);
    ContinuationData continuationData = new ContinuationData(newPage);
    ExportResult.ResultType resultType = ExportResult.ResultType.CONTINUE;
    if (newPage == null) {
        resultType = ExportResult.ResultType.END;
    }
    return new ExportResult<>(resultType, photosContainerResource, continuationData);
}
Also used : IntPaginationToken(org.datatransferproject.types.common.IntPaginationToken) PaginationData(org.datatransferproject.types.common.PaginationData) InputStream(java.io.InputStream) 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) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) Map(java.util.Map) HashSet(java.util.HashSet) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 33 with PhotoModel

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

the class ImgurPhotosExporter method requestPhotos.

/**
 * Exports photos from album.
 *
 * <p>This request doesn't support pages so it retrieves all photos at once for each album.
 *
 * @param authData authentication information
 * @param resource contains album id
 * @param paginationData pagination information to use for subsequent calls.
 */
private ExportResult<PhotosContainerResource> requestPhotos(TokensAndUrlAuthData authData, IdOnlyContainerResource resource, PaginationData paginationData, UUID jobId) throws IOException {
    String albumId = resource.getId();
    // Means all other albums with photos are imported, so non-album photos can be determined
    if (albumId.equals(DEFAULT_ALBUM_ID)) {
        return requestNonAlbumPhotos(authData, paginationData, jobId);
    }
    String url = format(ALBUM_PHOTOS_URL_TEMPLATE, albumId);
    List<PhotoModel> photos = new ArrayList<>();
    List<Map<String, Object>> items = requestData(authData, url);
    for (Map<String, Object> item : items) {
        PhotoModel photoModel = new PhotoModel((String) item.get("name"), (String) item.get("link"), (String) item.get("description"), (String) item.get("type"), (String) item.get("id"), albumId, true);
        photos.add(photoModel);
        InputStream inputStream = getImageAsStream(photoModel.getFetchableUrl());
        jobStore.create(jobId, photoModel.getFetchableUrl(), inputStream);
        // Save id of each album photo for finding non-album photos later
        albumPhotos.add((String) item.get("id"));
    }
    // This request doesn't support pages
    ExportResult.ResultType resultType = ExportResult.ResultType.END;
    if (photos.size() > 0) {
        monitor.info(() -> format("added albumPhotos, size: %s", photos.size()));
    }
    PhotosContainerResource photosContainerResource = new PhotosContainerResource(null, photos);
    return new ExportResult<>(resultType, photosContainerResource, new ContinuationData(null));
}
Also used : InputStream(java.io.InputStream) 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) Map(java.util.Map) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 34 with PhotoModel

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

the class GooglePhotosImporterTest method importPhotoInTempStoreFailure.

@Test
public void importPhotoInTempStoreFailure() 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())).thenThrow(new IOException("Unit Testing"));
    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();
    googlePhotosImporter.importPhotoBatch(jobId, Mockito.mock(TokensAndUrlAuthData.class), Lists.newArrayList(photoModel), executor, NEW_ALBUM_ID);
    Mockito.verify(jobStore, Mockito.times(0)).removeData(any(), anyString());
    Mockito.verify(jobStore, Mockito.times(1)).getStream(any(), anyString());
}
Also used : TemporaryPerJobDataStore(org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore) BatchMediaItemResponse(org.datatransferproject.datatransfer.google.mediaModels.BatchMediaItemResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) TokensAndUrlAuthData(org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData) LocalJobStore(org.datatransferproject.cloud.local.LocalJobStore) JobStore(org.datatransferproject.spi.cloud.storage.JobStore) NewMediaItemUpload(org.datatransferproject.datatransfer.google.mediaModels.NewMediaItemUpload) IOException(java.io.IOException) UUID(java.util.UUID) Test(org.junit.Test)

Example 35 with PhotoModel

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

the class KoofrMediaExport method processPhoto.

protected void processPhoto(FilesFile file, String path, String albumId) {
    String photoId = path;
    String name = getFileName(file);
    String description = getFileDescription(file);
    String contentType = file.getContentType();
    Date uploadedTime = new Date(file.getModified());
    String fullPath = rootPath + path;
    albumsWithPhotos.add(albumId);
    PhotoModelContainer container = new PhotoModelContainer();
    container.photoModel = new PhotoModel(name, "", description, contentType, photoId, albumId, false, uploadedTime);
    container.fullPath = fullPath;
    photos.add(container);
}
Also used : PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) Date(java.util.Date)

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