| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.itracker.web.util; |
| 18 |
|
|
| 19 |
|
import java.io.IOException; |
| 20 |
|
|
| 21 |
|
import org.apache.log4j.Logger; |
| 22 |
|
import org.apache.struts.action.ActionMessage; |
| 23 |
|
import org.apache.struts.action.ActionMessages; |
| 24 |
|
import org.apache.struts.upload.FormFile; |
| 25 |
|
import org.itracker.core.resources.ITrackerResources; |
| 26 |
|
import org.itracker.model.Issue; |
| 27 |
|
import org.itracker.model.IssueAttachment; |
| 28 |
|
import org.itracker.model.Project; |
| 29 |
|
import org.itracker.model.User; |
| 30 |
|
import org.itracker.services.ConfigurationService; |
| 31 |
|
import org.itracker.services.ITrackerServices; |
| 32 |
|
import org.itracker.services.IssueService; |
| 33 |
|
import org.itracker.services.util.ProjectUtilities; |
| 34 |
|
import org.itracker.web.forms.IssueForm; |
| 35 |
|
|
|
|
|
| 0% |
Uncovered Elements: 106 (106) |
Complexity: 25 |
Complexity Density: 0.36 |
|
| 36 |
|
public class AttachmentUtilities { |
| 37 |
|
|
| 38 |
|
private static final Logger logger = Logger.getLogger(AttachmentUtilities.class); |
| 39 |
|
private static boolean initialized = false; |
| 40 |
|
private static final String CONTENT_TYPE = "multipart/form-data"; |
| 41 |
|
private static final String CHAR_ENCODING = "ISO-8859-1"; |
| 42 |
|
private static final long MAX_FILE_SIZE_KB = 256L; |
| 43 |
|
private static final long MAX_TOTAL_FILE_SIZE_KB = 1000000L; |
| 44 |
|
|
| 45 |
|
private static long maxFileSize = MAX_FILE_SIZE_KB * 1024L; |
| 46 |
|
private static long maxTotalFileSize = MAX_TOTAL_FILE_SIZE_KB * 1024L; |
| 47 |
|
private static long spaceLeft = 0; |
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
@deprecated@link |
| 53 |
|
@param |
| 54 |
|
@param |
| 55 |
|
@return |
| 56 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 57 |
0
|
public static boolean checkFile(FormFile file, ITrackerServices services) {... |
| 58 |
|
|
| 59 |
0
|
return validate(file, services).isEmpty(); |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
} |
| 95 |
|
|
| 96 |
|
|
| 97 |
|
@param |
| 98 |
|
@param |
| 99 |
|
@param |
| 100 |
|
@param |
| 101 |
|
@param |
| 102 |
|
@param |
| 103 |
|
@return |
| 104 |
|
@throws |
| 105 |
|
|
|
|
|
| 0% |
Uncovered Elements: 51 (51) |
Complexity: 12 |
Complexity Density: 0.34 |
|
| 106 |
0
|
public static Issue addAttachment(Issue issue, Project project, User user,... |
| 107 |
|
IssueForm form, ITrackerServices services, ActionMessages messages) { |
| 108 |
|
|
| 109 |
0
|
if (ProjectUtilities.hasOption(ProjectUtilities.OPTION_NO_ATTACHMENTS, |
| 110 |
|
project.getOptions())) { |
| 111 |
0
|
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.validate.attachment.disabled", project.getName())); |
| 112 |
0
|
return issue; |
| 113 |
|
} |
| 114 |
|
|
| 115 |
0
|
FormFile file = form.getAttachment(); |
| 116 |
|
|
| 117 |
0
|
if (file == null || file.getFileName().trim().length() < 1) { |
| 118 |
0
|
logger.info("addAttachment: skipping file " + file); |
| 119 |
0
|
return issue; |
| 120 |
|
} |
| 121 |
|
|
| 122 |
0
|
String origFileName = file.getFileName(); |
| 123 |
0
|
String contentType = file.getContentType(); |
| 124 |
0
|
int fileSize = file.getFileSize(); |
| 125 |
|
|
| 126 |
0
|
String attachmentDescription = form.getAttachmentDescription(); |
| 127 |
|
|
| 128 |
0
|
if (null == contentType || 0 >= contentType.length()) { |
| 129 |
0
|
logger.info("addAttachment: got no mime-type, using default plain-text"); |
| 130 |
0
|
contentType = "text/plain"; |
| 131 |
|
} |
| 132 |
|
|
| 133 |
0
|
if (logger.isDebugEnabled()) { |
| 134 |
0
|
logger.debug("addAttachment: adding file, name: " + origFileName |
| 135 |
|
+ " of type " + file.getContentType() + ", description: " |
| 136 |
|
+ form.getAttachmentDescription() + ". filesize: " + fileSize); |
| 137 |
|
} |
| 138 |
0
|
ActionMessages validation = AttachmentUtilities.validate(file, services); |
| 139 |
0
|
if (validation.isEmpty()) { |
| 140 |
|
|
| 141 |
|
|
| 142 |
0
|
int lastSlash = Math.max(origFileName.lastIndexOf('/'), |
| 143 |
|
origFileName.lastIndexOf('\\')); |
| 144 |
0
|
if (lastSlash > -1) { |
| 145 |
0
|
origFileName = origFileName.substring(lastSlash + 1); |
| 146 |
|
} |
| 147 |
|
|
| 148 |
0
|
IssueAttachment attachmentModel = new IssueAttachment(issue, |
| 149 |
|
origFileName, contentType, attachmentDescription, fileSize, |
| 150 |
|
user); |
| 151 |
|
|
| 152 |
0
|
attachmentModel.setIssue(issue); |
| 153 |
|
|
| 154 |
0
|
byte[] fileData; |
| 155 |
0
|
try { |
| 156 |
0
|
fileData = file.getFileData(); |
| 157 |
|
} catch (IOException e) { |
| 158 |
0
|
logger.error("addAttachment: failed to get file data", e); |
| 159 |
0
|
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system")); |
| 160 |
0
|
return issue; |
| 161 |
|
} |
| 162 |
0
|
if(services.getIssueService() |
| 163 |
|
.addIssueAttachment(attachmentModel, fileData)) { |
| 164 |
0
|
return services.getIssueService().getIssue(issue.getId()); |
| 165 |
|
} |
| 166 |
|
|
| 167 |
|
|
| 168 |
|
} else { |
| 169 |
0
|
if (logger.isDebugEnabled()) { |
| 170 |
0
|
logger.debug("addAttachment: failed to validate: " + origFileName + ", " + validation); |
| 171 |
|
} |
| 172 |
0
|
messages.add(validation); |
| 173 |
|
} |
| 174 |
0
|
return issue; |
| 175 |
|
} |
| 176 |
|
|
|
|
|
| 0% |
Uncovered Elements: 27 (27) |
Complexity: 6 |
Complexity Density: 0.35 |
|
| 177 |
0
|
public static ActionMessages validate(FormFile file, ITrackerServices services) {... |
| 178 |
0
|
ActionMessages msg = new ActionMessages(); |
| 179 |
0
|
if(! initialized) { |
| 180 |
0
|
if(! init(services)) { |
| 181 |
0
|
msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system")); |
| 182 |
0
|
return msg; |
| 183 |
|
} |
| 184 |
|
} |
| 185 |
|
|
| 186 |
0
|
if(file == null) { |
| 187 |
0
|
return msg; |
| 188 |
|
} |
| 189 |
|
|
| 190 |
0
|
long origFileSize = file.getFileSize(); |
| 191 |
0
|
if(origFileSize > maxFileSize) { |
| 192 |
0
|
logger.info("Cannot save attachment. File is " + (origFileSize / 1024L) + " kB and max file size is set to " + (maxFileSize / 1024L) + "kB."); |
| 193 |
|
|
| 194 |
0
|
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"))); |
| 195 |
|
|
| 196 |
0
|
return msg; |
| 197 |
|
} |
| 198 |
|
|
| 199 |
0
|
if((spaceLeft - origFileSize) < 0) { |
| 200 |
0
|
logger.info("Cannot save attachment. Total allocated disk space already used."); |
| 201 |
0
|
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"))); |
| 202 |
|
|
| 203 |
|
} |
| 204 |
0
|
spaceLeft = spaceLeft - origFileSize; |
| 205 |
|
|
| 206 |
0
|
return msg; |
| 207 |
|
} |
| 208 |
|
|
| 209 |
|
|
| 210 |
|
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 4 |
Complexity Density: 0.27 |
|
| 211 |
0
|
private static boolean init(ITrackerServices services) {... |
| 212 |
0
|
if(! initialized) { |
| 213 |
0
|
try { |
| 214 |
0
|
ConfigurationService configurationService = services.getConfigurationService(); |
| 215 |
0
|
IssueService issueService = services.getIssueService(); |
| 216 |
|
|
| 217 |
0
|
maxFileSize = configurationService.getLongProperty("max_attachment_size", MAX_FILE_SIZE_KB) * 1024L; |
| 218 |
0
|
maxTotalFileSize = configurationService.getLongProperty("max_total_attachment_size", MAX_TOTAL_FILE_SIZE_KB) * 1024L; |
| 219 |
0
|
spaceLeft = maxTotalFileSize - issueService.getAllIssueAttachmentSize(); |
| 220 |
|
|
| 221 |
0
|
if(logger.isDebugEnabled()) { |
| 222 |
0
|
logger.debug("Attachment Properties: MaxAttachmentSize set to " + (maxFileSize / 1024L) + " kB"); |
| 223 |
0
|
logger.debug("Attachment Properties: MaxTotalAttachmentsSize set to " + (maxTotalFileSize / 1024L) + " kB"); |
| 224 |
0
|
logger.debug("Attachment Properties: Current space left is " + (spaceLeft / 1024L) + " kB"); |
| 225 |
|
} |
| 226 |
0
|
initialized = true; |
| 227 |
|
} catch(Exception e) { |
| 228 |
0
|
logger.error("Exception initializing AttachmentUtilities.", e); |
| 229 |
0
|
throw new Error("Failed to initialize AttachmentUtilities.", e); |
| 230 |
|
} |
| 231 |
|
} |
| 232 |
0
|
return initialized; |
| 233 |
|
} |
| 234 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 235 |
0
|
public static String getCONTENT_TYPE() {... |
| 236 |
0
|
return CONTENT_TYPE; |
| 237 |
|
} |
| 238 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 239 |
0
|
public static String getCHAR_ENCODING() {... |
| 240 |
0
|
return CHAR_ENCODING; |
| 241 |
|
} |
| 242 |
|
|
| 243 |
|
} |