1 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 category, String nameId, String fileName, InputStream inputStream, 68 int attachDataFileSize, int attachOption, int attachStatus, String contentType, String storageIP) 69 throws IOException { 70 71 if (inputStream == null) { 72 throw new IllegalArgumentException ("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 e) { 80 log.error("Cannot parse to an int value " + nameId, e); 81 throw new IllegalArgumentException ("Cannot parse to an int value " + nameId); 82 } 83 84 String 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); 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 e) { 116 log.error("Cannot parse to an int value " + nameId, e); 117 throw new IllegalArgumentException ("Cannot parse to an int value " + nameId); 118 } 119 120 String 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); 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 e) { 152 log.error("Cannot parse to an int value " + nameId, e); 153 throw new IllegalArgumentException ("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 memberName = memberBean.getMemberName(); 160 161 StringBuffer bufferPicFile = new StringBuffer (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 thumbnailFile = bufferPicFile.toString(); 166 log.debug("uploaded file = " + thumbnailFile); 167 168 ImageUtil.createThumbnail(inputStream, thumbnailFile, 150, 150); 171 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 ("Not support category = " + category); 202 } 203 } 204 205 public InputStream getInputStream(String category, String 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 e) { 213 log.error("Cannot parse to an int value " + nameId, e); 214 throw new IllegalArgumentException ("Cannot parse to an int value " + nameId); 215 } 216 217 String 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 e) { 234 log.error("Cannot parse to an int value " + nameId, e); 235 throw new IllegalArgumentException ("Cannot parse to an int value " + nameId); 236 } 237 238 String 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 e) { 255 log.error("Cannot parse to an int value " + nameId, e); 256 throw new IllegalArgumentException ("Cannot parse to an int value " + nameId); 257 } 258 259 try { 260 MemberBean memberBean = (MemberBean) DAOFactory.getMemberDAO().getMember_forPublic(memberID); 261 262 String 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 ("Not support category = " + category); 288 } 289 } 290 291 292 public void deleteData(String category, String 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 e) { 300 log.error("Cannot parse to an int value " + nameId, e); 301 throw new IllegalArgumentException ("Cannot parse to an int value " + nameId); 302 } 303 304 String filename = AttachmentUtil.getAttachFilenameOnDisk(attachID); 305 try { 306 log.info("About to delete post attachment = " + filename); 307 FileUtil.deleteFile(filename); 308 } catch (Exception ex) { 309 log.warn("Cannot delete post attachment file " + filename, ex); 310 } 312 } else if (category.equals(CATEGORY_PM_ATTACHMENT)) { 313 int attachID = 0; 314 try { 315 attachID = Integer.parseInt(nameId); 316 } catch (NumberFormatException e) { 317 log.error("Cannot parse to an int value " + nameId, e); 318 throw new IllegalArgumentException ("Cannot parse to an int value " + nameId); 319 } 320 321 String filename = AttachmentUtil.getPmAttachFilenameOnDisk(attachID); 322 try { 323 log.info("About to delete pmAttachment = " + filename); 324 FileUtil.deleteFile(filename); 325 } catch (Exception ex) { 326 log.warn("Cannot delete pmAttachment file " + filename, ex); 327 } 329 } else if (category.equals(CATEGORY_AVATAR)) { 330 int memberID = 0; 331 try { 332 memberID = Integer.parseInt(nameId); 333 } catch (NumberFormatException e) { 334 log.error("Cannot parse to an int value " + nameId, e); 335 throw new IllegalArgumentException ("Cannot parse to an int value " + nameId); 336 } 337 338 try { 339 MemberBean memberBean = DAOFactory.getMemberDAO().getMember_forPublic(memberID); 340 String memberName = memberBean.getMemberName(); 341 342 StringBuffer bufferPicFile = new StringBuffer (128); 343 bufferPicFile.append(MVNForumConfig.getAvatarDir()); 344 bufferPicFile.append(File.separatorChar).append(memberName).append(".jpg"); 345 String picFile = bufferPicFile.toString(); 346 347 log.debug("Delete avatar = " + picFile); 348 File file = new File(picFile); 349 file.delete(); } catch (Exception ex) { 351 log.warn("Cannot delete avatar file ", ex); 352 throw new IOException(ex.getMessage()); 353 } 354 } else { 355 throw new IllegalArgumentException ("Not support category = " + category); 356 } 357 } 358 359 } 360 | Popular Tags |