View Javadoc
1   /* This software was designed and created by Jason Carroll.
2    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
3    * The author can be reached at jcarroll@cowsultants.com
4    * ITracker website: http://www.cowsultants.com
5    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it only under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   */
17  package org.itracker.web.util;
18  
19  import org.apache.log4j.Logger;
20  import org.apache.struts.action.ActionMessage;
21  import org.apache.struts.action.ActionMessages;
22  import org.apache.struts.upload.FormFile;
23  import org.itracker.core.resources.ITrackerResources;
24  import org.itracker.services.ConfigurationService;
25  import org.itracker.services.ITrackerServices;
26  import org.itracker.services.IssueService;
27  
28  public class AttachmentUtilities {
29  
30      private static final Logger logger = Logger.getLogger(AttachmentUtilities.class);
31      private static boolean initialized = false;
32      private static final String CONTENT_TYPE = "multipart/form-data";
33      private static final String CHAR_ENCODING = "ISO-8859-1";
34      private static final long MAX_FILE_SIZE_KB = 256L;
35      private static final long MAX_TOTAL_FILE_SIZE_KB = 1000000L;
36  
37      private static long maxFileSize = MAX_FILE_SIZE_KB * 1024L;
38      private static long maxTotalFileSize = MAX_TOTAL_FILE_SIZE_KB * 1024L;
39      private static long spaceLeft = 0;
40  
41  
42      public static ActionMessages validate(FormFile file, ITrackerServices services) {
43          ActionMessages msg = new ActionMessages();
44          if (!initialized) {
45              if (!init(services)) {
46                  msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
47                  return msg;
48              }
49          }
50  
51          if (file == null) {
52              return msg;
53          }
54  
55          long origFileSize = file.getFileSize();
56          if (origFileSize > maxFileSize) {
57              logger.info("Cannot save attachment.  File is " + (origFileSize / 1024L) + " kB and max file size is set to " + (maxFileSize / 1024L) + "kB.");
58  
59              msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.validate.attachment.size", (Math.round((maxFileSize / 1024L) * 100) / 100) + " " + ITrackerResources.getString("itracker.web.generic.kilobyte")));
60  
61              return msg;
62          }
63  
64          if ((spaceLeft - origFileSize) < 0) {
65              logger.info("Cannot save attachment.  Total allocated disk space already used.");
66              msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.validate.attachment.quota", (Math.round((maxTotalFileSize / 1024L) * 100) / 100) + " " + ITrackerResources.getString("itracker.web.generic.kilobyte")));
67  
68          }
69          spaceLeft = spaceLeft - origFileSize;
70  
71          return msg;
72      }
73  
74  
75      private static boolean init(ITrackerServices services) {
76          if (!initialized) {
77              try {
78                  ConfigurationService configurationService = services.getConfigurationService();
79                  IssueService issueService = services.getIssueService();
80  
81                  maxFileSize = configurationService.getLongProperty("max_attachment_size", MAX_FILE_SIZE_KB) * 1024L;
82                  maxTotalFileSize = configurationService.getLongProperty("max_total_attachment_size", MAX_TOTAL_FILE_SIZE_KB) * 1024L;
83                  spaceLeft = maxTotalFileSize - issueService.getAllIssueAttachmentSize();
84  
85                  if (logger.isDebugEnabled()) {
86                      logger.debug("Attachment Properties: MaxAttachmentSize set to " + (maxFileSize / 1024L) + " kB");
87                      logger.debug("Attachment Properties: MaxTotalAttachmentsSize set to " + (maxTotalFileSize / 1024L) + " kB");
88                      logger.debug("Attachment Properties: Current space left is " + (spaceLeft / 1024L) + " kB");
89                  }
90                  initialized = true;
91              } catch (Exception e) {
92                  logger.error("Exception initializing AttachmentUtilities.", e);
93                  throw new Error("Failed to initialize AttachmentUtilities.", e);
94              }
95          }
96          return initialized;
97      }
98  
99      public static String getCONTENT_TYPE() {
100         return CONTENT_TYPE;
101     }
102 
103     public static String getCHAR_ENCODING() {
104         return CHAR_ENCODING;
105     }
106 
107 }