Skip to content

Commit 37d0e5a

Browse files
author
Amir Tocker
committed
Merge branch 'elevenfive-master' into merge-master
2 parents 517b9dc + 80f10bf commit 37d0e5a

File tree

2 files changed

+50
-26
lines changed

2 files changed

+50
-26
lines changed

cloudinary-android/src/main/java/com/cloudinary/android/MultipartUtility.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class MultipartUtility {
2323
private String charset;
2424
private OutputStream outputStream;
2525
private PrintWriter writer;
26+
2627
public final static String USER_AGENT = "CloudinaryAndroid/" + Cloudinary.VERSION;
2728

2829
/**
@@ -141,10 +142,21 @@ public HttpURLConnection execute() throws IOException {
141142
return httpConn;
142143
}
143144

144-
/***
145+
/**
146+
* Closes the internal connection's output stream.
147+
* Closing a previously closed stream has no effect.
148+
*/
149+
public void close(){
150+
if (writer != null){
151+
writer.close();
152+
}
153+
}
154+
155+
/**
145156
* For internal use only - callback to monitor multipart upload progress
146157
*/
147158
interface MultipartCallback {
148159
void totalBytesLoaded(long bytes);
149160
}
161+
150162
}

cloudinary-android/src/main/java/com/cloudinary/android/UploaderStrategy.java

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,35 +59,47 @@ public void totalBytesLoaded(long bytes) {
5959
};
6060
}
6161

62-
MultipartUtility multipart = new MultipartUtility(apiUrl, "UTF-8", this.cloudinary().randomPublicId(), (Map<String, String>) options.get("extra_headers"), multipartCallback);
62+
MultipartUtility multipart = null;
63+
HttpURLConnection connection;
6364

64-
// Remove blank parameters
65-
for (Map.Entry<String, Object> param : params.entrySet()) {
66-
if (param.getValue() instanceof Collection) {
67-
for (Object value : (Collection) param.getValue()) {
68-
multipart.addFormField(param.getKey() + "[]", ObjectUtils.asString(value));
69-
}
70-
} else {
71-
if (StringUtils.isNotBlank(param.getValue())) {
72-
multipart.addFormField(param.getKey(), param.getValue().toString());
65+
try {
66+
multipart = new MultipartUtility(apiUrl, "UTF-8", this.cloudinary().randomPublicId(), (Map<String, String>) options.get("extra_headers"));
67+
68+
// Remove blank parameters
69+
for (Map.Entry<String, Object> param : params.entrySet()) {
70+
if (param.getValue() instanceof Collection) {
71+
for (Object value : (Collection) param.getValue()) {
72+
multipart.addFormField(param.getKey() + "[]", ObjectUtils.asString(value));
73+
}
74+
} else {
75+
if (StringUtils.isNotBlank(param.getValue())) {
76+
multipart.addFormField(param.getKey(), param.getValue().toString());
77+
}
7378
}
7479
}
75-
}
7680

77-
if (file instanceof String && !((String) file).matches("(?s)ftp:.*|https?:.*|s3:.*|data:[^;]*;base64,([a-zA-Z0-9/+\n=]+)")) {
78-
file = new File((String) file);
79-
}
80-
String filename = (String) options.get("filename");
81-
if (file instanceof File) {
82-
multipart.addFilePart("file", (File) file, filename);
83-
} else if (file instanceof String) {
84-
multipart.addFormField("file", (String) file);
85-
} else if (file instanceof InputStream) {
86-
multipart.addFilePart("file", (InputStream) file, filename);
87-
} else if (file instanceof byte[]) {
88-
multipart.addFilePart("file", new ByteArrayInputStream((byte[]) file), filename);
89-
}
90-
HttpURLConnection connection = multipart.execute();
81+
if (file instanceof String && !((String) file).matches("(?s)ftp:.*|https?:.*|s3:.*|data:[^;]*;base64,([a-zA-Z0-9/+\n=]+)")) {
82+
file = new File((String) file);
83+
}
84+
String filename = (String) options.get("filename");
85+
if (file instanceof File) {
86+
multipart.addFilePart("file", (File) file, filename);
87+
} else if (file instanceof String) {
88+
multipart.addFormField("file", (String) file);
89+
} else if (file instanceof InputStream) {
90+
multipart.addFilePart("file", (InputStream) file, filename);
91+
} else if (file instanceof byte[]) {
92+
multipart.addFilePart("file", new ByteArrayInputStream((byte[]) file), filename);
93+
}
94+
95+
connection = multipart.execute();
96+
} finally {
97+
if (multipart != null){
98+
// Closing more than once has no effect so we can call it safely without having to check state
99+
multipart.close();
100+
}
101+
}
102+
91103
int code;
92104
try {
93105
code = connection.getResponseCode();

0 commit comments

Comments
 (0)