return new PhotosContainerResource(albums, photoModels);
@Test public void verifySerializeDeserialize() throws Exception { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerSubtypes(PhotosContainerResource.class); List<PhotoAlbum> albums = ImmutableList.of(new PhotoAlbum("id1", "albumb1", "This is a fake albumb")); List<PhotoModel> photos = ImmutableList.of( new PhotoModel("Pic1", "http://fake.com/1.jpg", "A pic", "image/jpg", "p1", "id1", false), new PhotoModel( "Pic2", "https://fake.com/pic.png", "fine art", "image/png", "p2", "id1", false)); ContainerResource data = new PhotosContainerResource(albums, photos); String serialized = objectMapper.writeValueAsString(data); ContainerResource deserializedModel = objectMapper.readValue(serialized, ContainerResource.class); Truth.assertThat(deserializedModel).isNotNull(); Truth.assertThat(deserializedModel).isInstanceOf(PhotosContainerResource.class); PhotosContainerResource deserialized = (PhotosContainerResource) deserializedModel; Truth.assertThat(deserialized.getAlbums()).hasSize(1); Truth.assertThat(deserialized.getPhotos()).hasSize(2); } }
@Test public void testSpecifiedAlbums() { ExportResult<PhotosContainerResource> result = facebookPhotosExporter.export( uuid, new TokensAndUrlAuthData("accessToken", null, null), Optional.of( new ExportInformation( new StringPaginationToken(PHOTO_TOKEN_PREFIX), new PhotosContainerResource( Lists.newArrayList(new PhotoAlbum(ALBUM_ID, ALBUM_NAME, ALBUM_DESCRIPTION)), new ArrayList<>())))); assertEquals(ExportResult.ResultType.CONTINUE, result.getType()); PhotosContainerResource exportedData = result.getExportedData(); assertEquals(1, exportedData.getAlbums().size()); assertEquals( new PhotoAlbum(ALBUM_ID, ALBUM_NAME, ALBUM_DESCRIPTION), exportedData.getAlbums().toArray()[0]); assertNull((result.getContinuationData().getPaginationData())); assertThat(result.getContinuationData().getContainerResources()) .contains(new IdOnlyContainerResource(ALBUM_ID)); }
assertThat(result.getExportedData().getPhotos().stream().map(PhotoModel::getFetchableUrl) .collect(Collectors.toList())).containsExactly(albumlessPhotoUri + "=d"); // download
@Test public void testExportPhoto() { 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_SOURCE, PHOTO_NAME, "image/jpg", PHOTO_ID, ALBUM_ID, false), exportedData.getPhotos().toArray()[0]); }
@Test public void testExportAlbum() { ExportResult<PhotosContainerResource> result = facebookPhotosExporter.export( uuid, new TokensAndUrlAuthData("accessToken", null, null), Optional.empty()); assertEquals(ExportResult.ResultType.CONTINUE, result.getType()); PhotosContainerResource exportedData = result.getExportedData(); assertEquals(1, exportedData.getAlbums().size()); assertEquals( new PhotoAlbum(ALBUM_ID, ALBUM_NAME, ALBUM_DESCRIPTION), exportedData.getAlbums().toArray()[0]); assertNull(result.getContinuationData().getPaginationData()); assertThat(result.getContinuationData().getContainerResources()) .contains(new IdOnlyContainerResource(ALBUM_ID)); }
private ExportResult<PhotosContainerResource> exportPhotos(TokensAndUrlAuthData authData, Optional<PaginationData> pageData) { Preconditions.checkNotNull(authData); MediaResponse response; try { response = makeRequest(MEDIA_URL, MediaResponse.class, authData); } catch (IOException e) { return new ExportResult<>(e); } List<PhotoModel> photos = new ArrayList<>(); // TODO: check out paging. for (MediaFeedData photo : response.getData()) { // TODO json mapping is broken. String photoId = photo.getId(); String url = photo.getImages().getStandardResolution().getUrl(); String text = (photo.getCaption() != null) ? photo.getCaption().getText() : null; photos.add(new PhotoModel("Instagram photo: " + photoId, url, text, null, null, FAKE_ALBUM_ID, false)); } List<PhotoAlbum> albums = new ArrayList<>(); if (!photos.isEmpty() && !pageData.isPresent()) { albums.add( new PhotoAlbum( FAKE_ALBUM_ID, "Imported Instagram Photos", "Photos imported from instagram")); } return new ExportResult<>(ResultType.END, new PhotosContainerResource(albums, photos)); }
@Override public ImportResult importItem( UUID jobId, TokenSecretAuthData authData, PhotosContainerResource data) { Twitter twitterApi = TwitterApiWrapper.getInstance(appCredentials, authData); // Twitter doesn't support an 'Albums' concept, so that information is just lost. for (PhotoModel image : data.getPhotos()) { try { StatusUpdate update = new StatusUpdate(image.getDescription()); InputStreamContent content = new InputStreamContent(null, getImageAsStream(image.getFetchableUrl())); update.media(image.getTitle(), content.getInputStream()); twitterApi.tweets().updateStatus(update); } catch (IOException | TwitterException e) { monitor.severe(() -> "Error importing twitter photo", e); return new ImportResult(e); } } return new ImportResult(ResultType.OK); }
@Override public ImportResult importItem( UUID jobId, TokensAndUrlAuthData authData, PhotosContainerResource data) throws IOException { if (data == null) { // Nothing to do return ImportResult.OK; } // Uploads album metadata if (data.getAlbums() != null && data.getAlbums().size() > 0) { for (PhotoAlbum album : data.getAlbums()) { importSingleAlbum(jobId, authData, album); } } // Uploads photos if (data.getPhotos() != null && data.getPhotos().size() > 0) { for (PhotoModel photo : data.getPhotos()) { importSinglePhoto(jobId, authData, photo); } } return ImportResult.OK; }
if (Strings.isNullOrEmpty(token)) { return new ExportResult<>( ExportResult.ResultType.END, new PhotosContainerResource(null, exportPhotos)); } else { PaginationData nextPageData = new StringPaginationToken(PHOTO_TOKEN_PREFIX + token); return new ExportResult<>( ExportResult.ResultType.CONTINUE, new PhotosContainerResource(null, exportPhotos), continuationData);
@Override public ImportResult importItem( UUID jobId, TokenSecretAuthData authData, PhotosContainerResource data) { try { SmugMugInterface smugMugInterface = getOrCreateSmugMugInterface(authData); for (PhotoAlbum album : data.getAlbums()) { importSingleAlbum(jobId, album, smugMugInterface); } for (PhotoModel photo : data.getPhotos()) { importSinglePhoto(jobId, photo, smugMugInterface); } } catch (IOException e) { monitor.severe(() -> "Error importing", e); return new ImportResult(e); } return ImportResult.OK; }
PhotosContainerResource containerResource = new PhotosContainerResource(albums, null); ExportResult<PhotosContainerResource> exportResult = new ExportResult<>(resultType, containerResource, continuationData);
@Override public ImportResult importItem( UUID jobId, TokenAuthData authData, PhotosContainerResource resource) throws IOException { TempPhotoData photoData = jobStore.findData(jobId, createCacheKey(), TempPhotoData.class); if (photoData == null) { photoData = new TempPhotoData(jobId.toString()); jobStore.create(jobId, createCacheKey(), photoData); } for (PhotoAlbum album : resource.getAlbums()) { // Create a OneDrive folder and then save the id with the mapping data createOneDriveFolder(album, jobId, authData, photoData); } for (PhotoModel photoModel : resource.getPhotos()) { importPhoto(photoModel, jobId, authData, photoData); } return null; }
@VisibleForTesting ExportResult<PhotosContainerResource> exportPhotos(TokensAndUrlAuthData authData, Optional<IdOnlyContainerResource> albumData, Optional<PaginationData> paginationData, UUID jobId) throws IOException { 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); }
data.getAlbums() != null || data.getPhotos() != null, "Error: There is no data to import"); if (data.getAlbums() != null) { importAlbums(data.getAlbums(), tempPhotosData); jobStore.update(jobId, createCacheKey(), tempPhotosData); if (data.getPhotos() != null) { for (PhotoModel photo : data.getPhotos()) { try { importSinglePhoto(jobId, photo);
private ExportResult<PhotosContainerResource> exportAlbums( TokensAndUrlAuthData authData, Optional<StringPaginationToken> paginationData) { Optional<String> paginationToken = stripTokenPrefix(paginationData, ALBUM_TOKEN_PREFIX); ArrayList<PhotoAlbum> exportAlbums = new ArrayList<>(); // 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(); 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); }
@Test public void exportPhotoFirstSet() throws IOException { 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(IMG_URI + "=d"); // for download assertThat(actualPhotos.stream().map(PhotoModel::getAlbumId).collect(Collectors.toList())) .containsExactly(ALBUM_ID); }