Skip to content

Commit a1eb3ad

Browse files
authored
Merge pull request #81 from cloudinary/fix/upload-large-url
Add support for uploading remote urls through `Uploader.uploadLarge()`
2 parents 8f8285b + ff93e78 commit a1eb3ad

File tree

5 files changed

+43
-13
lines changed

5 files changed

+43
-13
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
@@ -190,4 +190,8 @@ public static String read(InputStream in) throws IOException {
190190
return new String(baos.toByteArray());
191191
}
192192

193+
public static boolean isRemoteUrl(String file) {
194+
return file.matches("ftp:.*|https?:.*|s3:.*|data:[^;]*;base64,([a-zA-Z0-9/+\n=]+)");
195+
}
196+
193197
}

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/AbstractApiTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ abstract public class AbstractApiTest extends MockableTest {
4545
public static final String DELETE_TRANSFORMATION_NAME = "c_scale,l_text:Arial_60:" + SUFFIX + "_delete,w_100";
4646
public static final Transformation DELETE_TRANSFORMATION = new Transformation().width(100).crop("scale").overlay(new TextLayer().text(SUFFIX + "_delete").fontFamily("Arial").fontSize(60));
4747
public static final String TEST_KEY = "test-key" + SUFFIX;
48+
public static final String API_TEST_RESTORE = "api_test_restore" + SUFFIX;
4849

4950
protected Api api;
5051

@@ -708,18 +709,18 @@ public void testFolderApi() throws Exception {
708709
public void testRestore() throws Exception {
709710
// should support restoring resources
710711
cloudinary.uploader().upload(SRC_TEST_IMAGE,
711-
ObjectUtils.asMap("public_id", "api_test_restore", "backup", true, "tags", UPLOAD_TAGS));
712-
Map resource = api.resource("api_test_restore", ObjectUtils.emptyMap());
712+
ObjectUtils.asMap("public_id", API_TEST_RESTORE, "backup", true, "tags", UPLOAD_TAGS));
713+
Map resource = api.resource(API_TEST_RESTORE, ObjectUtils.emptyMap());
713714
assertEquals(resource.get("bytes"), 3381);
714-
api.deleteResources(Collections.singletonList("api_test_restore"), ObjectUtils.emptyMap());
715-
resource = api.resource("api_test_restore", ObjectUtils.emptyMap());
715+
api.deleteResources(Collections.singletonList(API_TEST_RESTORE), ObjectUtils.emptyMap());
716+
resource = api.resource(API_TEST_RESTORE, ObjectUtils.emptyMap());
716717
assertEquals(resource.get("bytes"), 0);
717718
assertTrue((Boolean) resource.get("placeholder"));
718-
Map response = api.restore(Collections.singletonList("api_test_restore"), ObjectUtils.emptyMap());
719-
Map info = (Map) response.get("api_test_restore");
719+
Map response = api.restore(Collections.singletonList(API_TEST_RESTORE), ObjectUtils.emptyMap());
720+
Map info = (Map) response.get(API_TEST_RESTORE);
720721
assertNotNull(info);
721722
assertEquals(info.get("bytes"), 3381);
722-
resource = api.resource("api_test_restore", ObjectUtils.emptyMap());
723+
resource = api.resource(API_TEST_RESTORE, ObjectUtils.emptyMap());
723724
assertEquals(resource.get("bytes"), 3381);
724725
}
725726

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)