Improve files management in JCR.
[lgpl/argeo-commons.git] / org.argeo.jcr / src / org / argeo / jcr / fs / BinaryChannel.java
index b90bb900ae129827768f277831a86e17a128f018..658fe93302981f309fc1e8483ea5bc6b8fe4cda5 100644 (file)
@@ -19,7 +19,6 @@ import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.nodetype.NodeType;
 
-import org.apache.commons.io.IOUtils;
 import org.argeo.jcr.JcrUtils;
 
 public class BinaryChannel implements SeekableByteChannel {
@@ -29,23 +28,26 @@ public class BinaryChannel implements SeekableByteChannel {
 
        private long position = 0;
 
-       // private ByteBuffer toWrite;
        private FileChannel fc = null;
 
-       public BinaryChannel(Node file) throws RepositoryException, IOException {
+       public BinaryChannel(Node file, Path path) throws RepositoryException, IOException {
                this.file = file;
-               // int capacity = 1024 * 1024;
-               // this.toWrite = ByteBuffer.allocate(capacity);
                if (file.isNodeType(NodeType.NT_FILE)) {
                        if (file.hasNode(Property.JCR_CONTENT)) {
                                Node data = file.getNode(Property.JCR_CONTENT);
                                this.binary = data.getProperty(Property.JCR_DATA).getBinary();
                        } else {
-                               Node data = file.addNode(Property.JCR_CONTENT, NodeType.NT_RESOURCE);
+                               Node data = file.addNode(Property.JCR_CONTENT, NodeType.NT_UNSTRUCTURED);
                                try (InputStream in = new ByteArrayInputStream(new byte[0])) {
                                        this.binary = data.getSession().getValueFactory().createBinary(in);
                                }
                                data.setProperty(Property.JCR_DATA, this.binary);
+
+                               // MIME type
+                               String mime = Files.probeContentType(path);
+                               // String mime = fileTypeMap.getContentType(file.getName());
+                               data.setProperty(Property.JCR_MIMETYPE, mime);
+
                                data.getSession().save();
                        }
                } else {
@@ -65,9 +67,6 @@ public class BinaryChannel implements SeekableByteChannel {
                        Binary newBinary = null;
                        try {
                                Session session = file.getSession();
-                               // byte[] arr = new byte[(int) position];
-                               // toWrite.flip();
-                               // toWrite.get(arr);
                                fc.position(0);
                                InputStream in = Channels.newInputStream(fc);
                                newBinary = session.getValueFactory().createBinary(in);
@@ -75,10 +74,13 @@ public class BinaryChannel implements SeekableByteChannel {
                                session.save();
                                open = false;
                        } catch (RepositoryException e) {
-                               throw new JcrFsException("Cannot close " + file, e);
+                               throw new IOException("Cannot close " + file, e);
                        } finally {
                                JcrUtils.closeQuietly(newBinary);
-                               IOUtils.closeQuietly(fc);
+                               // IOUtils.closeQuietly(fc);
+                               if (fc != null) {
+                                       fc.close();
+                               }
                        }
                } else {
                        clearReadState();
@@ -94,25 +96,14 @@ public class BinaryChannel implements SeekableByteChannel {
 
                        try {
                                int read;
-                               // int capacity = dst.capacity();
                                byte[] arr = dst.array();
                                read = binary.read(arr, position);
-                               // dst.put(arr, 0, read);
-
-                               // try {
-                               // byte[] arr = dst.array();
-                               // read = binary.read(arr, position);
-                               // } catch (UnsupportedOperationException e) {
-                               // int capacity = dst.capacity();
-                               // byte[] arr = new byte[capacity];
-                               // read = binary.read(arr, position);
-                               // dst.put(arr);
-                               // }
+
                                if (read != -1)
                                        position = position + read;
                                return read;
                        } catch (RepositoryException e) {
-                               throw new JcrFsException("Cannot read into buffer", e);
+                               throw new IOException("Cannot read into buffer", e);
                        }
                }
        }
@@ -121,15 +112,6 @@ public class BinaryChannel implements SeekableByteChannel {
        public int write(ByteBuffer src) throws IOException {
                int written = getFileChannel().write(src);
                return written;
-               // int byteCount = src.remaining();
-               // if (toWrite.remaining() < byteCount)
-               // throw new JcrFsException("Write buffer is full");
-               // toWrite.put(src);
-               // if (position < binarySize)
-               // position = binarySize + byteCount;
-               // else
-               // position = position + byteCount;
-               // return byteCount;
        }
 
        @Override
@@ -158,7 +140,7 @@ public class BinaryChannel implements SeekableByteChannel {
                        try {
                                return binary.getSize();
                        } catch (RepositoryException e) {
-                               throw new JcrFsException("Cannot get size", e);
+                               throw new IOException("Cannot get size", e);
                        }
                }
        }
@@ -166,9 +148,6 @@ public class BinaryChannel implements SeekableByteChannel {
        @Override
        public SeekableByteChannel truncate(long size) throws IOException {
                getFileChannel().truncate(size);
-               // if (size != size())
-               // throw new UnsupportedOperationException("Cannot truncate JCR
-               // binary");
                return this;
        }
 
@@ -184,7 +163,7 @@ public class BinaryChannel implements SeekableByteChannel {
                        }
                        return fc;
                } catch (RepositoryException e) {
-                       throw new JcrFsException("Cannot get temp file channel", e);
+                       throw new IOException("Cannot get temp file channel", e);
                }
        }