X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.jcr%2Fsrc%2Forg%2Fargeo%2Fjcr%2Ffs%2FBinaryChannel.java;h=94eb70482b6751ba3b7d62b98b138acab37a891f;hb=86ea60b48b0e813e5227be733d3cdd46f190665d;hp=2039f778a7a67231eab5eab4b1fd757d19044445;hpb=1d5ef69cc76d286d59de4f02d90196ca52cbe0f3;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.jcr/src/org/argeo/jcr/fs/BinaryChannel.java b/org.argeo.jcr/src/org/argeo/jcr/fs/BinaryChannel.java index 2039f778a..94eb70482 100644 --- a/org.argeo.jcr/src/org/argeo/jcr/fs/BinaryChannel.java +++ b/org.argeo.jcr/src/org/argeo/jcr/fs/BinaryChannel.java @@ -12,6 +12,8 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; +import javax.activation.FileTypeMap; +import javax.activation.MimetypesFileTypeMap; import javax.jcr.Binary; import javax.jcr.Node; import javax.jcr.Property; @@ -31,6 +33,16 @@ public class BinaryChannel implements SeekableByteChannel { // private ByteBuffer toWrite; private FileChannel fc = null; + private static FileTypeMap fileTypeMap; + + static { + try { + fileTypeMap = new MimetypesFileTypeMap("/etc/mime.types"); + } catch (IOException e) { + fileTypeMap = FileTypeMap.getDefaultFileTypeMap(); + } + } + public BinaryChannel(Node file) throws RepositoryException, IOException { this.file = file; // int capacity = 1024 * 1024; @@ -45,6 +57,11 @@ public class BinaryChannel implements SeekableByteChannel { this.binary = data.getSession().getValueFactory().createBinary(in); } data.setProperty(Property.JCR_DATA, this.binary); + + // MIME type + String mime = fileTypeMap.getContentType(file.getName()); + data.setProperty(Property.JCR_MIMETYPE, mime); + data.getSession().save(); } } else { @@ -74,7 +91,7 @@ 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); @@ -114,7 +131,7 @@ public class BinaryChannel implements SeekableByteChannel { position = position + read; return read; } catch (RepositoryException e) { - throw new JcrFsException("Cannot read into buffer", e); + throw new IOException("Cannot read into buffer", e); } } } @@ -160,7 +177,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); } } } @@ -186,7 +203,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); } }