Skip to content

Instantly share code, notes, and snippets.

@rah003
Created February 20, 2020 13:10
Show Gist options
  • Save rah003/82a2f0d0468e84f9b9d6b0ee02772ca1 to your computer and use it in GitHub Desktop.
Save rah003/82a2f0d0468e84f9b9d6b0ee02772ca1 to your computer and use it in GitHub Desktop.
import org.apache.commons.lang.StringUtils;
detailFolder = 'ke-stazeni'
rootPath = '/rjelinek/'+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("/home/neatresults/rjelinek/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