1 18 19 package cowsultants.itracker.web.util; 20 21 import java.io.*; 22 import java.net.*; 23 import java.rmi.*; 24 import java.util.*; 25 import javax.naming.*; 26 import javax.rmi.*; 27 import javax.ejb.*; 28 29 import org.apache.struts.upload.*; 30 31 import cowsultants.itracker.ejb.client.interfaces.*; 32 import cowsultants.itracker.ejb.client.util.*; 33 34 public class AttachmentUtilities { 35 private static boolean initialized = false; 36 private static final String CONTENT_TYPE = "multipart/form-data"; 37 private static final String CHAR_ENCODING = "ISO-8859-1"; 38 private static final long MAX_FILE_SIZE_KB = 256L; 39 private static final long MAX_TOTAL_FILE_SIZE_KB = 1000000L; 40 41 private static long maxFileSize = MAX_FILE_SIZE_KB * 1024L; 42 private static long maxTotalFileSize = MAX_TOTAL_FILE_SIZE_KB * 1024L; 43 private static long spaceLeft = 0; 44 45 public static boolean checkFile(FormFile file) { 46 if(! initialized) { 47 if(! init()) { 48 return false; 49 } 50 } 51 52 if(file == null) { 53 return false; 54 } 55 56 long origFileSize = file.getFileSize(); 57 if(origFileSize > maxFileSize) { 58 Logger.logInfo("Cannot save attachment. File is " + (origFileSize / 1024L) + " kB and max file size is set to " + (maxFileSize / 1024L) + "kB."); 59 return false; 60 } 61 62 if((spaceLeft - origFileSize) < 0) { 63 Logger.logInfo("Cannot save attachment. Total allocated disk space already used."); 64 return false; 65 } 66 spaceLeft = spaceLeft - origFileSize; 67 68 70 74 return true; 78 } 79 80 private static boolean init() { 81 if(! initialized) { 82 try { 83 InitialContext ic = new InitialContext(); 84 Object scRef = ic.lookup("java:comp/env/" + SystemConfiguration.JNDI_NAME); 85 SystemConfigurationHome scHome = (SystemConfigurationHome) PortableRemoteObject.narrow(scRef, SystemConfigurationHome.class); 86 SystemConfiguration sc = scHome.create(); 87 88 Object ihRef = ic.lookup("java:comp/env/" + IssueHandler.JNDI_NAME); 89 IssueHandlerHome ihHome = (IssueHandlerHome) PortableRemoteObject.narrow(ihRef, IssueHandlerHome.class); 90 IssueHandler ih = ihHome.create(); 91 92 maxFileSize = sc.getLongProperty("max_attachment_size", MAX_FILE_SIZE_KB) * 1024L; 93 maxTotalFileSize = sc.getLongProperty("max_total_attachment_size", MAX_TOTAL_FILE_SIZE_KB) * 1024L; 94 spaceLeft = maxTotalFileSize - ih.getAllIssueAttachmentsSizeAndCount()[0]; 95 96 if(Logger.isLoggingDebug()) { 97 Logger.logDebug("Attachment Properties: MaxAttachmentSize set to " + (maxFileSize / 1024L) + " kB"); 98 Logger.logDebug("Attachment Properties: MaxTotalAttachmentsSize set to " + (maxTotalFileSize / 1024L) + " kB"); 99 Logger.logDebug("Attachment Properties: Current space left is " + (spaceLeft / 1024L) + " kB"); 100 } 101 initialized = true; 102 } catch(Exception e) { 103 Logger.logError("Exception initializing AttachmentUtilities.", e); 104 } 105 } 106 return initialized; 107 } 108 109 } 110 | Popular Tags |