Skip to content

Commit cb380d6

Browse files
committed
better large raw upload support
1 parent 0c7148d commit cb380d6

File tree

3 files changed

+49
-43
lines changed

3 files changed

+49
-43
lines changed

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

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -121,36 +121,36 @@ public Map resources(Map options) throws Exception {
121121
uri.add(resourceType);
122122
if (type != null)
123123
uri.add(type);
124-
return callApi(HttpMethod.GET, uri, only(options, "next_cursor", "direction", "max_results", "prefix", "tags", "context", "moderations"), options);
124+
return callApi(HttpMethod.GET, uri, Cloudinary.only(options, "next_cursor", "direction", "max_results", "prefix", "tags", "context", "moderations"), options);
125125
}
126126

127127
public Map resourcesByTag(String tag, Map options) throws Exception {
128128
if (options == null) options = Cloudinary.emptyMap();
129129
String resourceType = Cloudinary.asString(options.get("resource_type"), "image");
130-
return callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, "tags", tag), only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations"), options);
130+
return callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, "tags", tag), Cloudinary.only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations"), options);
131131
}
132132

133133
public Map resourcesByIds(Iterable<String> publicIds, Map options) throws Exception {
134134
if (options == null) options = Cloudinary.emptyMap();
135135
String resourceType = Cloudinary.asString(options.get("resource_type"), "image");
136136
String type = Cloudinary.asString(options.get("type"), "upload");
137-
Map params = only(options, "tags", "context", "moderations");
137+
Map params = Cloudinary.only(options, "tags", "context", "moderations");
138138
params.put("public_ids", publicIds);
139139
return callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, type), params, options);
140140
}
141141

142142
public Map resourcesByModeration(String kind, String status, Map options) throws Exception {
143143
if (options == null) options = Cloudinary.emptyMap();
144144
String resourceType = Cloudinary.asString(options.get("resource_type"), "image");
145-
return callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, "moderations", kind, status), only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations"), options);
145+
return callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, "moderations", kind, status), Cloudinary.only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations"), options);
146146
}
147147

148148
public Map resource(String public_id, Map options) throws Exception {
149149
if (options == null) options = Cloudinary.emptyMap();
150150
String resourceType = Cloudinary.asString(options.get("resource_type"), "image");
151151
String type = Cloudinary.asString(options.get("type"), "upload");
152152
return callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, type, public_id),
153-
only(options, "exif", "colors", "faces", "image_metadata", "pages", "max_results"), options);
153+
Cloudinary.only(options, "exif", "colors", "faces", "image_metadata", "pages", "max_results"), options);
154154
}
155155

156156
public Map update(String public_id, Map options) throws Exception {
@@ -168,7 +168,7 @@ public Map deleteResources(Iterable<String> publicIds, Map options) throws Excep
168168
if (options == null) options = Cloudinary.emptyMap();
169169
String resourceType = Cloudinary.asString(options.get("resource_type"), "image");
170170
String type = Cloudinary.asString(options.get("type"), "upload");
171-
Map params = only(options, "keep_original", "next_cursor");
171+
Map params = Cloudinary.only(options, "keep_original", "next_cursor");
172172
params.put("public_ids", publicIds);
173173
return callApi(HttpMethod.DELETE, Arrays.asList("resources", resourceType, type), params, options);
174174
}
@@ -177,22 +177,22 @@ public Map deleteResourcesByPrefix(String prefix, Map options) throws Exception
177177
if (options == null) options = Cloudinary.emptyMap();
178178
String resourceType = Cloudinary.asString(options.get("resource_type"), "image");
179179
String type = Cloudinary.asString(options.get("type"), "upload");
180-
Map params = only(options, "keep_original", "next_cursor");
180+
Map params = Cloudinary.only(options, "keep_original", "next_cursor");
181181
params.put("prefix", prefix);
182182
return callApi(HttpMethod.DELETE, Arrays.asList("resources", resourceType, type), params, options);
183183
}
184184

185185
public Map deleteResourcesByTag(String tag, Map options) throws Exception {
186186
if (options == null) options = Cloudinary.emptyMap();
187187
String resourceType = Cloudinary.asString(options.get("resource_type"), "image");
188-
return callApi(HttpMethod.DELETE, Arrays.asList("resources", resourceType, "tags", tag), only(options, "keep_original", "next_cursor"), options);
188+
return callApi(HttpMethod.DELETE, Arrays.asList("resources", resourceType, "tags", tag), Cloudinary.only(options, "keep_original", "next_cursor"), options);
189189
}
190190

191191
public Map deleteAllResources(Map options) throws Exception {
192192
if (options == null) options = Cloudinary.emptyMap();
193193
String resourceType = Cloudinary.asString(options.get("resource_type"), "image");
194194
String type = Cloudinary.asString(options.get("type"), "upload");
195-
Map filtered = only(options, "keep_original", "next_cursor");
195+
Map filtered = Cloudinary.only(options, "keep_original", "next_cursor");
196196
filtered.put("all", true);
197197
return callApi(HttpMethod.DELETE, Arrays.asList("resources", resourceType, type), filtered, options);
198198
}
@@ -205,17 +205,17 @@ public Map deleteDerivedResources(Iterable<String> derivedResourceIds, Map optio
205205
public Map tags(Map options) throws Exception {
206206
if (options == null) options = Cloudinary.emptyMap();
207207
String resourceType = Cloudinary.asString(options.get("resource_type"), "image");
208-
return callApi(HttpMethod.GET, Arrays.asList("tags", resourceType), only(options, "next_cursor", "max_results", "prefix"), options);
208+
return callApi(HttpMethod.GET, Arrays.asList("tags", resourceType), Cloudinary.only(options, "next_cursor", "max_results", "prefix"), options);
209209
}
210210

211211
public Map transformations(Map options) throws Exception {
212212
if (options == null) options = Cloudinary.emptyMap();
213-
return callApi(HttpMethod.GET, Arrays.asList("transformations"), only(options, "next_cursor", "max_results"), options);
213+
return callApi(HttpMethod.GET, Arrays.asList("transformations"), Cloudinary.only(options, "next_cursor", "max_results"), options);
214214
}
215215

216216
public Map transformation(String transformation, Map options) throws Exception {
217217
if (options == null) options = Cloudinary.emptyMap();
218-
return callApi(HttpMethod.GET, Arrays.asList("transformations", transformation), only(options, "max_results"), options);
218+
return callApi(HttpMethod.GET, Arrays.asList("transformations", transformation), Cloudinary.only(options, "max_results"), options);
219219
}
220220

221221
public Map deleteTransformation(String transformation, Map options) throws Exception {
@@ -300,14 +300,4 @@ protected Map callApi(HttpMethod method, Iterable<String> uri, Map<String, ? ext
300300
throw exceptionConstructor.newInstance(message);
301301
}
302302
}
303-
304-
protected Map<String, ? extends Object> only(Map<String, ? extends Object> hash, String... keys) {
305-
Map<String, Object> result = new HashMap<String, Object>();
306-
for (String key : keys) {
307-
if (hash.containsKey(key)) {
308-
result.put(key, hash.get(key));
309-
}
310-
}
311-
return result;
312-
}
313303
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,14 @@ public static String encodeMap(Object arg) {
278278
return arg.toString();
279279
}
280280
}
281+
282+
public static Map<String, ? extends Object> only(Map<String, ? extends Object> hash, String... keys) {
283+
Map<String, Object> result = new HashMap<String, Object>();
284+
for (String key : keys) {
285+
if (hash.containsKey(key)) {
286+
result.put(key, hash.get(key));
287+
}
288+
}
289+
return result;
290+
}
281291
}

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

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -92,42 +92,48 @@ public Map uploadLargeRaw(Object file, Map options, int bufferSize) throws IOExc
9292
input = new FileInputStream(new File(file.toString()));
9393
}
9494
try {
95-
Map result = uploadLargeRawPart(input, buildUploadParams(options), bufferSize);
95+
Map result = uploadLargeRawParts(input, options, bufferSize);
9696
return result;
9797
} finally {
9898
input.close();
9999
}
100100
}
101101

102-
private Map uploadLargeRawPart(InputStream input, Map params, int bufferSize) throws IOException {
102+
private Map uploadLargeRawParts(InputStream input, Map options, int bufferSize) throws IOException {
103+
Map params = Cloudinary.only(options, "public_id", "backup", "type");
103104
Map nextParams = new HashMap();
104-
Map sentParams = new HashMap();
105105
nextParams.putAll(params);
106+
Map sentParams = new HashMap();
107+
108+
Map sentOptions = new HashMap();
109+
sentOptions.putAll(options);
110+
sentOptions.put("resource_type", "raw");
111+
106112
byte[] buffer = new byte[bufferSize];
107-
Map options = Cloudinary.asMap("resource_type", "raw");
108-
int bytesRead;
113+
int bytesRead = 0;
114+
int currentBufferSize = 0;
109115
int partNumber = 1;
110-
while ((bytesRead = input.read(buffer)) != -1) {
111-
if (bufferSize > bytesRead && bytesRead != -1) {
112-
byte[] shortBuffer = new byte[bytesRead];
113-
System.arraycopy(buffer, 0, shortBuffer, 0, bytesRead);
114-
buffer = shortBuffer;
115-
}
116-
nextParams.put("part_number", Integer.toString(partNumber));
117-
sentParams.clear();
118-
sentParams.putAll(nextParams);
119-
if (partNumber == 1) {
120-
Map response = callApi("upload_large", sentParams, options, buffer);
121-
nextParams.put("public_id", response.get("public_id"));
122-
nextParams.put("upload_id", response.get("upload_id"));
116+
while ((bytesRead = input.read(buffer, currentBufferSize, bufferSize - currentBufferSize)) != -1) {
117+
if (bytesRead + currentBufferSize == bufferSize) {
118+
nextParams.put("part_number", Integer.toString(partNumber));
119+
sentParams.clear();
120+
sentParams.putAll(nextParams);
121+
Map response = callApi("upload_large", sentParams, sentOptions, buffer);
122+
if (partNumber == 1) {
123+
nextParams.put("public_id", response.get("public_id"));
124+
nextParams.put("upload_id", response.get("upload_id"));
125+
}
126+
currentBufferSize = 0;
127+
partNumber++;
123128
} else {
124-
callApi("upload_large", sentParams, options, buffer);
129+
currentBufferSize += bytesRead;
125130
}
126-
partNumber++;
127131
}
132+
byte[] finalBuffer = new byte[currentBufferSize];
133+
System.arraycopy(buffer, 0, finalBuffer, 0, currentBufferSize);
128134
nextParams.put("final", true);
129135
nextParams.put("part_number", Integer.toString(partNumber));
130-
return callApi("upload_large", nextParams, options, new byte[0]);
136+
return callApi("upload_large", nextParams, sentOptions, finalBuffer);
131137
}
132138

133139

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy