1 package org
.argeo
.slc
.core
.attachment
;
4 import java
.io
.FileInputStream
;
5 import java
.io
.FileOutputStream
;
6 import java
.io
.FileWriter
;
7 import java
.io
.IOException
;
8 import java
.io
.InputStream
;
9 import java
.io
.OutputStream
;
10 import java
.text
.DateFormat
;
11 import java
.text
.SimpleDateFormat
;
12 import java
.util
.Date
;
14 import org
.apache
.commons
.io
.IOUtils
;
15 import org
.apache
.commons
.logging
.Log
;
16 import org
.apache
.commons
.logging
.LogFactory
;
17 import org
.argeo
.slc
.SlcException
;
18 import org
.springframework
.beans
.factory
.InitializingBean
;
20 public class FileAttachmentsStorage
implements AttachmentsStorage
,
22 private final static Log log
= LogFactory
23 .getLog(FileAttachmentsStorage
.class);
25 private File attachmentsDirectory
;
27 private String attachmentsTocFileName
= "attachmentsToc.csv";
29 private DateFormat dateFormatDay
= new SimpleDateFormat("yyyy-MM-dd");
30 private DateFormat dateFormatTime
= new SimpleDateFormat("HH:mm:ss");
32 public void afterPropertiesSet() {
33 if (attachmentsDirectory
== null) {
35 String osgiInstanceArea
= System
.getProperty("osgi.instance.area");
36 if (osgiInstanceArea
!= null) {
37 if (osgiInstanceArea
.startsWith("file:"))
38 osgiInstanceArea
= osgiInstanceArea
.substring("file:"
40 attachmentsDirectory
= new File(osgiInstanceArea
41 + File
.separator
+ "slcAttachments");
44 if (attachmentsDirectory
== null) {
45 String tempDir
= System
.getProperty("java.io.tmpdir");
46 attachmentsDirectory
= new File(tempDir
+ File
.separator
50 if (!attachmentsDirectory
.exists())
51 attachmentsDirectory
.mkdirs();
52 if (log
.isDebugEnabled())
53 log
.debug("File attachment storage initialized in directory "
54 + attachmentsDirectory
);
57 public void retrieveAttachment(Attachment attachment
,
58 OutputStream outputStream
) {
59 File file
= getFile(attachment
);
60 InputStream in
= null;
62 byte[] buffer
= new byte[1024 * 1024];
63 in
= new FileInputStream(file
);
65 while ((read
= in
.read(buffer
)) >= 0) {
66 outputStream
.write(buffer
, 0, read
);
68 if (log
.isTraceEnabled())
69 log
.trace("Read " + attachment
+ " from " + file
);
70 } catch (IOException e
) {
71 throw new SlcException("Cannot write attachment " + attachment
74 IOUtils
.closeQuietly(in
);
78 public void storeAttachment(Attachment attachment
, InputStream inputStream
) {
79 File file
= getFile(attachment
);
80 FileOutputStream out
= null;
82 byte[] buffer
= new byte[1024 * 1024];
83 out
= new FileOutputStream(file
);
85 while ((read
= inputStream
.read(buffer
)) >= 0) {
86 out
.write(buffer
, 0, read
);
88 if (log
.isTraceEnabled())
89 log
.trace("Wrote " + attachment
+ " to " + file
);
90 updateAttachmentToc(attachment
, file
);
91 } catch (IOException e
) {
92 throw new SlcException("Cannot write attachment " + attachment
95 IOUtils
.closeQuietly(out
);
100 /** For monitoring purposes only */
101 protected void updateAttachmentToc(Attachment attachment
, File file
) {
102 Date date
= new Date(file
.lastModified());
103 FileWriter writer
= null;
105 writer
= new FileWriter(attachmentsDirectory
+ File
.separator
106 + attachmentsTocFileName
, true);
107 writer
.append(dateFormatDay
.format(date
));
109 writer
.append(dateFormatTime
.format(date
));
111 writer
.append(attachment
.getUuid());
113 writer
.append(attachment
.getName());
115 writer
.append(attachment
.getContentType());
117 writer
.append(Long
.toString(file
.length()));
119 writer
.append(file
.getCanonicalPath());
121 } catch (IOException e
) {
122 log
.warn("Could not update attachments TOC for " + attachment
123 + " and file " + file
, e
);
125 IOUtils
.closeQuietly(writer
);
130 protected File
getFile(Attachment attachment
) {
131 File file
= new File(attachmentsDirectory
+ File
.separator
132 + attachment
.getUuid());
136 public void setAttachmentsDirectory(File attachmentsDirectory
) {
137 this.attachmentsDirectory
= attachmentsDirectory
;
140 public void setAttachmentsTocFileName(String attachmentsTocFileName
) {
141 this.attachmentsTocFileName
= attachmentsTocFileName
;
144 public void setDateFormatDay(DateFormat dateFormatDay
) {
145 this.dateFormatDay
= dateFormatDay
;
148 public void setDateFormatTime(DateFormat dateFormatTime
) {
149 this.dateFormatTime
= dateFormatTime
;