use of org.datatransferproject.spi.transfer.types.UploadErrorException 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();
}
}
Aggregations