X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.jcr%2Fsrc%2Forg%2Fargeo%2Fjcr%2FJcrUtils.java;h=56d959e08579c2306e662173227ae1a926c69e02;hb=3779305ca2c08e66d9ba2061c76eb79e278860fb;hp=79ae6cf62555afa9be184e8c80e431e654c46433;hpb=c83ff6ee040b68e2120ccb7d5964ce280fd642ca;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.jcr/src/org/argeo/jcr/JcrUtils.java b/org.argeo.jcr/src/org/argeo/jcr/JcrUtils.java index 79ae6cf62..56d959e08 100644 --- a/org.argeo.jcr/src/org/argeo/jcr/JcrUtils.java +++ b/org.argeo.jcr/src/org/argeo/jcr/JcrUtils.java @@ -6,10 +6,13 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.net.MalformedURLException; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.Principal; @@ -727,7 +730,7 @@ public class JcrUtils { Workspace fromWorkspace = fromSession.getWorkspace(); Workspace toWorkspace = toSession.getWorkspace(); String errorMsg = "Cannot copy workspace " + fromWorkspace + " to " + toWorkspace + " via XML."; - + try (PipedInputStream in = new PipedInputStream(1024 * 1024);) { new Thread(() -> { try (PipedOutputStream out = new PipedOutputStream(in)) { @@ -1468,6 +1471,8 @@ public class JcrUtils { // the new access control list must be applied otherwise this call: // acl.removeAccessControlEntry(ace); has no effect acm.setPolicy(path, acl); + session.refresh(true); + session.save(); } /* @@ -1575,6 +1580,7 @@ public class JcrUtils { * * @return the created file node */ + @Deprecated public static Node copyFile(Node folderNode, File file) { try (InputStream in = new FileInputStream(file)) { return copyStreamAsFile(folderNode, file.getName(), in); @@ -1619,6 +1625,7 @@ public class JcrUtils { } binary = contentNode.getSession().getValueFactory().createBinary(in); contentNode.setProperty(Property.JCR_DATA, binary); + updateLastModified(contentNode); return fileNode; } catch (RepositoryException e) { throw new JcrException("Cannot create file node " + fileName + " under " + folderNode, e); @@ -1632,6 +1639,41 @@ public class JcrUtils { return fileNode.getNode(Node.JCR_CONTENT).getProperty(Property.JCR_DATA).getBinary().getStream(); } + /** + * Set the properties of {@link NodeType#MIX_MIMETYPE} on the content of this + * file node. + */ + public static void setFileMimeType(Node fileNode, String mimeType, String encoding) throws RepositoryException { + Node contentNode = fileNode.getNode(Node.JCR_CONTENT); + if (mimeType != null) + contentNode.setProperty(Property.JCR_MIMETYPE, mimeType); + if (encoding != null) + contentNode.setProperty(Property.JCR_ENCODING, encoding); + // TODO remove properties if args are null? + } + + public static void copyFilesToFs(Node baseNode, Path targetDir, boolean recursive) { + try { + Files.createDirectories(targetDir); + for (NodeIterator nit = baseNode.getNodes(); nit.hasNext();) { + Node node = nit.nextNode(); + if (node.isNodeType(NodeType.NT_FILE)) { + Path filePath = targetDir.resolve(node.getName()); + try (OutputStream out = Files.newOutputStream(filePath); InputStream in = getFileAsStream(node)) { + IOUtils.copy(in, out); + } + } else if (recursive && node.isNodeType(NodeType.NT_FOLDER)) { + Path dirPath = targetDir.resolve(node.getName()); + copyFilesToFs(node, dirPath, true); + } + } + } catch (RepositoryException e) { + throw new JcrException("Cannot copy " + baseNode + " to " + targetDir, e); + } catch (IOException e) { + throw new RuntimeException("Cannot copy " + baseNode + " to " + targetDir, e); + } + } + /** * Computes the checksum of an nt:file. *