Search in sources :

Example 1 with Error

use of com.google.photos.library.v1.upload.UploadMediaItemResponse.Error in project data-transfer-project by google.

the class GoogleVideosImporter method uploadMediaItem.

private Pair<String, Long> uploadMediaItem(MediaObject inputVideo, PhotosLibraryClient photosLibraryClient) throws IOException, UploadErrorException {
    final File tmp;
    try (InputStream inputStream = this.videoStreamProvider.getConnection(inputVideo.getContentUrl().toString()).getInputStream()) {
        tmp = dataStore.getTempFileFromInputStream(inputStream, inputVideo.getName(), ".mp4");
    }
    try {
        UploadMediaItemRequest uploadRequest = UploadMediaItemRequest.newBuilder().setFileName(inputVideo.getName()).setDataFile(new RandomAccessFile(tmp, "r")).build();
        UploadMediaItemResponse uploadResponse = photosLibraryClient.uploadMediaItem(uploadRequest);
        String uploadToken;
        if (uploadResponse.getError().isPresent() || !uploadResponse.getUploadToken().isPresent()) {
            Error error = uploadResponse.getError().orElse(null);
            if (error != null && error.getCause().getMessage().contains("The upload url is either finalized or rejected by the server")) {
                throw new UploadErrorException("Upload was terminated because of error", error.getCause());
            }
            throw new IOException("An error was encountered while uploading the video.", error != null ? error.getCause() : null);
        } else {
            uploadToken = uploadResponse.getUploadToken().get();
        }
        return Pair.of(uploadToken, tmp.length());
    } finally {
        // noinspection ResultOfMethodCallIgnored
        tmp.delete();
    }
}
Also used : UploadMediaItemResponse(com.google.photos.library.v1.upload.UploadMediaItemResponse) RandomAccessFile(java.io.RandomAccessFile) InputStream(java.io.InputStream) UploadErrorException(org.datatransferproject.spi.transfer.types.UploadErrorException) UploadMediaItemRequest(com.google.photos.library.v1.upload.UploadMediaItemRequest) Error(com.google.photos.library.v1.upload.UploadMediaItemResponse.Error) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 2 with Error

use of com.google.photos.library.v1.upload.UploadMediaItemResponse.Error in project data-transfer-project by google.

the class GoogleVideosImporterTest method failOneVideo.

@Test
public void failOneVideo() throws Exception {
    PhotosLibraryClient photosLibraryClient = mock(PhotosLibraryClient.class);
    // Mock uploads
    when(photosLibraryClient.uploadMediaItem(any())).thenReturn(UploadMediaItemResponse.newBuilder().setUploadToken("token1").build(), UploadMediaItemResponse.newBuilder().setUploadToken("token2").build());
    // Mock creation response
    final NewMediaItemResult newMediaItemResult = NewMediaItemResult.newBuilder().setStatus(Status.newBuilder().setCode(Code.OK_VALUE).build()).setMediaItem(MediaItem.newBuilder().setId("RESULT_ID_1").build()).setUploadToken("token1").build();
    final NewMediaItemResult newMediaItemResult2 = NewMediaItemResult.newBuilder().setStatus(Status.newBuilder().setCode(Code.INVALID_ARGUMENT_VALUE).build()).setUploadToken("token2").build();
    BatchCreateMediaItemsResponse response = BatchCreateMediaItemsResponse.newBuilder().addNewMediaItemResults(newMediaItemResult).addNewMediaItemResults(newMediaItemResult2).build();
    when(photosLibraryClient.batchCreateMediaItems(ArgumentMatchers.anyList())).thenReturn(response);
    InMemoryIdempotentImportExecutor executor = new InMemoryIdempotentImportExecutor(mock(Monitor.class));
    long length = googleVideosImporter.importVideoBatch(Lists.newArrayList(new VideoModel(VIDEO_TITLE, VIDEO_URI, VIDEO_DESCRIPTION, MP4_MEDIA_TYPE, VIDEO_ID, null, false), new VideoModel(VIDEO_TITLE, VIDEO_URI, VIDEO_DESCRIPTION, MP4_MEDIA_TYPE, "myId2", null, false)), photosLibraryClient, executor);
    assertEquals("Expected the number of bytes to be the one files of 32L.", 32L, length);
    assertEquals("Expected executor to have one error.", 1, executor.getErrors().size());
    ErrorDetail errorDetail = executor.getErrors().iterator().next();
    assertEquals("myId2", errorDetail.id());
    assertThat(errorDetail.exception(), CoreMatchers.containsString("Video item could not be created."));
}
Also used : ErrorDetail(org.datatransferproject.types.transfer.errors.ErrorDetail) Monitor(org.datatransferproject.api.launcher.Monitor) NewMediaItemResult(com.google.photos.library.v1.proto.NewMediaItemResult) BatchCreateMediaItemsResponse(com.google.photos.library.v1.proto.BatchCreateMediaItemsResponse) InMemoryIdempotentImportExecutor(org.datatransferproject.spi.transfer.idempotentexecutor.InMemoryIdempotentImportExecutor) PhotosLibraryClient(com.google.photos.library.v1.PhotosLibraryClient) VideoModel(org.datatransferproject.types.common.models.videos.VideoModel) Test(org.junit.Test)

Aggregations

PhotosLibraryClient (com.google.photos.library.v1.PhotosLibraryClient)1 BatchCreateMediaItemsResponse (com.google.photos.library.v1.proto.BatchCreateMediaItemsResponse)1 NewMediaItemResult (com.google.photos.library.v1.proto.NewMediaItemResult)1 UploadMediaItemRequest (com.google.photos.library.v1.upload.UploadMediaItemRequest)1 UploadMediaItemResponse (com.google.photos.library.v1.upload.UploadMediaItemResponse)1 Error (com.google.photos.library.v1.upload.UploadMediaItemResponse.Error)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 Monitor (org.datatransferproject.api.launcher.Monitor)1 InMemoryIdempotentImportExecutor (org.datatransferproject.spi.transfer.idempotentexecutor.InMemoryIdempotentImportExecutor)1 UploadErrorException (org.datatransferproject.spi.transfer.types.UploadErrorException)1 VideoModel (org.datatransferproject.types.common.models.videos.VideoModel)1 ErrorDetail (org.datatransferproject.types.transfer.errors.ErrorDetail)1 Test (org.junit.Test)1