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