001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/service/impl/BinaryStorageServiceImplDisk.java,v 1.15 2007/10/09 11:09:21 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.15 $
005: * $Date: 2007/10/09 11:09:21 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Minh Nguyen
039: * @author: Trong Vo
040: */
041: package com.mvnforum.service.impl;
042:
043: import java.io.*;
044:
045: import net.myvietnam.mvncore.exception.DatabaseException;
046: import net.myvietnam.mvncore.exception.ObjectNotFoundException;
047: import net.myvietnam.mvncore.service.BinaryStorageService;
048: import net.myvietnam.mvncore.service.BinaryStorageHandle;
049: import net.myvietnam.mvncore.service.impl.BinaryStorageServiceImplBase;
050: import net.myvietnam.mvncore.util.AssertionUtil;
051: import net.myvietnam.mvncore.util.FileUtil;
052: import net.myvietnam.mvncore.util.ImageUtil;
053:
054: import org.apache.commons.io.IOUtils;
055: import org.apache.commons.logging.Log;
056: import org.apache.commons.logging.LogFactory;
057:
058: import com.mvnforum.MVNForumConfig;
059: import com.mvnforum.MVNForumGlobal;
060: import com.mvnforum.common.AttachmentUtil;
061: import com.mvnforum.db.DAOFactory;
062: import com.mvnforum.db.MemberBean;
063:
064: public class BinaryStorageServiceImplDisk extends
065: BinaryStorageServiceImplBase implements BinaryStorageService {
066:
067: private static Log log = LogFactory
068: .getLog(BinaryStorageServiceImplDisk.class);
069:
070: private static int count;
071:
072: public BinaryStorageServiceImplDisk() {
073: count++;
074: AssertionUtil.doAssert(count == 1,
075: "Assertion: Must have only one instance.");
076: }
077:
078: public BinaryStorageHandle storeData(String category,
079: String nameId, String fileName, InputStream inputStream,
080: int attachDataFileSize, int attachOption, int attachStatus,
081: String contentType, String storageIP) throws IOException {
082:
083: if (inputStream == null) {
084: throw new IllegalArgumentException(
085: "Cannot store an empty inputStream.");
086: }
087:
088: if (category.equals(CATEGORY_POST_ATTACHMENT)) {
089: int attachID = 0;
090: try {
091: attachID = Integer.parseInt(nameId);
092: } catch (NumberFormatException e) {
093: log.error("Cannot parse to an int value " + nameId, e);
094: throw new IllegalArgumentException(
095: "Cannot parse to an int value " + nameId);
096: }
097:
098: String filename = AttachmentUtil
099: .getAttachFilenameOnDisk(attachID);
100: FileOutputStream fos = null;
101: try {
102: log
103: .debug("Attachment filename to save to file system = "
104: + filename);
105:
106: fos = new FileOutputStream(filename);
107: IOUtils.copy(inputStream, fos);// already do the buffering with block = 4096
108:
109: BinaryStorageHandle handle = new BinaryStorageHandle(
110: BinaryStorageService.BINARY_STORAGE_TYPE_DISK,
111: 0, filename);
112: return handle;
113: } catch (IOException e) {
114: log.error("Disk Error", e);
115: throw e;
116: } finally {
117: try {
118: inputStream.close();
119: } catch (IOException e) {
120: log.debug("Cannot close input file", e);
121: }
122: if (fos != null) {
123: try {
124: fos.close();
125: } catch (IOException e) {
126: log.debug("Cannot close output file", e);
127: }
128: }
129: }
130: } else if (category.equals(CATEGORY_PM_ATTACHMENT)) {
131: int attachID = 0;
132: try {
133: attachID = Integer.parseInt(nameId);
134: } catch (NumberFormatException e) {
135: log.error("Cannot parse to an int value " + nameId, e);
136: throw new IllegalArgumentException(
137: "Cannot parse to an int value " + nameId);
138: }
139:
140: String filename = AttachmentUtil
141: .getPmAttachFilenameOnDisk(attachID);
142: FileOutputStream fos = null;
143: try {
144: log
145: .debug("PmAttachment filename to save to file system = "
146: + filename);
147:
148: fos = new FileOutputStream(filename);
149: IOUtils.copy(inputStream, fos);// already do the buffering with block = 4096
150:
151: BinaryStorageHandle handle = new BinaryStorageHandle(
152: BinaryStorageService.BINARY_STORAGE_TYPE_DISK,
153: 0, filename);
154: return handle;
155: } catch (IOException e) {
156: log.error("Disk Error", e);
157: throw e;
158: } finally {
159: try {
160: inputStream.close();
161: } catch (IOException e) {
162: log.debug("Cannot close input file", e);
163: }
164: if (fos != null) {
165: try {
166: fos.close();
167: } catch (IOException e) {
168: log.debug("Cannot close output file", e);
169: }
170: }
171: }
172: } else if (category.equals(CATEGORY_AVATAR)) {
173: int memberID = 0;
174: try {
175: memberID = Integer.parseInt(nameId);
176: } catch (NumberFormatException e) {
177: log.error("Cannot parse to an int value " + nameId, e);
178: throw new IllegalArgumentException(
179: "Cannot parse to an int value " + nameId);
180: }
181:
182: FileOutputStream fos = null;
183: try {
184: MemberBean memberBean = DAOFactory.getMemberDAO()
185: .getMember(memberID);
186: String memberName = memberBean.getMemberName();
187:
188: StringBuffer bufferPicFile = new StringBuffer(128);
189: bufferPicFile.append(MVNForumConfig.getAvatarDir());
190: log.debug("Upload avatar to the folder "
191: + MVNForumConfig.getAvatarDir());
192: bufferPicFile.append(File.separatorChar).append(
193: memberName).append(".jpg");
194: String thumbnailFile = bufferPicFile.toString();
195: log.debug("uploaded file = " + thumbnailFile);
196:
197: // The below method createThumbnail closes the inputStream after it have done its work.
198: ImageUtil.createThumbnail(inputStream, thumbnailFile,
199: 150/*maxWidth*/, 150/*maxHeight*/);// can throw BadInputException
200:
201: // NOTE: actually we should call FileUtil.deleteFile(thumbnailFile);
202: // if the below method failed, however, left it here is also not serious
203: DAOFactory.getMemberDAO().updateAvatar(memberID,
204: MemberBean.MEMBER_AVATAR_USING_UPLOAD);
205:
206: BinaryStorageHandle handle = new BinaryStorageHandle(
207: BinaryStorageService.BINARY_STORAGE_TYPE_DISK,
208: 0, fileName);
209: return handle;
210: } catch (ObjectNotFoundException e) {
211: log.error("ObjectNotFoundException Error", e);
212: throw new IOException(e.getMessage());
213: } catch (DatabaseException e) {
214: log.error("DatabaseException Error", e);
215: throw new IOException(e.getMessage());
216: } catch (AssertionError e) {
217: log.error("AssertionError Error", e);
218: throw new IOException(e.getMessage());
219: } finally {
220: try {
221: inputStream.close();
222: } catch (IOException e) {
223: log.debug("Cannot close input file", e);
224: }
225: if (fos != null) {
226: try {
227: fos.close();
228: } catch (IOException e) {
229: log.debug("Cannot close output file", e);
230: }
231: }
232: }
233: } else {
234: throw new IllegalArgumentException(
235: "Not support category = " + category);
236: }
237: }
238:
239: public InputStream getInputStream(String category, String nameId,
240: BinaryStorageHandle handle) throws IOException {
241:
242: if (category.equals(CATEGORY_POST_ATTACHMENT)) {
243: int attachID = 0;
244: try {
245: attachID = Integer.parseInt(nameId);
246: } catch (NumberFormatException e) {
247: log.error("Cannot parse to an int value " + nameId, e);
248: throw new IllegalArgumentException(
249: "Cannot parse to an int value " + nameId);
250: }
251:
252: String attachFilename = AttachmentUtil
253: .getAttachFilenameOnDisk(attachID);
254: File attachFile = new File(attachFilename);
255:
256: if (attachFile.exists() == false) {
257: throw new IOException(
258: "Can't find the file to be downloaded ("
259: + attachFile + ")");
260: }
261: if (attachFile.isFile() == false) {
262: throw new IOException(
263: "The file to download is a directory.");
264: }
265:
266: InputStream inputStream = new FileInputStream(attachFile);
267: return inputStream;
268: } else if (category.equals(CATEGORY_PM_ATTACHMENT)) {
269: int attachID = 0;
270: try {
271: attachID = Integer.parseInt(nameId);
272: } catch (NumberFormatException e) {
273: log.error("Cannot parse to an int value " + nameId, e);
274: throw new IllegalArgumentException(
275: "Cannot parse to an int value " + nameId);
276: }
277:
278: String pmAttachFilename = AttachmentUtil
279: .getPmAttachFilenameOnDisk(attachID);
280: File attachFile = new File(pmAttachFilename);
281:
282: if (attachFile.exists() == false) {
283: throw new IOException(
284: "Can't find the file to be downloaded ("
285: + attachFile + ")");
286: }
287: if (attachFile.isFile() == false) {
288: throw new IOException(
289: "The file to download is a directory.");
290: }
291:
292: InputStream inputStream = new FileInputStream(attachFile);
293: return inputStream;
294: } else if (category.equals(CATEGORY_AVATAR)) {
295: int memberID = 0;
296: try {
297: memberID = Integer.parseInt(nameId);
298: } catch (NumberFormatException e) {
299: log.error("Cannot parse to an int value " + nameId, e);
300: throw new IllegalArgumentException(
301: "Cannot parse to an int value " + nameId);
302: }
303:
304: try {
305: MemberBean memberBean = DAOFactory.getMemberDAO()
306: .getMember(memberID);
307:
308: String memberAvatar = memberBean.getMemberAvatar();
309: if (memberAvatar
310: .equals(MemberBean.MEMBER_AVATAR_USING_UPLOAD)
311: || memberAvatar
312: .startsWith(BinaryStorageService.BINARY_STORAGE)
313: || memberAvatar
314: .startsWith(MVNForumGlobal.UPLOADED_AVATAR_DIR)) {
315: memberAvatar = memberBean.getMemberName() + ".jpg";
316: } else {
317: throw new IOException(
318: "Assertion: Bad request for memberID = "
319: + memberID);
320: }
321: File avatarFile = new File(MVNForumConfig
322: .getAvatarDir()
323: + File.separator + memberAvatar);
324: if (avatarFile.exists() == false) {
325: throw new IOException(
326: "Can't find the file to be downloaded ("
327: + avatarFile + ")");
328: }
329: if (avatarFile.isFile() == false) {
330: throw new IOException(
331: "The file to download is a directory.");
332: }
333: InputStream inputStream = new FileInputStream(
334: avatarFile);
335: return inputStream;
336: } catch (ObjectNotFoundException e) {
337: log.error("ObjectNotFoundException Error", e);
338: throw new IOException(e.getMessage());
339: } catch (DatabaseException e) {
340: log.error("DatabaseException Error", e);
341: throw new IOException(e.getMessage());
342: }
343: } else {
344: throw new IllegalArgumentException(
345: "Not support category = " + category);
346: }
347: }
348:
349: public void deleteData(String category, String nameId,
350: BinaryStorageHandle handle) throws IOException {
351:
352: if (category.equals(CATEGORY_POST_ATTACHMENT)) {
353: int attachID = 0;
354: try {
355: attachID = Integer.parseInt(nameId);
356: } catch (NumberFormatException e) {
357: log.error("Cannot parse to an int value " + nameId, e);
358: throw new IllegalArgumentException(
359: "Cannot parse to an int value " + nameId);
360: }
361:
362: String filename = AttachmentUtil
363: .getAttachFilenameOnDisk(attachID);
364: try {
365: log.info("About to delete post attachment = "
366: + filename);
367: FileUtil.deleteFile(filename);
368: } catch (Exception ex) {
369: log.warn("Cannot delete post attachment file "
370: + filename, ex);
371: //@todo schedule to delete it later
372: }
373: } else if (category.equals(CATEGORY_PM_ATTACHMENT)) {
374: int attachID = 0;
375: try {
376: attachID = Integer.parseInt(nameId);
377: } catch (NumberFormatException e) {
378: log.error("Cannot parse to an int value " + nameId, e);
379: throw new IllegalArgumentException(
380: "Cannot parse to an int value " + nameId);
381: }
382:
383: String filename = AttachmentUtil
384: .getPmAttachFilenameOnDisk(attachID);
385: try {
386: log.info("About to delete pmAttachment = " + filename);
387: FileUtil.deleteFile(filename);
388: } catch (Exception ex) {
389: log.warn("Cannot delete pmAttachment file " + filename,
390: ex);
391: //@todo schedule to delete it later
392: }
393: } else if (category.equals(CATEGORY_AVATAR)) {
394: int memberID = 0;
395: try {
396: memberID = Integer.parseInt(nameId);
397: } catch (NumberFormatException e) {
398: log.error("Cannot parse to an int value " + nameId, e);
399: throw new IllegalArgumentException(
400: "Cannot parse to an int value " + nameId);
401: }
402:
403: try {
404: MemberBean memberBean = DAOFactory.getMemberDAO()
405: .getMember(memberID);
406: String memberName = memberBean.getMemberName();
407:
408: StringBuffer bufferPicFile = new StringBuffer(128);
409: bufferPicFile.append(MVNForumConfig.getAvatarDir());
410: bufferPicFile.append(File.separatorChar).append(
411: memberName).append(".jpg");
412: String picFile = bufferPicFile.toString();
413:
414: log.debug("Delete avatar = " + picFile);
415: File file = new File(picFile);
416: file.delete();// we dont need to check the returned value
417: } catch (Exception ex) {
418: log.warn("Cannot delete avatar file ", ex);
419: throw new IOException(ex.getMessage());
420: }
421: } else {
422: throw new IllegalArgumentException(
423: "Not support category = " + category);
424: }
425: }
426:
427: }
|