albums.add(album); PhotoModel photoModel = new PhotoModel(name, null, "", mimeType, id, albumId, false);
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PhotoModel that = (PhotoModel) o; return Objects.equal(getTitle(), that.getTitle()) && Objects.equal(getFetchableUrl(), that.getFetchableUrl()) && Objects.equal(getDescription(), that.getDescription()) && Objects.equal(getMediaType(), that.getMediaType()) && Objects.equal(getDataId(), that.getDataId()) && Objects.equal(getAlbumId(), that.getAlbumId()); } }
SmugMugImageUploadResponse uploadImage(PhotoModel photoModel, String albumUri, InputStream inputStream) throws IOException { // Set up photo InputStreamContent content = new InputStreamContent(null, inputStream); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); content.writeTo(outputStream); byte[] contentBytes = outputStream.toByteArray(); // Headers from: https://api.smugmug.com/api/v2/doc/reference/upload.html Map<String, String> headersMap = new HashMap<>(); headersMap.put("X-Smug-AlbumUri", albumUri); headersMap.put("X-Smug-ResponseType", "JSON"); headersMap.put("X-Smug-Version", "v2"); headersMap.put("Content-Type", photoModel.getMediaType()); if (!Strings.isNullOrEmpty(photoModel.getTitle())) { headersMap.put("X-Smug-Title", photoModel.getTitle()); } if (!Strings.isNullOrEmpty(photoModel.getDescription())) { headersMap.put("X-Smug-Caption", photoModel.getDescription()); } // Upload photo return postRequest( "https://upload.smugmug.com/", ImmutableMap.of(), // No content params for photo upload contentBytes, headersMap, new TypeReference<SmugMugImageUploadResponse>() { }); }
@VisibleForTesting void importSinglePhoto(UUID jobId, PhotoModel inputPhoto, SmugMugInterface smugMugInterface) throws IOException { // Find album to upload photo to TempPhotosData tempPhotosData = jobStore.findData(jobId, createCacheKey(), TempPhotosData.class); checkState( tempPhotosData != null, "cached temp photos data for %s is null", inputPhoto.getAlbumId()); String newAlbumUri = tempPhotosData.lookupNewAlbumId(inputPhoto.getAlbumId()); checkState( !Strings.isNullOrEmpty(newAlbumUri), "Cached album URI for %s is null", inputPhoto.getAlbumId()); InputStream inputStream; if (inputPhoto.isInTempStore()) { inputStream = jobStore.getStream(jobId, inputPhoto.getFetchableUrl()); } else { inputStream = smugMugInterface.getImageAsStream(inputPhoto.getFetchableUrl()); } SmugMugImageUploadResponse response = smugMugInterface.uploadImage(inputPhoto, newAlbumUri, inputStream); }
if (inputPhoto.isInTempStore()) { inputStream = jobStore.getStream(jobId, inputPhoto.getFetchableUrl()); } else { inputStream = imageStreamProvider.get(inputPhoto.getFetchableUrl()); if (Strings.isNullOrEmpty(inputPhoto.getDescription())) { description = ""; } else { description = COPY_PREFIX + inputPhoto.getDescription(); if (Strings.isNullOrEmpty(inputPhoto.getAlbumId())) { albumId = tempPhotosData.lookupNewAlbumId(inputPhoto.getAlbumId());
private String uploadPhoto(PhotoModel photo) throws IOException, FlickrException { BufferedInputStream inStream = imageStreamProvider.get(photo.getFetchableUrl()); UploadMetaData uploadMetaData = new UploadMetaData() .setAsync(false) .setPublicFlag(false) .setFriendFlag(false) .setFamilyFlag(false) .setTitle(COPY_PREFIX + photo.getTitle()) .setDescription(photo.getDescription()); return uploader.upload(inStream, uploadMetaData); }
new PhotoModel( title, true); InputStream inputStream = smugMugInterface.getImageAsStream(model.getFetchableUrl()); jobStore.create(jobId, model.getFetchableUrl(), inputStream);
private void importSinglePhoto(UUID id, PhotoModel photo) throws FlickrException, IOException { String photoId = uploadPhoto(photo); String oldAlbumId = photo.getAlbumId();
@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); }
if (photoModel.getDataId() != null) { inputStream = jobStore.getStream(jobId, photoModel.getDataId()); } else if (photoModel.getFetchableUrl() != null) { inputStream = new URL(photoModel.getFetchableUrl()).openStream(); } else { return; String importedAlbumId = photoData.getImportedId(photoModel.getAlbumId()); String uploadUrl = String.format(uploadPhotoUrlTemplate, importedAlbumId, photoModel.getTitle()); MediaType contentType = MediaType.parse(photoModel.getMediaType());
private PhotoModel convertToPhotoModel(Optional<String> albumId, GoogleMediaItem mediaItem) { Preconditions.checkArgument(mediaItem.getMediaMetadata().getPhoto() != null); return new PhotoModel( "", // TODO: no title? mediaItem.getBaseUrl() + "=d", mediaItem.getDescription(), mediaItem.getMimeType(), mediaItem.getId(), albumId.isPresent() ? albumId.get() : null, false); }
@Test public void toCommonPhoto() { Photo photo = FlickrTestUtils.initializePhoto(PHOTO_TITLE, FETCHABLE_URL, PHOTO_DESCRIPTION, MEDIA_TYPE); PhotoModel photoModel = FlickrPhotosExporter.toCommonPhoto(photo, ALBUM_ID); assertThat(photoModel.getAlbumId()).isEqualTo(ALBUM_ID); assertThat(photoModel.getFetchableUrl()).isEqualTo(FETCHABLE_URL); assertThat(photoModel.getTitle()).isEqualTo(PHOTO_TITLE); assertThat(photoModel.getDescription()).isEqualTo(PHOTO_DESCRIPTION); assertThat(photoModel.getMediaType()).isEqualTo("image/jpeg"); }
@VisibleForTesting static PhotoModel toCommonPhoto(Photo p, String albumId) { Preconditions.checkArgument( !Strings.isNullOrEmpty(p.getOriginalSize().getSource()), "Photo [" + p.getId() + "] has a null authUrl"); return new PhotoModel( p.getTitle(), p.getOriginalSize().getSource(), p.getDescription(), toMimeType(p.getOriginalFormat()), null, // TODO this needs to be provided albumId, false); }
PhotoModel photo1 = resource.getPhotos().stream().filter(p -> "image1".equals(p.getDataId())) .findFirst().get(); Assert.assertEquals("testimage1.png", photo1.getTitle()); Assert.assertEquals("image1", photo1.getDataId()); Assert.assertEquals("image/png", photo1.getMediaType()); Assert.assertEquals(album.getId(), photo1.getAlbumId()); PhotoModel photo2 = resource.getPhotos().stream().filter(p -> "image2".equals(p.getDataId())) .findFirst().get(); Assert.assertEquals("testimage2.png", photo2.getTitle()); Assert.assertEquals("image2", photo2.getDataId()); Assert.assertEquals("image/jpeg", photo2.getMediaType()); Assert.assertEquals(album.getId(), photo2.getAlbumId());
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)); }
@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); } }
Preconditions.checkNotNull(photo.getImages().get(0).getSource()); exportPhotos.add( new PhotoModel( String.format("%s.jpg", photo.getId()), photo.getImages().get(0).getSource(),
for (MediaEntity mediaEntity : status.getMediaEntities()) { photos.add( new PhotoModel( "Twitter Photo " + mediaEntity.getId(), mediaEntity.getMediaURL(),
@Test public void exportPhoto() throws IOException { // Set up PhotoModel photoModel = new PhotoModel(PHOTO_TITLE, IMG_URI, PHOTO_DESCRIPTION, JPEG_MEDIA_TYPE, null, OLD_ALBUM_ID, false); TempPhotosData tempPhotosData = new TempPhotosData(uuid); tempPhotosData.addAlbumId(OLD_ALBUM_ID, NEW_ALBUM_ID); jobStore.create(uuid, "tempPhotosData", tempPhotosData); // Run test googlePhotosImporter.importSinglePhoto(uuid, null, photoModel); // Check results verify(imageStreamProvider).get(IMG_URI); verify(googlePhotosInterface).uploadPhotoContent(inputStream); ArgumentCaptor<NewMediaItemUpload> uploadArgumentCaptor = ArgumentCaptor .forClass(NewMediaItemUpload.class); verify(googlePhotosInterface).createPhoto(uploadArgumentCaptor.capture()); assertEquals(uploadArgumentCaptor.getValue().getAlbumId(), NEW_ALBUM_ID); List<NewMediaItem> newMediaItems = uploadArgumentCaptor.getValue().getNewMediaItems(); assertEquals(newMediaItems.size(), 1); NewMediaItem mediaItem = newMediaItems.get(0); assertEquals(mediaItem.getSimpleMediaItem().getUploadToken(), UPLOAD_TOKEN); assertEquals(mediaItem.getDescription(), "Copy of " + PHOTO_DESCRIPTION); } }
@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]); }