Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions cloudinary-core/src/main/java/com/cloudinary/Uploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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=]+)");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public Map callApi(String action, Map<String, Object> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> to_sign = new HashMap<String, Object>();
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)));
Expand Down