Skip to content

Add support for folder deletion #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 24, 2019
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
12 changes: 12 additions & 0 deletions cloudinary-core/src/main/java/com/cloudinary/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,18 @@ public ApiResponse updateResourcesAccessModeByTag(String accessMode, String tag,
return updateResourcesAccessMode(accessMode, "tag", tag, options);
}

/**
* Delete a folder (must be empty).
* @param folder The full path of the folder to delete
* @param options additional options.
* @return The operation result.
* @throws Exception When the folder isn't empty or doesn't exist.
*/
public ApiResponse deleteFolder(String folder, Map options) throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nitzanj , please add a javadoc and explain how the 'folder' string supposed to look like

List<String> uri = Arrays.asList("folders", folder);
return callApi(HttpMethod.DELETE, uri, Collections.<String, Object>emptyMap(), options);
}

/**
* Update access mode of one or more resources by publicIds
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.*;

import static com.cloudinary.utils.ObjectUtils.asMap;
import static com.cloudinary.utils.ObjectUtils.emptyMap;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.AllOf.allOf;
import static org.hamcrest.core.IsNot.not;
Expand Down Expand Up @@ -935,4 +936,17 @@ public void testQualityAnalysis() throws Exception {
ApiResponse result = cloudinary.api().resource(API_TEST, ObjectUtils.asMap("quality_analysis", true));
assertNotNull(result.get("quality_analysis"));
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nitzanj , what are we expected to get when deleting a non-existing folder or a non-empty one?
Let's add a test

@Test(expected = NotFound.class)
public void testDeleteFolder() throws Exception {
String toDelete = "todelete_" + SUFFIX;
Map uploadResult = cloudinary.uploader().upload(SRC_TEST_IMAGE, asMap("tags", UPLOAD_TAGS, "folder", toDelete));
Thread.sleep(5000);
api.deleteResources(Collections.singletonList(uploadResult.get("public_id").toString()), emptyMap());
ApiResponse result = api.deleteFolder(toDelete, emptyMap());
assertTrue(((ArrayList)result.get("deleted")).contains(toDelete));

// should throw exception (folder not found):
api.deleteFolder(cloudinary.randomPublicId(), emptyMap());
}
}