Skip to content

Commit 87df64e

Browse files
committed
support folder listing API
1 parent f3f6c94 commit 87df64e

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,18 @@ public ApiResponse createUploadPreset(Map options) throws Exception {
357357
params.putAll(Cloudinary.only(options, "name", "unsigned", "disallow_public_id"));
358358
return callApi(HttpMethod.POST, Arrays.asList("upload_presets"), params, options);
359359
}
360+
361+
public ApiResponse rootFolders(Map options) throws Exception {
362+
if (options == null)
363+
options = Cloudinary.emptyMap();
364+
return callApi(HttpMethod.GET, Arrays.asList("folders"), Cloudinary.emptyMap(), options);
365+
}
366+
367+
public ApiResponse subFolders(String ofFolderPath, Map options) throws Exception {
368+
if (options == null)
369+
options = Cloudinary.emptyMap();
370+
return callApi(HttpMethod.GET, Arrays.asList("folders", ofFolderPath), Cloudinary.emptyMap(), options);
371+
}
360372

361373
public Api withConnectionManager(ClientConnectionManager connectionManager) {
362374
this.connectionManager = connectionManager;

cloudinary-core/src/test/java/com/cloudinary/test/ApiTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,34 @@ public void testListByModerationUpdate() throws Exception {
624624
"public_id", (String) result2.get("public_id")));
625625
}
626626

627+
// For this test to work, "Auto-create folders" should be enabled in the
628+
// Upload Settings.
629+
// Uncomment @Test if you really want to test it.
630+
// @Test
631+
public void testFolderApi() throws Exception {
632+
// should allow deleting all resources
633+
cloudinary.uploader().upload("src/test/resources/logo.png", Cloudinary.asMap("public_id", "test_folder1/item"));
634+
cloudinary.uploader().upload("src/test/resources/logo.png", Cloudinary.asMap("public_id", "test_folder2/item"));
635+
cloudinary.uploader().upload("src/test/resources/logo.png",
636+
Cloudinary.asMap("public_id", "test_folder1/test_subfolder1/item"));
637+
cloudinary.uploader().upload("src/test/resources/logo.png",
638+
Cloudinary.asMap("public_id", "test_folder1/test_subfolder2/item"));
639+
Map result = api.rootFolders(null);
640+
assertEquals("test_folder1", ((Map) ((org.json.simple.JSONArray) result.get("folders")).get(0)).get("name"));
641+
assertEquals("test_folder2", ((Map) ((org.json.simple.JSONArray) result.get("folders")).get(1)).get("name"));
642+
result = api.subFolders("test_folder1", null);
643+
assertEquals("test_folder1/test_subfolder1",
644+
((Map) ((org.json.simple.JSONArray) result.get("folders")).get(0)).get("path"));
645+
assertEquals("test_folder1/test_subfolder2",
646+
((Map) ((org.json.simple.JSONArray) result.get("folders")).get(1)).get("path"));
647+
try {
648+
api.subFolders("test_folder", null);
649+
} catch (Exception e) {
650+
assertTrue(e instanceof com.cloudinary.Api.NotFound);
651+
}
652+
api.deleteResourcesByPrefix("test_folder", Cloudinary.emptyMap());
653+
}
654+
627655
private void assertContains(Object object, Collection list) {
628656
assertTrue(list.contains(object));
629657
}

0 commit comments

Comments
 (0)