Skip to content

Commit 29270d4

Browse files
Fix uploadLarge to use X-Unique-Upload-Id instead of updating params. Solves cloudinary/cloudinary_android#18
1 parent 2962532 commit 29270d4

File tree

6 files changed

+29
-17
lines changed

6 files changed

+29
-17
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.net.HttpURLConnection;
1111
import java.net.URL;
1212
import java.net.URLConnection;
13+
import java.util.Map;
1314

1415
import com.cloudinary.Cloudinary;
1516

@@ -40,15 +41,19 @@ public class MultipartUtility {
4041
* @param charset
4142
* @throws IOException
4243
*/
43-
public MultipartUtility(String requestURL, String charset, String boundary, String contentRange) throws IOException {
44+
public MultipartUtility(String requestURL, String charset, String boundary, Map<String,String> headers) throws IOException {
4445
this.charset = charset;
4546
this.boundary = boundary;
4647

4748
URL url = new URL(requestURL);
4849
httpConn = (HttpURLConnection) url.openConnection();
4950
httpConn.setDoOutput(true); // indicates POST method
5051
httpConn.setDoInput(true);
51-
if (contentRange != null) httpConn.setRequestProperty("Content-Range", contentRange);
52+
if (headers != null) {
53+
for (Map.Entry<String,String> header : headers.entrySet()) {
54+
httpConn.setRequestProperty(header.getKey(), header.getValue());
55+
}
56+
}
5257
httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
5358
httpConn.setRequestProperty("User-Agent", USER_AGENT);
5459
outputStream = httpConn.getOutputStream();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public Map callApi(String action, Map<String, Object> params, Map options, Objec
4747
}
4848
}
4949
String apiUrl = this.cloudinary().cloudinaryApiUrl(action, options);
50-
MultipartUtility multipart = new MultipartUtility(apiUrl, "UTF-8", this.cloudinary().randomPublicId(), (String) options.get("content_range"));
50+
MultipartUtility multipart = new MultipartUtility(apiUrl, "UTF-8", this.cloudinary().randomPublicId(), (Map<String,String>) options.get("extra_headers"));
5151

5252
// Remove blank parameters
5353
for (Map.Entry<String, Object> param : params.entrySet()) {

cloudinary-core/src/main/java/com/cloudinary/Uploader.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ public Map uploadLarge(Object file, Map options, int bufferSize) throws IOExcept
9898

9999
private Map uploadLargeParts(InputStream input, Map options, int bufferSize, long length) throws IOException {
100100
Map params = buildUploadParams(options);
101-
Map nextParams = new HashMap();
102-
nextParams.putAll(params);
103-
Map sentParams = new HashMap();
104101

105102
Map sentOptions = new HashMap();
106103
sentOptions.putAll(options);
104+
Map extraHeaders = new HashMap();
105+
extraHeaders.put("X-Unique-Upload-Id", cloudinary().randomPublicId());
106+
sentOptions.put("extra_headers", extraHeaders);
107107

108108
byte[] buffer = new byte[bufferSize];
109109
byte[] nibbleBuffer = new byte[1];
@@ -120,8 +120,6 @@ private Map uploadLargeParts(InputStream input, Map options, int bufferSize, lon
120120

121121
if (atEnd || fullBuffer) {
122122
totalBytes += currentBufferSize;
123-
sentParams.clear();
124-
sentParams.putAll(nextParams);
125123
int currentLoc = bufferSize * partNumber;
126124
if (!atEnd) {
127125
//verify not on end - try read another byte
@@ -135,10 +133,10 @@ private Map uploadLargeParts(InputStream input, Map options, int bufferSize, lon
135133
buffer = finalBuffer;
136134
}
137135
String range = String.format("bytes %d-%d/%d", currentLoc, currentLoc + currentBufferSize - 1, length);
138-
sentOptions.put("content_range", range);
136+
extraHeaders.put("Content-Range", range);
137+
Map sentParams = new HashMap();
138+
sentParams.putAll(params);
139139
response = callApi("upload", sentParams, sentOptions, buffer);
140-
nextParams.put("public_id", response.get("public_id"));
141-
nextParams.put("upload_id", response.get("upload_id"));
142140
if (atEnd) break;
143141
buffer[0] = nibbleBuffer[0];
144142
currentBufferSize = 1;

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,11 @@ public Map callApi(String action, Map<String, Object> params, Map options, Objec
6060
HttpPost postMethod = new HttpPost(apiUrl);
6161
postMethod.setHeader("User-Agent", Cloudinary.USER_AGENT + " ApacheHTTPComponents/4.2");
6262

63-
if (options.get("content_range") != null) {
64-
postMethod.setHeader("Content-Range", (String) options.get("content_range"));
63+
Map<String,String> extraHeaders = (Map<String,String>) options.get("extra_headers");
64+
if (extraHeaders != null) {
65+
for (Map.Entry<String,String> header : extraHeaders.entrySet()) {
66+
postMethod.setHeader(header.getKey(), header.getValue());
67+
}
6568
}
6669

6770
Charset utf8 = Charset.forName("UTF-8");

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,11 @@ public Map callApi(String action, Map<String, Object> params, Map options, Objec
7171

7272
HttpPost postMethod = new HttpPost(apiUrl);
7373

74-
if (options.get("content_range") != null) {
75-
postMethod.setHeader("Content-Range", (String) options.get("content_range"));
74+
Map<String,String> extraHeaders = (Map<String,String>) options.get("extra_headers");
75+
if (extraHeaders != null) {
76+
for (Map.Entry<String,String> header : extraHeaders.entrySet()) {
77+
postMethod.setHeader(header.getKey(), header.getValue());
78+
}
7679
}
7780

7881
MultipartEntityBuilder multipart = MultipartEntityBuilder.create();

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,11 @@ public Map callApi(String action, Map<String, Object> params, Map options, Objec
7171

7272
HttpPost postMethod = new HttpPost(apiUrl);
7373

74-
if (options.get("content_range") != null) {
75-
postMethod.setHeader("Content-Range", (String) options.get("content_range"));
74+
Map<String,String> extraHeaders = (Map<String,String>) options.get("extra_headers");
75+
if (extraHeaders != null) {
76+
for (Map.Entry<String,String> header : extraHeaders.entrySet()) {
77+
postMethod.setHeader(header.getKey(), header.getValue());
78+
}
7679
}
7780

7881
MultipartEntityBuilder multipart = MultipartEntityBuilder.create();

0 commit comments

Comments
 (0)