Skip to content

Commit 349a27f

Browse files
committed
Add support for uploading remote urls through Uploader.uploadLarge()
1 parent 656958e commit 349a27f

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public Map uploadLarge(Object file, Map options, int bufferSize) throws IOExcept
112112
public Map uploadLarge(Object file, Map options, int bufferSize, ProgressCallback progressCallback) throws IOException {
113113
InputStream input;
114114
long length = -1;
115+
boolean remote = false;
115116
if (file instanceof InputStream) {
116117
input = (InputStream) file;
117118
} else if (file instanceof File) {
@@ -121,15 +122,27 @@ public Map uploadLarge(Object file, Map options, int bufferSize, ProgressCallbac
121122
length = ((byte[]) file).length;
122123
input = new ByteArrayInputStream((byte[]) file);
123124
} else {
124-
File f = new File(file.toString());
125-
length = f.length();
126-
input = new FileInputStream(f);
125+
if (StringUtils.isRemoteUrl(file.toString())){
126+
remote = true;
127+
input = null;
128+
} else {
129+
File f = new File(file.toString());
130+
length = f.length();
131+
input = new FileInputStream(f);
132+
}
127133
}
128134
try {
129-
Map result = uploadLargeParts(input, options, bufferSize, length, progressCallback);
135+
final Map result;
136+
if (remote) {
137+
result = upload(file, options);
138+
} else {
139+
result = uploadLargeParts(input, options, bufferSize, length, progressCallback);
140+
}
130141
return result;
131142
} finally {
132-
input.close();
143+
if (input != null) {
144+
input.close();
145+
}
133146
}
134147
}
135148

cloudinary-core/src/main/java/com/cloudinary/utils/StringUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,8 @@ public static String read(InputStream in) throws IOException {
113113
return new String(baos.toByteArray());
114114
}
115115

116+
public static boolean isRemoteUrl(String file) {
117+
return file.matches("ftp:.*|https?:.*|s3:.*|data:[^;]*;base64,([a-zA-Z0-9/+\n=]+)");
118+
}
119+
116120
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public Map callApi(String action, Map<String, Object> params, Map options, Objec
102102
}
103103
}
104104

105-
if (file instanceof String && !((String) file).matches("ftp:.*|https?:.*|s3:.*|data:[^;]*;base64,([a-zA-Z0-9/+\n=]+)")) {
105+
if (file instanceof String && !StringUtils.isRemoteUrl((String) file)) {
106106
File _file = new File((String) file);
107107
if (!_file.isFile() && !_file.canRead()) {
108108
throw new IOException("File not found or unreadable: " + file);

cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractUploaderTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ public void testUploadUrl() throws IOException {
119119
assertEquals(result.get("signature"), expected_signature);
120120
}
121121

122+
@Test
123+
public void testUploadLargeUrl() throws IOException {
124+
Map result = cloudinary.uploader().uploadLarge(REMOTE_TEST_IMAGE, asMap("tags", SDK_TEST_TAG));
125+
assertEquals(result.get("width"), SRC_TEST_IMAGE_W);
126+
assertEquals(result.get("height"), SRC_TEST_IMAGE_H);
127+
Map<String, Object> to_sign = new HashMap<String, Object>();
128+
to_sign.put("public_id", result.get("public_id"));
129+
to_sign.put("version", ObjectUtils.asString(result.get("version")));
130+
String expected_signature = cloudinary.apiSignRequest(to_sign, cloudinary.config.apiSecret);
131+
assertEquals(result.get("signature"), expected_signature);
132+
}
133+
122134
@Test
123135
public void testUploadDataUri() throws IOException {
124136
Map result = cloudinary.uploader().upload("data:image/png;base64,iVBORw0KGgoAA\nAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0l\nEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6\nP9/AFGGFyjOXZtQAAAAAElFTkSuQmCC", asMap("tags", Arrays.asList(SDK_TEST_TAG, UPLOADER_TAG)));

0 commit comments

Comments
 (0)