Changeset 1690 for FCKeditor.Java/branches/2.4/src/main/java/net/fckeditor/connector/ConnectorServlet.java
- Timestamp:
- 2008-03-13 14:08:42 (8 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
FCKeditor.Java/branches/2.4/src/main/java/net/fckeditor/connector/ConnectorServlet.java
r1674 r1690 79 79 80 80 File defaultUserFilesDir = new File(realDefaultUserFilesPath); 81 if (!defaultUserFilesDir.exists()) {82 defaultUserFilesDir.mkdirs();83 }81 UtilsFile.checkDirAndCreate(defaultUserFilesDir); 82 83 // FIXME wrong spelling 84 84 logger.info("ConnectorServlet successfull initialized!"); 85 85 } … … 113 113 XmlResponse xr; 114 114 115 if (! CommandHandler.isValidForGet(commandStr))115 if (!SessionDataHandler.isEnabledForFileBrowsing(request)) 116 116 xr = new XmlResponse(XmlResponse.EN_ERROR, 117 XmlResponse.CM_INVALID_COMMAND);118 else if (! SessionDataHandler.isEnabledForFileBrowsing(request))117 Messages.NOT_AUTHORIZED_FOR_BROWSING); 118 else if (!CommandHandler.isValidForGet(commandStr)) 119 119 xr = new XmlResponse(XmlResponse.EN_ERROR, 120 XmlResponse.CM_NOT_AUTHORIZED);120 Messages.INVALID_COMMAND); 121 121 else if (typeStr != null && !ResourceTypeHandler.isValid(typeStr)) 122 122 xr = new XmlResponse(XmlResponse.EN_ERROR, 123 XmlResponse.CM_INVALID_TYPE);123 Messages.INVALID_TYPE); 124 124 else if (!UtilsFile.isValidPath(currentFolderStr)) 125 xr = new XmlResponse(XmlResponse.EN_INVALID_FOLDER_NAME); 125 xr = new XmlResponse(XmlResponse.EN_ERROR, 126 Messages.INVALID_CURRENT_FOLDER); 126 127 else { 127 128 CommandHandler command = CommandHandler.getCommand(commandStr); 128 129 ResourceTypeHandler resourceType = ResourceTypeHandler 129 130 .getDefaultResourceType(typeStr); 130 // TODO clean up this folder handling mess somehow?! 131 131 132 String typePath = constructResourceTypeUrl(resourceType, request); 132 133 String typeDirPath = getServletContext().getRealPath(typePath); … … 134 135 135 136 File typeDir = new File(typeDirPath); 136 if (!typeDir.exists()) { 137 typeDir.mkdirs(); 138 logger.debug("Type dir '{}' successfully created", typeDirPath); 139 } 137 UtilsFile.checkDirAndCreate(typeDir); 140 138 141 139 File currentDir = new File(typeDir, currentFolderStr); … … 204 202 205 203 logger.debug("Parameter Command: {}", commandStr); 204 logger.debug("Parameter Type: {}", typeStr); 206 205 logger.debug("Parameter CurrentFolder: {}", currentFolderStr); 207 logger.debug("Parameter Type: {}", typeStr); 208 209 if (!ResourceTypeHandler.isValid(typeStr)) 210 logger.warn("Unknown Type requested: {}", typeStr); 211 212 ResourceTypeHandler fileType = ResourceTypeHandler.getDefaultResourceType(typeStr); 213 206 207 UploadResponse ur; 208 209 // FIXME improve comment 214 210 // if this is a QuickUpload-Request, 'commandStr' and 'currentFolderStr' are empty and have 215 211 // to preset and construct the full resource type path !! … … 217 213 commandStr = "QuickUpload"; 218 214 currentFolderStr = "/"; 219 String quickUploadFolder = constructResponseUrl(fileType, currentFolderStr,220 request);221 File file = new File(getServletContext().getRealPath(quickUploadFolder));222 if (!file.exists())223 file.mkdirs();224 215 } 225 226 UploadResponse ur = null; 227 228 if (Utils.isEmpty(commandStr) || Utils.isEmpty(currentFolderStr) 229 || Utils.isEmpty(typeStr)) 230 ur = UploadResponse.UR_BAD_REQUEST; 216 217 if (!SessionDataHandler.isEnabledForFileUpload(request)) 218 ur = new UploadResponse(UploadResponse.EN_SECURITY_ERROR, null, 219 null, Messages.NOT_AUTHORIZED_FOR_UPLOAD); 231 220 else if (!CommandHandler.isValidForPost(commandStr)) 232 ur = UploadResponse.UR_SECURITY_ERROR; 233 else if (!UtilsFile.isValidPath(currentFolderStr)) { 234 ur = new UploadResponse(UploadResponse.EN_SECURITY_ERROR, null, null, 235 "'currentFolderStr' isn't valid!"); 236 } else if (!SessionDataHandler.isEnabledForFileUpload(request)) { 237 ur = new UploadResponse(UploadResponse.EN_SECURITY_ERROR, null, null, 238 "The current user isn't authorized for uploading files!"); 239 } else { 240 String currentPath = constructResponseUrl(fileType, currentFolderStr, request); 241 File currentDirPath = new File(getServletContext().getRealPath(currentPath)); 242 243 if (!UtilsFile.isValidPath(currentFolderStr) || !currentDirPath.exists()) 244 ur = UploadResponse.UR_BAD_REQUEST; 221 ur = new UploadResponse(UploadResponse.EN_ERROR, null, null, 222 Messages.INVALID_COMMAND); 223 else if (typeStr != null && !ResourceTypeHandler.isValid(typeStr)) 224 ur = new UploadResponse(UploadResponse.EN_ERROR, null, null, 225 Messages.INVALID_TYPE); 226 else if (!UtilsFile.isValidPath(currentFolderStr)) 227 ur = new UploadResponse(UploadResponse.EN_ERROR, null, null, 228 Messages.INVALID_CURRENT_FOLDER); 229 else { 230 ResourceTypeHandler resourceType = ResourceTypeHandler.getDefaultResourceType(typeStr); 231 232 String typePath = constructResourceTypeUrl(resourceType, request); 233 String typeDirPath = getServletContext().getRealPath(typePath); 234 String currentPath = typePath + currentFolderStr; 235 236 File typeDir = new File(typeDirPath); 237 UtilsFile.checkDirAndCreate(typeDir); 238 239 File currentDir = new File(typeDir, currentFolderStr); 240 241 if (!currentDir.exists()) 242 ur = new UploadResponse(UploadResponse.EN_ERROR, null, null, 243 Messages.INVALID_CURRENT_FOLDER); 245 244 else { 246 245 247 246 String newFilename = null; 248 247 FileItemFactory factory = new DiskFileItemFactory(); 249 248 ServletFileUpload upload = new ServletFileUpload(factory); 249 250 250 try { 251 251 252 List<FileItem> items = upload.parseRequest(request); 252 253 … … 258 259 String extension = FilenameUtils.getExtension(filename); 259 260 260 boolean validExtension = ExtensionsHandler.isAllowed(fileType, extension); 261 262 if (!validExtension) 263 ur = UploadResponse.UR_INVALID_EXTENSION; 261 if (!ExtensionsHandler.isAllowed(resourceType, extension)) 262 ur = new UploadResponse(UploadResponse.EN_INVALID_EXTENSION); 264 263 else { 265 264 266 265 // construct an unique file name 267 File pathToSave = new File(currentDir Path, filename);266 File pathToSave = new File(currentDir, filename); 268 267 int counter = 1; 269 268 while (pathToSave.exists()) { 270 newFilename = baseName.concat("(").concat(String.valueOf(counter)) 271 .concat(")").concat(".").concat(extension); 272 pathToSave = new File(currentDirPath, newFilename); 269 newFilename = baseName.concat("(").concat( 270 String.valueOf(counter)).concat(")") 271 .concat(".").concat(extension); 272 pathToSave = new File(currentDir, newFilename); 273 273 counter++; 274 274 } 275 275 276 if (Utils.isEmpty(newFilename)) {276 if (Utils.isEmpty(newFilename)) 277 277 ur = new UploadResponse(UploadResponse.EN_OK, Utils 278 .constructServerAddress(request, currentPath).concat(filename)); 279 } else { 280 ur = new UploadResponse(UploadResponse.EN_RENAMED, Utils 281 .constructServerAddress(request, currentPath).concat( 282 newFilename), newFilename); 283 } 278 .constructServerAddress(request, 279 currentPath).concat(filename)); 280 else 281 ur = new UploadResponse(UploadResponse.EN_RENAMED, 282 Utils.constructServerAddress(request, 283 currentPath).concat(newFilename), 284 newFilename); 284 285 285 286 // secure image check 286 if ( fileType.equals(ResourceTypeHandler.IMAGE) && ConnectorHandler.isSecureImageUploads()) {287 boolean check = UtilsFile.isImage(uplFile.getInputStream());288 if ( check) {287 if (resourceType.equals(ResourceTypeHandler.IMAGE) 288 && ConnectorHandler.isSecureImageUploads()) { 289 if (UtilsFile.isImage(uplFile.getInputStream())) 289 290 uplFile.write(pathToSave); 290 }else {291 else { 291 292 uplFile.delete(); 292 ur = UploadResponse.UR_INVALID_EXTENSION; 293 ur = new UploadResponse( 294 UploadResponse.EN_INVALID_EXTENSION); 293 295 } 294 } else {296 } else 295 297 uplFile.write(pathToSave); 296 }297 298 298 299 } 299 } catch (FileUploadException ex) {300 ur = UploadResponse.UR_BAD_REQUEST;301 300 } catch (Exception e) { 302 ur = UploadResponse.UR_SECURITY_ERROR;301 ur = new UploadResponse(UploadResponse.EN_SECURITY_ERROR); 303 302 } 304 303 } 305 } 304 305 } 306 306 307 out.print(ur); 307 308 out.flush(); … … 322 323 323 324 /** 324 * TODO document me 325 * TODO document me! 325 326 * @param resourceType 326 327 * @param request