Java Code Examples for com.google.api.client.http.InputStreamContent#writeTo()

The following examples show how to use com.google.api.client.http.InputStreamContent#writeTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GooglePhotosInterface.java    From data-transfer-project with Apache License 2.0 6 votes vote down vote up
String uploadPhotoContent(InputStream inputStream)
        throws IOException, InvalidTokenException, PermissionDeniedException {
  // TODO: add filename
  InputStreamContent content = new InputStreamContent(null, inputStream);
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  content.writeTo(outputStream);
  byte[] contentBytes = outputStream.toByteArray();
  if (contentBytes.length == 0) {
    // Google Photos cannot add an empty photo so gracefully ignore
    return "EMPTY_PHOTO";
  }
  HttpContent httpContent = new ByteArrayContent(null, contentBytes);

  return makePostRequest(
      BASE_URL + "uploads/", Optional.of(PHOTO_UPLOAD_PARAMS), httpContent, String.class);
}
 
Example 2
Source File: SmugMugInterface.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
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
  SmugMugImageUploadResponse response =
      postRequest(
          "https://upload.smugmug.com/",
          ImmutableMap.of(), // No content params for photo upload
          contentBytes,
          headersMap,
          new TypeReference<SmugMugImageUploadResponse>() {});

  Preconditions.checkState(response.getStat().equals("ok"), "Failed to upload image");
  return Preconditions.checkNotNull(response, "Image upload Response is null");
}
 
Example 3
Source File: GoogleVideosInterface.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
String uploadVideoContent(InputStream inputStream, String filename) throws IOException {
  InputStreamContent content = new InputStreamContent(null, inputStream);
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  content.writeTo(outputStream);
  byte[] contentBytes = outputStream.toByteArray();
  HttpContent httpContent = new ByteArrayContent(null, contentBytes);

  return makePostRequest(BASE_URL + "uploads/", Optional.empty(), httpContent, String.class);
}