1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.web.actions.project;
20
21 import java.io.File;
22 import java.io.IOException;
23
24 import javax.servlet.ServletException;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.apache.log4j.Logger;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32 import org.apache.struts.action.ActionMessage;
33 import org.apache.struts.action.ActionMessages;
34 import org.itracker.model.IssueAttachment;
35 import org.itracker.services.ConfigurationService;
36 import org.itracker.services.IssueService;
37 import org.itracker.services.util.UserUtilities;
38 import org.itracker.web.actions.base.ItrackerBaseAction;
39
40
41 public class RemoveIssueAttachmentAction extends ItrackerBaseAction {
42 private static final Logger log = Logger.getLogger(RemoveIssueAttachmentAction.class);
43
44
45
46 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
47
48 ActionMessages errors = new ActionMessages();
49
50 if(! hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
51 return mapping.findForward("unauthorized");
52 }
53
54 try {
55 IssueService issueService = getITrackerServices().getIssueService();
56
57 try {
58 Integer attachmentId = new Integer((request.getParameter("id") == null ? "-1" : request.getParameter("id")));
59 IssueAttachment attachment = issueService.getIssueAttachment(attachmentId);
60
61 if(attachment != null) {
62 ConfigurationService configurationService = getITrackerServices().getConfigurationService();
63
64 File attachmentFile = new File( configurationService.getProperty("attachment_dir")
65 + File.separator + attachment.getFileName());
66 attachmentFile.delete();
67
68 issueService.removeIssueAttachment(attachmentId);
69 }
70 } catch(NumberFormatException nfe) {
71 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.removeattachment"));
72 if(log.isDebugEnabled()) {
73 log.debug("Invalid attachmentId " + request.getParameter("id") + " specified.");
74 }
75 }
76 } catch(Exception e) {
77 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
78 }
79
80 if(! errors.isEmpty()) {
81 saveErrors(request, errors);
82 }
83 return mapping.findForward("listattachments");
84 }
85
86 }
87