KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > impl > BinaryStorageImplDisk


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/impl/BinaryStorageImplDisk.java,v 1.14 2006/04/14 16:29:57 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.14 $
5  * $Date: 2006/04/14 16:29:57 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Minh Nguyen
39  * @author: Trong Vo
40  */

41 package com.mvnforum.impl;
42
43 import java.io.*;
44
45 import net.myvietnam.mvncore.exception.AssertionException;
46 import net.myvietnam.mvncore.exception.DatabaseException;
47 import net.myvietnam.mvncore.exception.ObjectNotFoundException;
48 import net.myvietnam.mvncore.service.BinaryStorage;
49 import net.myvietnam.mvncore.service.BinaryStorageHandle;
50 import net.myvietnam.mvncore.util.FileUtil;
51 import net.myvietnam.mvncore.util.ImageUtil;
52
53 import org.apache.commons.io.IOUtils;
54 import org.apache.commons.logging.Log;
55 import org.apache.commons.logging.LogFactory;
56
57 import com.mvnforum.MVNForumConfig;
58 import com.mvnforum.MVNForumGlobal;
59 import com.mvnforum.common.AttachmentUtil;
60 import com.mvnforum.db.DAOFactory;
61 import com.mvnforum.db.MemberBean;
62
63 public class BinaryStorageImplDisk extends BinaryStorageBase implements BinaryStorage {
64
65     private static Log log = LogFactory.getLog(BinaryStorageImplDisk.class);
66
67     public BinaryStorageHandle storeData(String JavaDoc category, String JavaDoc nameId, String JavaDoc fileName, InputStream inputStream,
68                                          int attachDataFileSize, int attachOption, int attachStatus, String JavaDoc contentType, String JavaDoc storageIP)
69         throws IOException {
70
71         if (inputStream == null) {
72             throw new IllegalArgumentException JavaDoc("Cannot store an empty inputStream.");
73         }
74
75         if (category.equals(CATEGORY_POST_ATTACHMENT)) {
76             int attachID = 0;
77             try {
78                 attachID = Integer.parseInt(nameId);
79             } catch (NumberFormatException JavaDoc e) {
80                 log.error("Cannot parse to an int value " + nameId, e);
81                 throw new IllegalArgumentException JavaDoc("Cannot parse to an int value " + nameId);
82             }
83
84             String JavaDoc filename = AttachmentUtil.getAttachFilenameOnDisk(attachID);
85             FileOutputStream fos = null;
86             try {
87                 log.debug("Attach filename to save to file system = " + filename);
88
89                 fos = new FileOutputStream(filename);
90                 IOUtils.copy(inputStream, fos);// already do the buffering with block = 4096
91

92                 BinaryStorageHandle handle = new BinaryStorageHandle(BinaryStorage.BINARY_STORAGE_TYPE_DISK, 0, filename);
93                 return handle;
94             } catch (IOException e) {
95                 log.error("Disk Error", e);
96                 throw e;
97             } finally {
98                 try {
99                     inputStream.close();
100                 } catch (IOException e) {
101                     log.debug("Cannot close input file", e);
102                 }
103                 if (fos != null) {
104                     try {
105                         fos.close();
106                     } catch (IOException e) {
107                         log.debug("Cannot close output file", e);
108                     }
109                 }
110             }
111         } else if (category.equals(CATEGORY_PM_ATTACHMENT)) {
112             int attachID = 0;
113             try {
114                 attachID = Integer.parseInt(nameId);
115             } catch (NumberFormatException JavaDoc e) {
116                 log.error("Cannot parse to an int value " + nameId, e);
117                 throw new IllegalArgumentException JavaDoc("Cannot parse to an int value " + nameId);
118             }
119
120             String JavaDoc filename = AttachmentUtil.getPmAttachFilenameOnDisk(attachID);
121             FileOutputStream fos = null;
122             try {
123                 log.debug("PmAttach filename to save to file system = " + filename);
124
125                 fos = new FileOutputStream(filename);
126                 IOUtils.copy(inputStream, fos);// already do the buffering with block = 4096
127

128                 BinaryStorageHandle handle = new BinaryStorageHandle(BinaryStorage.BINARY_STORAGE_TYPE_DISK, 0, filename);
129                 return handle;
130             } catch (IOException e) {
131                 log.error("Disk Error", e);
132                 throw e;
133             } finally {
134                 try {
135                     inputStream.close();
136                 } catch (IOException e) {
137                     log.debug("Cannot close input file", e);
138                 }
139                 if (fos != null) {
140                     try {
141                         fos.close();
142                     } catch (IOException e) {
143                         log.debug("Cannot close output file", e);
144                     }
145                 }
146             }
147         } else if (category.equals(CATEGORY_AVATAR)) {
148             int memberID = 0;
149             try {
150                 memberID = Integer.parseInt(nameId);
151             } catch (NumberFormatException JavaDoc e) {
152                 log.error("Cannot parse to an int value " + nameId, e);
153                 throw new IllegalArgumentException JavaDoc("Cannot parse to an int value " + nameId);
154             }
155
156             FileOutputStream fos = null;
157             try {
158                 MemberBean memberBean = (MemberBean) DAOFactory.getMemberDAO().getMember_forPublic(memberID);
159                 String JavaDoc memberName = memberBean.getMemberName();
160
161                 StringBuffer JavaDoc bufferPicFile = new StringBuffer JavaDoc(128);
162                 bufferPicFile.append(MVNForumConfig.getAvatarDir());
163                 log.debug("Upload avatar to the folder " + MVNForumConfig.getAvatarDir());
164                 bufferPicFile.append(File.separatorChar).append(memberName).append(".jpg");
165                 String JavaDoc thumbnailFile = bufferPicFile.toString();
166                 log.debug("uploaded file = " + thumbnailFile);
167
168 // The below method createThumbnail closes the inputStream after it have done its work.
169
ImageUtil.createThumbnail(inputStream, thumbnailFile, 150/*maxWidth*/, 150/*maxHeight*/);// can throw BadInputException
170

171                 // NOTE: actually we should call FileUtil.deleteFile(thumbnailFile);
172
// if the below method failed, however, left it here is also not serious
173
DAOFactory.getMemberDAO().updateAvatar(memberID, MemberBean.MEMBER_AVATAR_USING_UPLOAD);
174
175                 BinaryStorageHandle handle = new BinaryStorageHandle(BinaryStorage.BINARY_STORAGE_TYPE_DISK, 0, fileName);
176                 return handle;
177             } catch (ObjectNotFoundException e) {
178                 log.error("ObjectNotFoundException Error", e);
179                 throw new IOException(e.getMessage());
180             } catch (DatabaseException e) {
181                 log.error("DatabaseException Error", e);
182                 throw new IOException(e.getMessage());
183             } catch (AssertionException e) {
184                 log.error("AssertionException Error", e);
185                 throw new IOException(e.getMessage());
186             } finally {
187                 try {
188                     inputStream.close();
189                 } catch (IOException e) {
190                     log.debug("Cannot close input file", e);
191                 }
192                 if (fos != null) {
193                     try {
194                         fos.close();
195                     } catch (IOException e) {
196                         log.debug("Cannot close output file", e);
197                     }
198                 }
199             }
200         } else {
201             throw new IllegalArgumentException JavaDoc("Not support category = " + category);
202         }
203     }
204
205     public InputStream getInputStream(String JavaDoc category, String JavaDoc nameId, BinaryStorageHandle handle)
206         throws IOException {
207
208         if (category.equals(CATEGORY_POST_ATTACHMENT)) {
209             int attachID = 0;
210             try {
211                 attachID = Integer.parseInt(nameId);
212             } catch (NumberFormatException JavaDoc e) {
213                 log.error("Cannot parse to an int value " + nameId, e);
214                 throw new IllegalArgumentException JavaDoc("Cannot parse to an int value " + nameId);
215             }
216
217             String JavaDoc attachFilename = AttachmentUtil.getAttachFilenameOnDisk(attachID);
218             File attachFile = new File(attachFilename);
219
220             if (attachFile.exists() == false) {
221                 throw new IOException("Can't find the file to be downloaded (" + attachFile + ")");
222             }
223             if (attachFile.isFile() == false) {
224                 throw new IOException("The file to download is a directory.");
225             }
226
227             InputStream inputStream = new FileInputStream(attachFile);
228             return inputStream;
229         } else if (category.equals(CATEGORY_PM_ATTACHMENT)) {
230             int attachID = 0;
231             try {
232                 attachID = Integer.parseInt(nameId);
233             } catch (NumberFormatException JavaDoc e) {
234                 log.error("Cannot parse to an int value " + nameId, e);
235                 throw new IllegalArgumentException JavaDoc("Cannot parse to an int value " + nameId);
236             }
237
238             String JavaDoc pmAttachFilename = AttachmentUtil.getPmAttachFilenameOnDisk(attachID);
239             File attachFile = new File(pmAttachFilename);
240
241             if (attachFile.exists() == false) {
242                 throw new IOException("Can't find the file to be downloaded (" + attachFile + ")");
243             }
244             if (attachFile.isFile() == false) {
245                 throw new IOException("The file to download is a directory.");
246             }
247
248             InputStream inputStream = new FileInputStream(attachFile);
249             return inputStream;
250         } else if (category.equals(CATEGORY_AVATAR)) {
251             int memberID = 0;
252             try {
253                 memberID = Integer.parseInt(nameId);
254             } catch (NumberFormatException JavaDoc e) {
255                 log.error("Cannot parse to an int value " + nameId, e);
256                 throw new IllegalArgumentException JavaDoc("Cannot parse to an int value " + nameId);
257             }
258
259             try {
260                 MemberBean memberBean = (MemberBean) DAOFactory.getMemberDAO().getMember_forPublic(memberID);
261
262                 String JavaDoc memberAvatar = memberBean.getMemberAvatar();
263                 if (memberAvatar.equals(MemberBean.MEMBER_AVATAR_USING_UPLOAD) ||
264                     memberAvatar.startsWith(BinaryStorage.BINARY_STORAGE)||
265                     memberAvatar.startsWith(MVNForumGlobal.UPLOADED_AVATAR_DIR)) {
266                     memberAvatar = memberBean.getMemberName() + ".jpg";
267                 } else {
268                     throw new IOException("Assertion: Bad request for memberID = " + memberID);
269                 }
270                 File avatarFile = new File(MVNForumConfig.getAvatarDir() + File.separator + memberAvatar);
271                 if (avatarFile.exists() == false) {
272                     throw new IOException("Can't find the file to be downloaded (" + avatarFile + ")");
273                 }
274                 if (avatarFile.isFile() == false) {
275                     throw new IOException("The file to download is a directory.");
276                 }
277                 InputStream inputStream = new FileInputStream(avatarFile);
278                 return inputStream;
279             } catch (ObjectNotFoundException e) {
280                 log.error("ObjectNotFoundException Error", e);
281                 throw new IOException(e.getMessage());
282             } catch (DatabaseException e) {
283                 log.error("DatabaseException Error", e);
284                 throw new IOException(e.getMessage());
285             }
286         } else {
287             throw new IllegalArgumentException JavaDoc("Not support category = " + category);
288         }
289     }
290
291
292     public void deleteData(String JavaDoc category, String JavaDoc nameId, BinaryStorageHandle handle)
293         throws IOException {
294
295         if (category.equals(CATEGORY_POST_ATTACHMENT)) {
296             int attachID = 0;
297             try {
298                 attachID = Integer.parseInt(nameId);
299             } catch (NumberFormatException JavaDoc e) {
300                 log.error("Cannot parse to an int value " + nameId, e);
301                 throw new IllegalArgumentException JavaDoc("Cannot parse to an int value " + nameId);
302             }
303
304             String JavaDoc filename = AttachmentUtil.getAttachFilenameOnDisk(attachID);
305             try {
306                 log.info("About to delete post attachment = " + filename);
307                 FileUtil.deleteFile(filename);
308             } catch (Exception JavaDoc ex) {
309                 log.warn("Cannot delete post attachment file " + filename, ex);
310                 //@todo schedule to delete it later
311
}
312         } else if (category.equals(CATEGORY_PM_ATTACHMENT)) {
313             int attachID = 0;
314             try {
315                 attachID = Integer.parseInt(nameId);
316             } catch (NumberFormatException JavaDoc e) {
317                 log.error("Cannot parse to an int value " + nameId, e);
318                 throw new IllegalArgumentException JavaDoc("Cannot parse to an int value " + nameId);
319             }
320
321             String JavaDoc filename = AttachmentUtil.getPmAttachFilenameOnDisk(attachID);
322             try {
323                 log.info("About to delete pmAttachment = " + filename);
324                 FileUtil.deleteFile(filename);
325             } catch (Exception JavaDoc ex) {
326                 log.warn("Cannot delete pmAttachment file " + filename, ex);
327                 //@todo schedule to delete it later
328
}
329         } else if (category.equals(CATEGORY_AVATAR)) {
330             int memberID = 0;
331             try {
332                 memberID = Integer.parseInt(nameId);
333             } catch (NumberFormatException JavaDoc e) {
334                 log.error("Cannot parse to an int value " + nameId, e);
335                 throw new IllegalArgumentException JavaDoc("Cannot parse to an int value " + nameId);
336             }
337
338             try {
339                 MemberBean memberBean = DAOFactory.getMemberDAO().getMember_forPublic(memberID);
340                 String JavaDoc memberName = memberBean.getMemberName();
341
342                 StringBuffer JavaDoc bufferPicFile = new StringBuffer JavaDoc(128);
343                 bufferPicFile.append(MVNForumConfig.getAvatarDir());
344                 bufferPicFile.append(File.separatorChar).append(memberName).append(".jpg");
345                 String JavaDoc picFile = bufferPicFile.toString();
346
347                 log.debug("Delete avatar = " + picFile);
348                 File file = new File(picFile);
349                 file.delete();// we dont need to check the returned value
350
} catch (Exception JavaDoc ex) {
351                 log.warn("Cannot delete avatar file ", ex);
352                 throw new IOException(ex.getMessage());
353             }
354         } else {
355             throw new IllegalArgumentException JavaDoc("Not support category = " + category);
356         }
357     }
358
359 }
360
Popular Tags