2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.slc
.core
.attachment
;
20 import java
.io
.FileInputStream
;
21 import java
.io
.FileOutputStream
;
22 import java
.io
.FileWriter
;
23 import java
.io
.IOException
;
24 import java
.io
.InputStream
;
25 import java
.io
.OutputStream
;
26 import java
.text
.DateFormat
;
27 import java
.text
.SimpleDateFormat
;
28 import java
.util
.Date
;
30 import org
.apache
.commons
.io
.IOUtils
;
31 import org
.apache
.commons
.logging
.Log
;
32 import org
.apache
.commons
.logging
.LogFactory
;
33 import org
.argeo
.slc
.SlcException
;
34 import org
.springframework
.beans
.factory
.InitializingBean
;
35 import org
.springframework
.core
.io
.Resource
;
37 public class FileAttachmentsStorage
implements AttachmentsStorage
,
38 AttachmentUploader
, InitializingBean
{
39 private final static Log log
= LogFactory
40 .getLog(FileAttachmentsStorage
.class);
42 private File attachmentsDirectory
;
44 private String attachmentsTocFileName
= "attachmentsToc.csv";
46 private DateFormat dateFormatDay
= new SimpleDateFormat("yyyy-MM-dd");
47 private DateFormat dateFormatTime
= new SimpleDateFormat("HH:mm:ss");
49 public void afterPropertiesSet() {
50 if (attachmentsDirectory
== null) {
52 String osgiInstanceArea
= System
.getProperty("osgi.instance.area");
53 if (osgiInstanceArea
!= null) {
54 if (osgiInstanceArea
.startsWith("file:"))
55 osgiInstanceArea
= osgiInstanceArea
.substring("file:"
57 attachmentsDirectory
= new File(osgiInstanceArea
58 + File
.separator
+ "slcAttachments");
61 if (attachmentsDirectory
== null) {
62 String tempDir
= System
.getProperty("java.io.tmpdir");
63 attachmentsDirectory
= new File(tempDir
+ File
.separator
67 if (!attachmentsDirectory
.exists())
68 attachmentsDirectory
.mkdirs();
69 if (log
.isDebugEnabled())
70 log
.debug("File attachment storage initialized in directory "
71 + attachmentsDirectory
);
74 public void retrieveAttachment(Attachment attachment
,
75 OutputStream outputStream
) {
76 File file
= getFile(attachment
);
77 InputStream in
= null;
79 byte[] buffer
= new byte[1024 * 1024];
80 in
= new FileInputStream(file
);
82 while ((read
= in
.read(buffer
)) >= 0) {
83 outputStream
.write(buffer
, 0, read
);
85 if (log
.isTraceEnabled())
86 log
.trace("Read " + attachment
+ " from " + file
);
87 } catch (IOException e
) {
88 throw new SlcException("Cannot write attachment " + attachment
91 IOUtils
.closeQuietly(in
);
95 public void storeAttachment(Attachment attachment
, InputStream inputStream
) {
96 File file
= getFile(attachment
);
97 FileOutputStream out
= null;
99 byte[] buffer
= new byte[1024 * 1024];
100 out
= new FileOutputStream(file
);
102 while ((read
= inputStream
.read(buffer
)) >= 0) {
103 out
.write(buffer
, 0, read
);
105 if (log
.isTraceEnabled())
106 log
.trace("Wrote " + attachment
+ " to " + file
);
107 updateAttachmentToc(attachment
, file
);
108 } catch (IOException e
) {
109 throw new SlcException("Cannot write attachment " + attachment
112 IOUtils
.closeQuietly(out
);
117 public void upload(Attachment attachment
, Resource resource
) {
119 storeAttachment(attachment
, resource
.getInputStream());
120 } catch (IOException e
) {
121 throw new SlcException("Cannot upload attachment " + attachment
, e
);
125 /** For monitoring purposes only */
126 protected void updateAttachmentToc(Attachment attachment
, File file
) {
127 Date date
= new Date(file
.lastModified());
128 FileWriter writer
= null;
130 writer
= new FileWriter(attachmentsDirectory
+ File
.separator
131 + attachmentsTocFileName
, true);
132 writer
.append(dateFormatDay
.format(date
));
134 writer
.append(dateFormatTime
.format(date
));
136 writer
.append(attachment
.getUuid());
138 writer
.append(attachment
.getName());
140 writer
.append(attachment
.getContentType());
142 writer
.append(Long
.toString(file
.length()));
144 writer
.append(file
.getCanonicalPath());
146 } catch (IOException e
) {
147 log
.warn("Could not update attachments TOC for " + attachment
148 + " and file " + file
, e
);
150 IOUtils
.closeQuietly(writer
);
155 protected File
getFile(Attachment attachment
) {
156 File file
= new File(attachmentsDirectory
+ File
.separator
157 + attachment
.getUuid());
161 public void setAttachmentsDirectory(File attachmentsDirectory
) {
162 this.attachmentsDirectory
= attachmentsDirectory
;
165 public void setAttachmentsTocFileName(String attachmentsTocFileName
) {
166 this.attachmentsTocFileName
= attachmentsTocFileName
;
169 public void setDateFormatDay(DateFormat dateFormatDay
) {
170 this.dateFormatDay
= dateFormatDay
;
173 public void setDateFormatTime(DateFormat dateFormatTime
) {
174 this.dateFormatTime
= dateFormatTime
;