1 22 23 package org.cofax.module; 24 25 import org.cofax.*; 26 import org.cofax.cms.*; 27 28 import javax.servlet.http.*; 29 import java.io.*; 30 import java.util.*; 31 32 38 39 public class CofaxToolsExtUpload extends CofaxToolsExtModule { 40 41 63 64 68 public String navigate(DataStore db, CofaxPage page, HttpServletRequest req, HttpServletResponse res, HttpSession session) { 69 String mode = (String ) req.getParameter("mode"); 70 71 String contentType = (String ) req.getHeader("content-type"); 72 if (contentType != null) { 73 if (contentType.indexOf("multipart/form-data") > -1) { 74 mode = (String ) req.getAttribute("mode"); 75 } 76 } 77 78 HashMap ht = new HashMap(); 79 CofaxToolsUser user = (CofaxToolsUser) (session.getAttribute("user")); 80 String workingPubName = (String ) user.workingPubName; 81 82 String newFilePath = CofaxToolsServlet.staticFolder; 84 if (!(newFilePath.endsWith(File.separator))) 85 newFilePath += File.separator; 86 newFilePath += user.workingPubName + File.separator; 87 newFilePath += CofaxToolsServlet.userImageFolder + File.separator; 89 newFilePath += (String ) user.userInfoHash.get("USERNAME") + File.separator; 90 91 CofaxToolsUtil.log("Path to store : " + newFilePath); 92 93 page.putGlossaryValue("system:message", getI18NMessage("message_welcome")); 94 95 page.putGlossaryValue("system:highLightTab", "admin"); 96 page.putGlossaryValue("request:pubName", workingPubName); 97 98 if (mode.equals("imageupload_edit_imageupload")) { 99 CofaxToolsNavigation.includeResource(page, "" + CofaxToolsServlet.templatePath + "module/editImageupload.jsp", req, res, session); return ""; 103 } 104 105 CofaxToolsUtil.log("Entering mode : " + mode); 106 107 if (mode.equals("imageupload_upload_imageupload")) { 108 CofaxToolsUtil.log("uploading done "); 109 String errorMessage = ""; 110 boolean uploaded = false; 111 String uploadfile = (String ) req.getAttribute("uploadfile"); 112 CofaxToolsUtil.log("upload file : " + uploadfile); 113 if (uploadfile == null) 114 uploadfile = ""; 115 if (uploadfile.length() >= 5) { 116 String fileName = uploadfile; if (uploadfile.lastIndexOf("/") > -1) { 119 fileName = uploadfile.substring(uploadfile.lastIndexOf("/") + 1); 120 } 121 if (uploadfile.lastIndexOf("\\") > -1) { 122 fileName = uploadfile.substring(uploadfile.lastIndexOf("\\") + 1); 123 } 124 fileName = CofaxToolsUtil.replace(fileName, " ", "_"); 126 fileName = CofaxToolsUtil.replace(fileName, "é", "e"); 127 fileName = CofaxToolsUtil.replace(fileName, "è", "e"); 128 fileName = CofaxToolsUtil.replace(fileName, "à", "a"); 129 fileName = CofaxToolsUtil.replace(fileName, "ù", "u"); 130 fileName = CofaxToolsUtil.replace(fileName, "î", "i"); 131 fileName = CofaxToolsUtil.replace(fileName, "û", "u"); 132 fileName = CofaxToolsUtil.replace(fileName, "'", "_"); 133 134 File createPath = new File(newFilePath); 135 136 CofaxToolsUtil.log("Direcotory to store : " + createPath); 137 CofaxToolsUtil.log("Directory exists ? " + createPath.exists()); 138 139 if (!createPath.exists()) { 140 CofaxToolsUtil.log("CofaxToolsExtUpload: creating directory " + newFilePath); 141 boolean createdDir = false; 142 try { 143 createdDir = createPath.mkdirs(); 144 } catch (Exception e) { 145 CofaxToolsUtil.log("CofaxToolsExtUpload creating " + newFilePath + "threw an exception: " + e); 146 errorMessage += "<br>ERROR creating " + newFilePath + "threw an exception: " + e; 147 } 148 if (!createdDir) { 149 CofaxToolsUtil.log("CofaxToolsExtUpload ERROR creating dir:" + newFilePath); 150 errorMessage += "<br>ERROR creating dir:" + newFilePath; 151 } 152 } 153 String orgFilePath = CofaxToolsServlet.fileTransferFolder; 154 if (!(orgFilePath.endsWith(File.separator))) 155 orgFilePath += File.separator; 156 orgFilePath += CofaxToolsFTP.stripPath(uploadfile); 157 File orgFile = new File(orgFilePath); 158 159 File newFile = new File(newFilePath + File.separator + fileName); 160 161 CofaxToolsUtil.log("orgFile : " + orgFile); 162 CofaxToolsUtil.log("newFile : " + newFile); 163 164 if (newFile.exists()) { 165 newFile.delete(); 166 } 167 try { 168 uploaded = orgFile.renameTo(newFile); 169 CofaxToolsUtil.log("Renaming OK"); 170 } catch (Exception e) { 171 CofaxToolsUtil.log("CofaxToolsExtUpload: failed remaning fileName." + e.toString()); 172 errorMessage += "<br>ERROR failed remaning fileName." + e.toString(); 173 } 174 page.putGlossaryValue("system:errorMessage", errorMessage); 175 page.putGlossaryValue("system:uploaded", uploaded + ""); 176 } 177 CofaxToolsNavigation.includeResource(page, "" + CofaxToolsServlet.templatePath + "module/editImageupload_step2.jsp", req, res, session); return ""; 181 } 182 183 if (mode.equals("imageupload_view_imageupload")) { 184 page.putGlossaryValue("request:localImportDir", newFilePath); 185 page.putGlossaryValue("request:userName", (String ) user.userInfoHash.get("USERNAME")); 186 187 CofaxToolsNavigation.includeResource(page, "" + CofaxToolsServlet.templatePath + "module/editImageupload_step3.jsp", req, res, session); return ""; 191 } 192 193 if (mode.equals("imageupload_delete_imageupload")) { 194 File localDir = new File(newFilePath); 195 String [] localFileList = localDir.list(); 196 if ((localFileList != null) && (localFileList.length != 0)) { 197 for (int i = 0; i < localFileList.length; i++) { 198 String filename = localFileList[i]; 199 String delete = (String ) req.getParameter("" + i); 200 if ((delete != null) && (delete.equals("on"))) { 201 File newFile = new File(newFilePath + File.separator + filename); 202 if (newFile.exists()) { 203 newFile.delete(); 204 } 205 } 206 } 207 } 208 page.putGlossaryValue("request:localImportDir", newFilePath); 209 page.putGlossaryValue("request:userName", (String ) user.userInfoHash.get("USERNAME")); 210 CofaxToolsNavigation.includeResource(page, "" + CofaxToolsServlet.templatePath + "module/editImageupload_step3.jsp", req, res, session); return ""; 214 } 215 216 return ""; 217 } 218 219 public String formatString(String input) { 220 String retVal = CofaxToolsUtil.replace(input, "'", "''"); 221 retVal = CofaxToolsUtil.replace(retVal, "\\", "\\\\"); 222 return retVal; 223 } 224 225 public String getI18NMessage(String message) { 226 ResourceBundle messages; 227 String returnMessage = ""; 228 Locale lcl = CofaxToolsServlet.lcl; 229 try { 230 messages = ResourceBundle.getBundle("org.cofax.module.upload", lcl); 231 returnMessage = messages.getString(message); 232 } catch (Exception e) { 233 CofaxToolsUtil.log("CofaxToolsExtUpload : getI18NMessage : error while reading " + message); 234 } 235 return (returnMessage); 236 } 237 238 } 239 | Popular Tags |