AttachmentUtilities.java

  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. import org.apache.log4j.Logger;
  19. import org.apache.struts.action.ActionMessage;
  20. import org.apache.struts.action.ActionMessages;
  21. import org.apache.struts.upload.FormFile;
  22. import org.itracker.core.resources.ITrackerResources;
  23. import org.itracker.services.ConfigurationService;
  24. import org.itracker.services.ITrackerServices;
  25. import org.itracker.services.IssueService;

  26. public class AttachmentUtilities {

  27.     private static final Logger logger = Logger.getLogger(AttachmentUtilities.class);
  28.     private static boolean initialized = false;
  29.     private static final String CONTENT_TYPE = "multipart/form-data";
  30.     private static final String CHAR_ENCODING = "ISO-8859-1";
  31.     private static final long MAX_FILE_SIZE_KB = 256L;
  32.     private static final long MAX_TOTAL_FILE_SIZE_KB = 1000000L;

  33.     private static long maxFileSize = MAX_FILE_SIZE_KB * 1024L;
  34.     private static long maxTotalFileSize = MAX_TOTAL_FILE_SIZE_KB * 1024L;
  35.     private static long spaceLeft = 0;


  36.     public static ActionMessages validate(FormFile file, ITrackerServices services) {
  37.         ActionMessages msg = new ActionMessages();
  38.         if (!initialized) {
  39.             if (!init(services)) {
  40.                 msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
  41.                 return msg;
  42.             }
  43.         }

  44.         if (file == null) {
  45.             return msg;
  46.         }

  47.         long origFileSize = file.getFileSize();
  48.         if (origFileSize > maxFileSize) {
  49.             logger.info("Cannot save attachment.  File is " + (origFileSize / 1024L) + " kB and max file size is set to " + (maxFileSize / 1024L) + "kB.");

  50.             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")));

  51.             return msg;
  52.         }

  53.         if ((spaceLeft - origFileSize) < 0) {
  54.             logger.info("Cannot save attachment.  Total allocated disk space already used.");
  55.             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")));

  56.         }
  57.         spaceLeft = spaceLeft - origFileSize;

  58.         return msg;
  59.     }


  60.     private static boolean init(ITrackerServices services) {
  61.         if (!initialized) {
  62.             try {
  63.                 ConfigurationService configurationService = services.getConfigurationService();
  64.                 IssueService issueService = services.getIssueService();

  65.                 maxFileSize = configurationService.getLongProperty("max_attachment_size", MAX_FILE_SIZE_KB) * 1024L;
  66.                 maxTotalFileSize = configurationService.getLongProperty("max_total_attachment_size", MAX_TOTAL_FILE_SIZE_KB) * 1024L;
  67.                 spaceLeft = maxTotalFileSize - issueService.getAllIssueAttachmentSize();

  68.                 if (logger.isDebugEnabled()) {
  69.                     logger.debug("Attachment Properties: MaxAttachmentSize set to " + (maxFileSize / 1024L) + " kB");
  70.                     logger.debug("Attachment Properties: MaxTotalAttachmentsSize set to " + (maxTotalFileSize / 1024L) + " kB");
  71.                     logger.debug("Attachment Properties: Current space left is " + (spaceLeft / 1024L) + " kB");
  72.                 }
  73.                 initialized = true;
  74.             } catch (Exception e) {
  75.                 logger.error("Exception initializing AttachmentUtilities.", e);
  76.                 throw new Error("Failed to initialize AttachmentUtilities.", e);
  77.             }
  78.         }
  79.         return initialized;
  80.     }

  81.     public static String getCONTENT_TYPE() {
  82.         return CONTENT_TYPE;
  83.     }

  84.     public static String getCHAR_ENCODING() {
  85.         return CHAR_ENCODING;
  86.     }

  87. }