Skip to content

Allow uploading files from InputStreams and Files #544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/main/java/com/stripe/net/LiveStripeResponseGetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.stripe.model.StripeObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -642,7 +643,17 @@ private static StripeResponse getMultipartStripeResponse(
"Must have read permissions on file for key "
+ key + ".", null, null, null, 0, null);
}
multipartProcessor.addFileField(key, currentFile);
multipartProcessor.addFileField(key, currentFile.getName(),
new FileInputStream(currentFile));
} else if (value instanceof InputStream) {
InputStream inputStream = (InputStream) value;
if (inputStream.available() == 0) {
throw new InvalidRequestException(
"Must have available bytes to read on InputStream for key "
+ key + ".", null, null, null, 0, null
);
}
multipartProcessor.addFileField(key, "blob", inputStream);
} else {
// We only allow a single level of nesting for params
// for multipart
Expand Down
40 changes: 25 additions & 15 deletions src/main/java/com/stripe/net/MultipartProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
Expand Down Expand Up @@ -53,26 +54,38 @@ public void addFormField(String name, String value) {
}

/**
* Adds a file field to the multipart message.
* Adds a file field to the multipart message, but takes in an InputStream instead of
* just a file to read bytes from.
*
* @param name field name
* @param file File instance
* @param name Field name
* @param fileName Name of the "file" being uploaded.
* @param inputStream Stream of bytes to use in place of a file.
* @throws IOException Thrown when writing / reading from streams fails.
*/
public void addFileField(String name, File file) throws IOException {
String fileName = file.getName();
writer.append("--" + boundary).append(LINE_BREAK);
writer.append(
"Content-Disposition: form-data; name=\"" + name
+ "\"; filename=\"" + fileName + "\"").append(
LINE_BREAK);
public void addFileField(String name, String fileName, InputStream inputStream)
throws IOException {
writer.append("--").append(boundary).append(LINE_BREAK);
writer.append("Content-Disposition: form-data; name=\"").append(name)
.append("\"; filename=\"").append(fileName).append("\"").append(LINE_BREAK);

String probableContentType = URLConnection.guessContentTypeFromName(fileName);
writer.append("Content-Type: " + probableContentType).append(LINE_BREAK);
writer.append("Content-Type: ").append(probableContentType).append(LINE_BREAK);
writer.append("Content-Transfer-Encoding: binary").append(LINE_BREAK);
writer.append(LINE_BREAK);
writer.flush();

FileInputStream inputStream = new FileInputStream(file);
streamToOutput(inputStream);

writer.append(LINE_BREAK);
writer.flush();
}

/**
* Utility method to read all the bytes from an InputStream into the outputStream.
* @param inputStream Stream of bytes to read from.
* @throws IOException Thrown on errors reading / writing.
*/
private void streamToOutput(InputStream inputStream) throws IOException {
try {
byte[] buffer = new byte[4096];
int bytesRead = -1;
Expand All @@ -83,9 +96,6 @@ public void addFileField(String name, File file) throws IOException {
} finally {
inputStream.close();
}

writer.append(LINE_BREAK);
writer.flush();
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/com/stripe/functional/FileUploadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.stripe.net.APIResource;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -36,6 +37,24 @@ public void testCreate() throws IOException, StripeException {
);
}

@Test
public void testStreamCreate() throws IOException, StripeException {
final Map<String, Object> params = new HashMap<>();
params.put("purpose", "dispute_evidence");
params.put("file", new FileInputStream(getClass().getResource("/test.png").getFile()));

final FileUpload fileUpload = FileUpload.create(params);

assertNotNull(fileUpload);
verifyRequest(
APIResource.RequestMethod.POST,
"/v1/files",
params,
APIResource.RequestType.MULTIPART,
null
);
}

@Test
public void testRetrieve() throws IOException, StripeException {
final FileUpload fileUpload = FileUpload.retrieve(FILE_UPLOAD_ID);
Expand Down