diff --git a/cloudinary-core/src/main/java/com/cloudinary/Uploader.java b/cloudinary-core/src/main/java/com/cloudinary/Uploader.java index 78684b73..7b329455 100644 --- a/cloudinary-core/src/main/java/com/cloudinary/Uploader.java +++ b/cloudinary-core/src/main/java/com/cloudinary/Uploader.java @@ -112,6 +112,7 @@ public Map uploadLarge(Object file, Map options, int bufferSize) throws IOExcept public Map uploadLarge(Object file, Map options, int bufferSize, ProgressCallback progressCallback) throws IOException { InputStream input; long length = -1; + boolean remote = false; if (file instanceof InputStream) { input = (InputStream) file; } else if (file instanceof File) { @@ -121,15 +122,27 @@ public Map uploadLarge(Object file, Map options, int bufferSize, ProgressCallbac length = ((byte[]) file).length; input = new ByteArrayInputStream((byte[]) file); } else { - File f = new File(file.toString()); - length = f.length(); - input = new FileInputStream(f); + if (StringUtils.isRemoteUrl(file.toString())){ + remote = true; + input = null; + } else { + File f = new File(file.toString()); + length = f.length(); + input = new FileInputStream(f); + } } try { - Map result = uploadLargeParts(input, options, bufferSize, length, progressCallback); + final Map result; + if (remote) { + result = upload(file, options); + } else { + result = uploadLargeParts(input, options, bufferSize, length, progressCallback); + } return result; } finally { - input.close(); + if (input != null) { + input.close(); + } } } diff --git a/cloudinary-core/src/main/java/com/cloudinary/utils/StringUtils.java b/cloudinary-core/src/main/java/com/cloudinary/utils/StringUtils.java index 5a364422..e9d56f8f 100644 --- a/cloudinary-core/src/main/java/com/cloudinary/utils/StringUtils.java +++ b/cloudinary-core/src/main/java/com/cloudinary/utils/StringUtils.java @@ -190,4 +190,8 @@ public static String read(InputStream in) throws IOException { return new String(baos.toByteArray()); } + public static boolean isRemoteUrl(String file) { + return file.matches("ftp:.*|https?:.*|s3:.*|data:[^;]*;base64,([a-zA-Z0-9/+\n=]+)"); + } + } diff --git a/cloudinary-http44/src/main/java/com/cloudinary/http44/UploaderStrategy.java b/cloudinary-http44/src/main/java/com/cloudinary/http44/UploaderStrategy.java index 617e5923..e509e321 100644 --- a/cloudinary-http44/src/main/java/com/cloudinary/http44/UploaderStrategy.java +++ b/cloudinary-http44/src/main/java/com/cloudinary/http44/UploaderStrategy.java @@ -102,7 +102,7 @@ public Map callApi(String action, Map params, Map options, Objec } } - if (file instanceof String && !((String) file).matches("ftp:.*|https?:.*|s3:.*|data:[^;]*;base64,([a-zA-Z0-9/+\n=]+)")) { + if (file instanceof String && !StringUtils.isRemoteUrl((String) file)) { File _file = new File((String) file); if (!_file.isFile() && !_file.canRead()) { throw new IOException("File not found or unreadable: " + file); diff --git a/cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractApiTest.java b/cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractApiTest.java index a3c5e920..7fda665d 100644 --- a/cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractApiTest.java +++ b/cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractApiTest.java @@ -45,6 +45,7 @@ abstract public class AbstractApiTest extends MockableTest { public static final String DELETE_TRANSFORMATION_NAME = "c_scale,l_text:Arial_60:" + SUFFIX + "_delete,w_100"; public static final Transformation DELETE_TRANSFORMATION = new Transformation().width(100).crop("scale").overlay(new TextLayer().text(SUFFIX + "_delete").fontFamily("Arial").fontSize(60)); public static final String TEST_KEY = "test-key" + SUFFIX; + public static final String API_TEST_RESTORE = "api_test_restore" + SUFFIX; protected Api api; @@ -708,18 +709,18 @@ public void testFolderApi() throws Exception { public void testRestore() throws Exception { // should support restoring resources cloudinary.uploader().upload(SRC_TEST_IMAGE, - ObjectUtils.asMap("public_id", "api_test_restore", "backup", true, "tags", UPLOAD_TAGS)); - Map resource = api.resource("api_test_restore", ObjectUtils.emptyMap()); + ObjectUtils.asMap("public_id", API_TEST_RESTORE, "backup", true, "tags", UPLOAD_TAGS)); + Map resource = api.resource(API_TEST_RESTORE, ObjectUtils.emptyMap()); assertEquals(resource.get("bytes"), 3381); - api.deleteResources(Collections.singletonList("api_test_restore"), ObjectUtils.emptyMap()); - resource = api.resource("api_test_restore", ObjectUtils.emptyMap()); + api.deleteResources(Collections.singletonList(API_TEST_RESTORE), ObjectUtils.emptyMap()); + resource = api.resource(API_TEST_RESTORE, ObjectUtils.emptyMap()); assertEquals(resource.get("bytes"), 0); assertTrue((Boolean) resource.get("placeholder")); - Map response = api.restore(Collections.singletonList("api_test_restore"), ObjectUtils.emptyMap()); - Map info = (Map) response.get("api_test_restore"); + Map response = api.restore(Collections.singletonList(API_TEST_RESTORE), ObjectUtils.emptyMap()); + Map info = (Map) response.get(API_TEST_RESTORE); assertNotNull(info); assertEquals(info.get("bytes"), 3381); - resource = api.resource("api_test_restore", ObjectUtils.emptyMap()); + resource = api.resource(API_TEST_RESTORE, ObjectUtils.emptyMap()); assertEquals(resource.get("bytes"), 3381); } diff --git a/cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractUploaderTest.java b/cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractUploaderTest.java index 0d4a60b7..ca1efe97 100644 --- a/cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractUploaderTest.java +++ b/cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractUploaderTest.java @@ -119,6 +119,18 @@ public void testUploadUrl() throws IOException { assertEquals(result.get("signature"), expected_signature); } + @Test + public void testUploadLargeUrl() throws IOException { + Map result = cloudinary.uploader().uploadLarge(REMOTE_TEST_IMAGE, asMap("tags", SDK_TEST_TAG)); + assertEquals(result.get("width"), SRC_TEST_IMAGE_W); + assertEquals(result.get("height"), SRC_TEST_IMAGE_H); + Map to_sign = new HashMap(); + to_sign.put("public_id", result.get("public_id")); + to_sign.put("version", ObjectUtils.asString(result.get("version"))); + String expected_signature = cloudinary.apiSignRequest(to_sign, cloudinary.config.apiSecret); + assertEquals(result.get("signature"), expected_signature); + } + @Test public void testUploadDataUri() throws IOException { 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)));