Last active
March 20, 2020 10:19
-
-
Save rah003/f3eef99e0075d5be411bd6d2618d61e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.lang.StringUtils; | |
detailFolder = 'xx' | |
rootPath = '/PL/'+ detailFolder | |
javax.jcr.NodeIterator nodeList = info.magnolia.cms.util.QueryUtil.search("dam", "select * from [mgnl:resource] as t where ISDESCENDANTNODE('"+rootPath+"') and name(t) = 'jcr:content'"); | |
while(nodeList.hasNext()) { | |
javax.jcr.Node currNode = nodeList.nextNode(); | |
println "downloading ${currNode.path}"; | |
try { | |
javax.jcr.Binary binary = currNode.getProperty("jcr:data").getBinary() | |
InputStream inputStream = binary.getStream(); | |
byte[] buffer = new byte[inputStream.available()]; | |
inputStream.read(buffer); | |
String fileName = info.magnolia.jcr.util.PropertyUtil.getString(currNode, "fileName", "noname"); | |
String extension = info.magnolia.jcr.util.PropertyUtil.getString(currNode, "extension", ".bin"); | |
String downloadFileName = fileName.endsWith(extension) ? fileName : fileName + "." + extension; | |
subpath = StringUtils.substringAfter(currNode.getParent().getPath(), rootPath) | |
File targetFolder = new File("/some/path/dam-files/"+detailFolder+"/" + subpath); | |
targetFolder.mkdirs() | |
File targetFile = new File(targetFolder, downloadFileName); | |
OutputStream outStream = new FileOutputStream(targetFile); | |
outStream.write(buffer); | |
println("File Downloaded in " + targetFile.getAbsolutePath()); | |
} catch (Exception x) { | |
println("Error:" + x); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment