Skip to content

Commit e4e73a1

Browse files
author
RTLcoil
authored
Add downloadFolder method (#219)
1 parent 470de5c commit e4e73a1

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,31 @@ public String downloadMulti(String[] urls, Map options) throws IOException {
284284
return buildUrl(cloudinaryApiUrl("multi", options), params);
285285
}
286286

287+
/**
288+
* Generates URL for executing "Download Folder" operation on Cloudinary site.
289+
*
290+
* @param folderPath path of folder to generate download URL for
291+
* @param options optional, holds hints for URL generation procedure, see documentation for full list
292+
* @return generated URL for downloading specified folder as ZIP archive
293+
*/
294+
public String downloadFolder(String folderPath, Map options) throws UnsupportedEncodingException {
295+
if (StringUtils.isEmpty(folderPath)) {
296+
throw new IllegalArgumentException("Folder path parameter value is required");
297+
}
298+
299+
Map adjustedOptions = new HashMap();
300+
if (options != null) {
301+
adjustedOptions.putAll(options);
302+
}
303+
304+
adjustedOptions.put("prefixes", folderPath);
305+
306+
final Object resourceType = adjustedOptions.get("resource_type");
307+
adjustedOptions.put("resource_type", resourceType != null ? resourceType : "all");
308+
309+
return downloadArchive(adjustedOptions, (String) adjustedOptions.get("target_format"));
310+
}
311+
287312
private String buildUrl(String base, Map<String, Object> params) throws UnsupportedEncodingException {
288313
StringBuilder urlBuilder = new StringBuilder();
289314
urlBuilder.append(base);

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,37 @@ public void testDownloadMulti() throws Exception{
702702
assertNotNull(parameters.get("signature"));
703703

704704
}
705+
706+
@Test
707+
public void testDownloadFolderShouldReturnURLWithResourceTypeAllByDefault() throws UnsupportedEncodingException {
708+
String url = cloudinary.downloadFolder("folder", null);
709+
assertTrue(url.contains("all"));
710+
}
711+
712+
@Test
713+
public void testDownloadFolderShouldAllowToOverrideResourceType() throws UnsupportedEncodingException {
714+
String url = cloudinary.downloadFolder("folder", Collections.singletonMap("resource_type", "audio"));
715+
assertTrue(url.contains("audio"));
716+
}
717+
718+
@Test
719+
public void testDownloadFolderShouldPutFolderPathAsPrefixes() throws UnsupportedEncodingException {
720+
String url = cloudinary.downloadFolder("folder", null);
721+
assertTrue(url.contains("prefixes[]=folder"));
722+
}
723+
724+
@Test
725+
public void testDownloadFolderShouldIncludeSpecifiedTargetFormat() throws UnsupportedEncodingException {
726+
String url = cloudinary.downloadFolder("folder", Collections.singletonMap("target_format", "rar"));
727+
assertTrue(url.contains("target_format=rar"));
728+
}
729+
730+
@Test
731+
public void testDownloadFolderShouldNotIncludeTargetFormatIfNotSpecified() throws UnsupportedEncodingException {
732+
String url = cloudinary.downloadFolder("folder", null);
733+
assertFalse(url.contains("target_format"));
734+
}
735+
705736
@Test
706737
public void testSpriteCss() {
707738
String result = cloudinary.url().generateSpriteCss("test");

0 commit comments

Comments
 (0)