1 package org
.argeo
.slc
.core
.attachment
;
4 import java
.io
.FileInputStream
;
5 import java
.io
.FileOutputStream
;
6 import java
.io
.IOException
;
7 import java
.io
.InputStream
;
8 import java
.io
.OutputStream
;
10 import org
.apache
.commons
.io
.IOUtils
;
11 import org
.argeo
.slc
.SlcException
;
13 public class FileAttachmentsStorage
implements AttachmentsStorage
{
14 private File attachmentsDirectory
;
16 public FileAttachmentsStorage() {
17 String osgiInstanceArea
= System
.getProperty("osgi.instance.area");
18 if (osgiInstanceArea
!= null) {
19 if (osgiInstanceArea
.startsWith("file:"))
20 osgiInstanceArea
= osgiInstanceArea
.substring("file:".length());
21 attachmentsDirectory
= new File(osgiInstanceArea
+ File
.separator
25 if (attachmentsDirectory
== null) {
26 String tempDir
= System
.getProperty("java.io.tmpdir");
27 attachmentsDirectory
= new File(tempDir
+ File
.separator
32 public void retrieveAttachment(Attachment attachment
,
33 OutputStream outputStream
) {
34 File file
= getFile(attachment
);
35 InputStream in
= null;
37 byte[] buffer
= new byte[1024 * 1024];
38 in
= new FileInputStream(file
);
39 while (in
.read(buffer
) >= 0) {
40 outputStream
.write(buffer
);
42 } catch (IOException e
) {
43 throw new SlcException("Cannot write attachment " + attachment
46 IOUtils
.closeQuietly(in
);
50 public void storeAttachment(Attachment attachment
, InputStream inputStream
) {
51 File file
= getFile(attachment
);
52 FileOutputStream out
= null;
54 byte[] buffer
= new byte[1024 * 1024];
55 out
= new FileOutputStream(file
);
57 while ((read
= inputStream
.read(buffer
)) >= 0) {
58 out
.write(buffer
, 0, read
);
60 } catch (IOException e
) {
61 throw new SlcException("Cannot write attachment " + attachment
64 IOUtils
.closeQuietly(out
);
69 protected File
getFile(Attachment attachment
) {
70 if (!attachmentsDirectory
.exists())
71 attachmentsDirectory
.mkdirs();
72 File file
= new File(attachmentsDirectory
+ File
.separator
73 + attachment
.getUuid());