diff --git a/src/main/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadService.java b/src/main/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadService.java index cd50f10a..b47aa93b 100644 --- a/src/main/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadService.java +++ b/src/main/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadService.java @@ -241,10 +241,10 @@ public List preflightUploadFileSystemItem(lon log.debug("map: {}", responses); if (null == alreadyExistingResponse) { - PreflightResponse preflightResponse = handlePreflightFolder(currentAbsolutePath, + PreflightResponse preflightResponse = handlePreflightEntity(currentAbsolutePath, currentFolderName, responses, - uploadParent, authenticatedUser); + uploadParent, authenticatedUser, false); log.debug("Path {} now has the response {}.", currentAbsolutePath, preflightResponse); responses.put(currentAbsolutePath, preflightResponse); @@ -263,7 +263,7 @@ public List preflightUploadFileSystemItem(lon log.debug("here is this file {}", upload); // here is the file. String absolutPathToFile = paths[paths.length - 1]; - PreflightResponse fileResponse = handlePreflightFile(absolutPathToFile, upload.getName(), responses, uploadParent, authenticatedUser); + PreflightResponse fileResponse = handlePreflightEntity(absolutPathToFile, upload.getName(), responses, uploadParent, authenticatedUser, true); log.debug("Response: {} for upload {}", fileResponse, upload); // build the response and add it to list @@ -280,17 +280,17 @@ public List preflightUploadFileSystemItem(lon return preflightResponses; } - public PreflightResponse handlePreflightFolder(String currentAbsolutePath, String currentFolderName, Map responses, FileSystemEntity uploadParent, User authenticatedUser) { + public PreflightResponse handlePreflightEntity(String absolutePath, String currentEntitiyName, Map responses, FileSystemEntity uploadParent, User authenticatedUser, boolean isFile) { // Check if the current name is not valid - if (!inputSanitizerService.pathIsValid(currentFolderName)) { + if (!inputSanitizerService.pathIsValid(currentEntitiyName)) { return PreflightResponse.NAME_WAS_NOT_VALID; } - // there was not a matching path in the map -> find a possible entity in the database - FileSystemEntity alreadyExistingFolder = fileSystemRepository.findByPathAndOwnerId(currentAbsolutePath, uploadParent.getOwnerId()); + // there was not a matching path in the map -> find a possible folder with the same name as the file in the database + FileSystemEntity alreadyExistingFolder = fileSystemRepository.findByPathAndOwnerId(absolutePath, uploadParent.getOwnerId()); if (null == alreadyExistingFolder) { - // current folder does not exist. - String parentPath = fileSystemHelperService.getParentPathFromPath(currentAbsolutePath); + // current path is not taken + String parentPath = fileSystemHelperService.getParentPathFromPath(absolutePath); // GET PARENT long[] parentsChildren = new long[0]; @@ -317,7 +317,11 @@ public PreflightResponse handlePreflightFolder(String currentAbsolutePath, Strin return PreflightResponse.STATEMENT_CANNOT_BE_MADE; case FOLDER_CAN_BE_CREATED: - return PreflightResponse.FOLDER_CAN_BE_CREATED; + if (isFile) { + return PreflightResponse.FILE_CAN_BE_CREATED; + } else { + return PreflightResponse.FOLDER_CAN_BE_CREATED; + } case FOLDER_CAN_BE_MERGED: // 3. get parent from db (cache this with another map) @@ -343,105 +347,43 @@ public PreflightResponse handlePreflightFolder(String currentAbsolutePath, Strin // CHECK PERMISSIONS if (!fileSystemHelperService.userIsAllowedToInteractWithFileSystemEntity(parent, authenticatedUser, InteractionType.READ) || !fileSystemHelperService.userIsAllowedToInteractWithFileSystemEntity(parent, authenticatedUser, InteractionType.CHANGE)) { - return PreflightResponse.FOLDER_CANT_BE_CREATED; + if (isFile) { + return PreflightResponse.FILE_CANT_BE_CREATED; + } else { + return PreflightResponse.FOLDER_CANT_BE_CREATED; + } } // CHECK FOR EXISTING FILE WITH SAME NAME. (we already checked for a folder.) Long[] childrenIdsLong = fileSystemHelperService.transformlongArrayToLong(parentsChildren); - List alreadyExistingFilesWithSameName = fileSystemRepository.findAllByFileSystemIdInAndNameIgnoreCase(Arrays.asList(childrenIdsLong), currentFolderName); + List alreadyExistingFilesWithSameName = fileSystemRepository.findAllByFileSystemIdInAndNameIgnoreCase(Arrays.asList(childrenIdsLong), currentEntitiyName); if (!alreadyExistingFilesWithSameName.isEmpty()) { - return PreflightResponse.FOLDER_CANT_BE_CREATED; + if (isFile) { + return PreflightResponse.FILE_CAN_BE_OVERWRITEN; + } else { + return PreflightResponse.FOLDER_CANT_BE_CREATED; + } } - return PreflightResponse.FOLDER_CAN_BE_CREATED; - } else { - // a folder already exists with the current path. - if (!fileSystemHelperService.userIsAllowedToInteractWithFileSystemEntity(alreadyExistingFolder, authenticatedUser, InteractionType.READ) || - !fileSystemHelperService.userIsAllowedToInteractWithFileSystemEntity(alreadyExistingFolder, authenticatedUser, InteractionType.CHANGE)) { - return PreflightResponse.FOLDER_CANT_BE_MERGED; + + if (isFile) { + return PreflightResponse.FILE_CAN_BE_CREATED; } else { - return PreflightResponse.FOLDER_CAN_BE_MERGED; + return PreflightResponse.FOLDER_CAN_BE_CREATED; } - } - } - - public PreflightResponse handlePreflightFile(String completePathWithFileName, String currentFileName, Map responses, FileSystemEntity uploadParent, User authenticatedUser) { - // Check if the current name is not valid - if (!inputSanitizerService.pathIsValid(currentFileName)) { - return PreflightResponse.NAME_WAS_NOT_VALID; - } - - // there was not a matching path in the map -> find a possible folder with the same name as the file in the database - FileSystemEntity alreadyExistingFolder = fileSystemRepository.findByPathAndOwnerId(completePathWithFileName, uploadParent.getOwnerId()); - if (null == alreadyExistingFolder) { - // current path is not taken - String parentPath = fileSystemHelperService.getParentPathFromPath(completePathWithFileName); - - // GET PARENT - long[] parentsChildren = new long[0]; - FileSystemEntity parent = null; - - // 1. upload parent = parent - if (uploadParent.getPath().equals(parentPath)) { - parentsChildren = uploadParent.getItemIds(); - parent = uploadParent; + } else { + if (isFile) { + // a folder already exists with the current path of the file, so you can't create it + return PreflightResponse.FILE_CANT_BE_CREATED; } else { - PreflightResponse alreadyHandledParent = responses.get(parentPath); - - if (alreadyHandledParent != null) { - // 2. parent is in map - switch (alreadyHandledParent) { - case FOLDER_CANT_BE_CREATED: - - case FOLDER_CANT_BE_MERGED: - - case STATEMENT_CANNOT_BE_MADE: - - case NAME_WAS_NOT_VALID: - // When the parent name was not valid we cannot say something about the children. - return PreflightResponse.STATEMENT_CANNOT_BE_MADE; - - case FOLDER_CAN_BE_CREATED: - return PreflightResponse.FILE_CAN_BE_CREATED; - - case FOLDER_CAN_BE_MERGED: - // 3. get parent from db (cache this with another map) - FileSystemEntity alreadyExistingParentFolder = fileSystemRepository.findByPathAndOwnerId(parentPath, uploadParent.getOwnerId()); - if (alreadyExistingParentFolder == null) { - // 4. exception - throw new FileFighterDataException("Parent folder was not found while upload preflight."); - } - parent = alreadyExistingParentFolder; - parentsChildren = alreadyExistingParentFolder.getItemIds(); - break; - default: - log.warn("Found enum type not explicitly handled {} when trying to handle parent {}.", alreadyHandledParent, parentPath); - } + // a folder already exists with the current path. + if (!fileSystemHelperService.userIsAllowedToInteractWithFileSystemEntity(alreadyExistingFolder, authenticatedUser, InteractionType.READ) || + !fileSystemHelperService.userIsAllowedToInteractWithFileSystemEntity(alreadyExistingFolder, authenticatedUser, InteractionType.CHANGE)) { + return PreflightResponse.FOLDER_CANT_BE_MERGED; } else { - // parent needs to be in the map or the upload parent - throw new FileFighterDataException("Parent folder was not found while upload preflight."); + return PreflightResponse.FOLDER_CAN_BE_MERGED; } } - if (null == parent) - throw new FileFighterDataException("Parent was null."); - - // CHECK PERMISSIONS - if (!fileSystemHelperService.userIsAllowedToInteractWithFileSystemEntity(parent, authenticatedUser, InteractionType.READ) || - !fileSystemHelperService.userIsAllowedToInteractWithFileSystemEntity(parent, authenticatedUser, InteractionType.CHANGE)) { - return PreflightResponse.FILE_CANT_BE_CREATED; - } - - // CHECK FOR EXISTING FILE WITH SAME NAME. (we already checked for a folder.) - Long[] childrenIdsLong = fileSystemHelperService.transformlongArrayToLong(parentsChildren); - List alreadyExistingFilesWithSameName = fileSystemRepository.findAllByFileSystemIdInAndNameIgnoreCase(Arrays.asList(childrenIdsLong), currentFileName); - - if (!alreadyExistingFilesWithSameName.isEmpty()) { - return PreflightResponse.FILE_CAN_BE_OVERWRITEN; - } - return PreflightResponse.FILE_CAN_BE_CREATED; - } else { - // a folder already exists with the current path of the file, so you can't create it - return PreflightResponse.FILE_CANT_BE_CREATED; } } } \ No newline at end of file