X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.core%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fcore%2Fattachment%2FFileAttachmentsStorage.java;fp=runtime%2Forg.argeo.slc.core%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fcore%2Fattachment%2FFileAttachmentsStorage.java;h=0e70805c061c602f10e678d9063ef176f503d060;hb=ee6c3543a0ff9403420ce6a9c647723269f14331;hp=0000000000000000000000000000000000000000;hpb=9daa55ce316d52ffd8f30dc0d1b516ccf78a8c73;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/attachment/FileAttachmentsStorage.java b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/attachment/FileAttachmentsStorage.java new file mode 100644 index 000000000..0e70805c0 --- /dev/null +++ b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/attachment/FileAttachmentsStorage.java @@ -0,0 +1,77 @@ +package org.argeo.slc.core.attachment; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.commons.io.IOUtils; +import org.argeo.slc.SlcException; + +public class FileAttachmentsStorage implements AttachmentsStorage { + private File attachmentsDirectory; + + public FileAttachmentsStorage() { + String osgiInstanceArea = System.getProperty("osgi.instance.area"); + if (osgiInstanceArea != null) { + if (osgiInstanceArea.startsWith("file:")) + osgiInstanceArea = osgiInstanceArea.substring("file:".length()); + attachmentsDirectory = new File(osgiInstanceArea + File.separator + + "slcAttachments"); + } + + if (attachmentsDirectory == null) { + String tempDir = System.getProperty("java.io.tmpdir"); + attachmentsDirectory = new File(tempDir + File.separator + + "slcAttachments"); + } + } + + public void retrieveAttachment(Attachment attachment, + OutputStream outputStream) { + File file = getFile(attachment); + InputStream in = null; + try { + byte[] buffer = new byte[1024 * 1024]; + in = new FileInputStream(file); + int read = -1; + while ((read = in.read(buffer)) >= 0) { + outputStream.write(buffer, 0, read); + } + } catch (IOException e) { + throw new SlcException("Cannot write attachment " + attachment + + " to " + file, e); + } finally { + IOUtils.closeQuietly(in); + } + } + + public void storeAttachment(Attachment attachment, InputStream inputStream) { + File file = getFile(attachment); + FileOutputStream out = null; + try { + byte[] buffer = new byte[1024 * 1024]; + out = new FileOutputStream(file); + int read = -1; + while ((read = inputStream.read(buffer)) >= 0) { + out.write(buffer, 0, read); + } + } catch (IOException e) { + throw new SlcException("Cannot write attachment " + attachment + + " to " + file, e); + } finally { + IOUtils.closeQuietly(out); + } + + } + + protected File getFile(Attachment attachment) { + if (!attachmentsDirectory.exists()) + attachmentsDirectory.mkdirs(); + File file = new File(attachmentsDirectory + File.separator + + attachment.getUuid()); + return file; + } +}