Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit 85d7bb8

Browse files
authored
refactored preflight of folder and file to one function (#114)
1 parent 9638608 commit 85d7bb8

File tree

1 file changed

+38
-96
lines changed

1 file changed

+38
-96
lines changed

src/main/java/de/filefighter/rest/domain/filesystem/business/FileSystemUploadService.java

Lines changed: 38 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,10 @@ public List<FileSystemUploadPreflightResponse> preflightUploadFileSystemItem(lon
241241
log.debug("map: {}", responses);
242242

243243
if (null == alreadyExistingResponse) {
244-
PreflightResponse preflightResponse = handlePreflightFolder(currentAbsolutePath,
244+
PreflightResponse preflightResponse = handlePreflightEntity(currentAbsolutePath,
245245
currentFolderName,
246246
responses,
247-
uploadParent, authenticatedUser);
247+
uploadParent, authenticatedUser, false);
248248
log.debug("Path {} now has the response {}.", currentAbsolutePath, preflightResponse);
249249
responses.put(currentAbsolutePath, preflightResponse);
250250

@@ -263,7 +263,7 @@ public List<FileSystemUploadPreflightResponse> preflightUploadFileSystemItem(lon
263263
log.debug("here is this file {}", upload);
264264
// here is the file.
265265
String absolutPathToFile = paths[paths.length - 1];
266-
PreflightResponse fileResponse = handlePreflightFile(absolutPathToFile, upload.getName(), responses, uploadParent, authenticatedUser);
266+
PreflightResponse fileResponse = handlePreflightEntity(absolutPathToFile, upload.getName(), responses, uploadParent, authenticatedUser, true);
267267
log.debug("Response: {} for upload {}", fileResponse, upload);
268268

269269
// build the response and add it to list
@@ -280,17 +280,17 @@ public List<FileSystemUploadPreflightResponse> preflightUploadFileSystemItem(lon
280280
return preflightResponses;
281281
}
282282

283-
public PreflightResponse handlePreflightFolder(String currentAbsolutePath, String currentFolderName, Map<String, PreflightResponse> responses, FileSystemEntity uploadParent, User authenticatedUser) {
283+
public PreflightResponse handlePreflightEntity(String absolutePath, String currentEntitiyName, Map<String, PreflightResponse> responses, FileSystemEntity uploadParent, User authenticatedUser, boolean isFile) {
284284
// Check if the current name is not valid
285-
if (!inputSanitizerService.pathIsValid(currentFolderName)) {
285+
if (!inputSanitizerService.pathIsValid(currentEntitiyName)) {
286286
return PreflightResponse.NAME_WAS_NOT_VALID;
287287
}
288288

289-
// there was not a matching path in the map -> find a possible entity in the database
290-
FileSystemEntity alreadyExistingFolder = fileSystemRepository.findByPathAndOwnerId(currentAbsolutePath, uploadParent.getOwnerId());
289+
// there was not a matching path in the map -> find a possible folder with the same name as the file in the database
290+
FileSystemEntity alreadyExistingFolder = fileSystemRepository.findByPathAndOwnerId(absolutePath, uploadParent.getOwnerId());
291291
if (null == alreadyExistingFolder) {
292-
// current folder does not exist.
293-
String parentPath = fileSystemHelperService.getParentPathFromPath(currentAbsolutePath);
292+
// current path is not taken
293+
String parentPath = fileSystemHelperService.getParentPathFromPath(absolutePath);
294294

295295
// GET PARENT
296296
long[] parentsChildren = new long[0];
@@ -317,7 +317,11 @@ public PreflightResponse handlePreflightFolder(String currentAbsolutePath, Strin
317317
return PreflightResponse.STATEMENT_CANNOT_BE_MADE;
318318

319319
case FOLDER_CAN_BE_CREATED:
320-
return PreflightResponse.FOLDER_CAN_BE_CREATED;
320+
if (isFile) {
321+
return PreflightResponse.FILE_CAN_BE_CREATED;
322+
} else {
323+
return PreflightResponse.FOLDER_CAN_BE_CREATED;
324+
}
321325

322326
case FOLDER_CAN_BE_MERGED:
323327
// 3. get parent from db (cache this with another map)
@@ -343,105 +347,43 @@ public PreflightResponse handlePreflightFolder(String currentAbsolutePath, Strin
343347
// CHECK PERMISSIONS
344348
if (!fileSystemHelperService.userIsAllowedToInteractWithFileSystemEntity(parent, authenticatedUser, InteractionType.READ) ||
345349
!fileSystemHelperService.userIsAllowedToInteractWithFileSystemEntity(parent, authenticatedUser, InteractionType.CHANGE)) {
346-
return PreflightResponse.FOLDER_CANT_BE_CREATED;
350+
if (isFile) {
351+
return PreflightResponse.FILE_CANT_BE_CREATED;
352+
} else {
353+
return PreflightResponse.FOLDER_CANT_BE_CREATED;
354+
}
347355
}
348356

349357
// CHECK FOR EXISTING FILE WITH SAME NAME. (we already checked for a folder.)
350358
Long[] childrenIdsLong = fileSystemHelperService.transformlongArrayToLong(parentsChildren);
351-
List<FileSystemEntity> alreadyExistingFilesWithSameName = fileSystemRepository.findAllByFileSystemIdInAndNameIgnoreCase(Arrays.asList(childrenIdsLong), currentFolderName);
359+
List<FileSystemEntity> alreadyExistingFilesWithSameName = fileSystemRepository.findAllByFileSystemIdInAndNameIgnoreCase(Arrays.asList(childrenIdsLong), currentEntitiyName);
352360

353361
if (!alreadyExistingFilesWithSameName.isEmpty()) {
354-
return PreflightResponse.FOLDER_CANT_BE_CREATED;
362+
if (isFile) {
363+
return PreflightResponse.FILE_CAN_BE_OVERWRITEN;
364+
} else {
365+
return PreflightResponse.FOLDER_CANT_BE_CREATED;
366+
}
355367
}
356-
return PreflightResponse.FOLDER_CAN_BE_CREATED;
357-
} else {
358-
// a folder already exists with the current path.
359-
if (!fileSystemHelperService.userIsAllowedToInteractWithFileSystemEntity(alreadyExistingFolder, authenticatedUser, InteractionType.READ) ||
360-
!fileSystemHelperService.userIsAllowedToInteractWithFileSystemEntity(alreadyExistingFolder, authenticatedUser, InteractionType.CHANGE)) {
361-
return PreflightResponse.FOLDER_CANT_BE_MERGED;
368+
369+
if (isFile) {
370+
return PreflightResponse.FILE_CAN_BE_CREATED;
362371
} else {
363-
return PreflightResponse.FOLDER_CAN_BE_MERGED;
372+
return PreflightResponse.FOLDER_CAN_BE_CREATED;
364373
}
365-
}
366-
}
367-
368-
public PreflightResponse handlePreflightFile(String completePathWithFileName, String currentFileName, Map<String, PreflightResponse> responses, FileSystemEntity uploadParent, User authenticatedUser) {
369-
// Check if the current name is not valid
370-
if (!inputSanitizerService.pathIsValid(currentFileName)) {
371-
return PreflightResponse.NAME_WAS_NOT_VALID;
372-
}
373-
374-
// there was not a matching path in the map -> find a possible folder with the same name as the file in the database
375-
FileSystemEntity alreadyExistingFolder = fileSystemRepository.findByPathAndOwnerId(completePathWithFileName, uploadParent.getOwnerId());
376-
if (null == alreadyExistingFolder) {
377-
// current path is not taken
378-
String parentPath = fileSystemHelperService.getParentPathFromPath(completePathWithFileName);
379-
380-
// GET PARENT
381-
long[] parentsChildren = new long[0];
382-
FileSystemEntity parent = null;
383-
384-
// 1. upload parent = parent
385-
if (uploadParent.getPath().equals(parentPath)) {
386-
parentsChildren = uploadParent.getItemIds();
387-
parent = uploadParent;
374+
} else {
375+
if (isFile) {
376+
// a folder already exists with the current path of the file, so you can't create it
377+
return PreflightResponse.FILE_CANT_BE_CREATED;
388378
} else {
389-
PreflightResponse alreadyHandledParent = responses.get(parentPath);
390-
391-
if (alreadyHandledParent != null) {
392-
// 2. parent is in map
393-
switch (alreadyHandledParent) {
394-
case FOLDER_CANT_BE_CREATED:
395-
396-
case FOLDER_CANT_BE_MERGED:
397-
398-
case STATEMENT_CANNOT_BE_MADE:
399-
400-
case NAME_WAS_NOT_VALID:
401-
// When the parent name was not valid we cannot say something about the children.
402-
return PreflightResponse.STATEMENT_CANNOT_BE_MADE;
403-
404-
case FOLDER_CAN_BE_CREATED:
405-
return PreflightResponse.FILE_CAN_BE_CREATED;
406-
407-
case FOLDER_CAN_BE_MERGED:
408-
// 3. get parent from db (cache this with another map)
409-
FileSystemEntity alreadyExistingParentFolder = fileSystemRepository.findByPathAndOwnerId(parentPath, uploadParent.getOwnerId());
410-
if (alreadyExistingParentFolder == null) {
411-
// 4. exception
412-
throw new FileFighterDataException("Parent folder was not found while upload preflight.");
413-
}
414-
parent = alreadyExistingParentFolder;
415-
parentsChildren = alreadyExistingParentFolder.getItemIds();
416-
break;
417-
default:
418-
log.warn("Found enum type not explicitly handled {} when trying to handle parent {}.", alreadyHandledParent, parentPath);
419-
}
379+
// a folder already exists with the current path.
380+
if (!fileSystemHelperService.userIsAllowedToInteractWithFileSystemEntity(alreadyExistingFolder, authenticatedUser, InteractionType.READ) ||
381+
!fileSystemHelperService.userIsAllowedToInteractWithFileSystemEntity(alreadyExistingFolder, authenticatedUser, InteractionType.CHANGE)) {
382+
return PreflightResponse.FOLDER_CANT_BE_MERGED;
420383
} else {
421-
// parent needs to be in the map or the upload parent
422-
throw new FileFighterDataException("Parent folder was not found while upload preflight.");
384+
return PreflightResponse.FOLDER_CAN_BE_MERGED;
423385
}
424386
}
425-
if (null == parent)
426-
throw new FileFighterDataException("Parent was null.");
427-
428-
// CHECK PERMISSIONS
429-
if (!fileSystemHelperService.userIsAllowedToInteractWithFileSystemEntity(parent, authenticatedUser, InteractionType.READ) ||
430-
!fileSystemHelperService.userIsAllowedToInteractWithFileSystemEntity(parent, authenticatedUser, InteractionType.CHANGE)) {
431-
return PreflightResponse.FILE_CANT_BE_CREATED;
432-
}
433-
434-
// CHECK FOR EXISTING FILE WITH SAME NAME. (we already checked for a folder.)
435-
Long[] childrenIdsLong = fileSystemHelperService.transformlongArrayToLong(parentsChildren);
436-
List<FileSystemEntity> alreadyExistingFilesWithSameName = fileSystemRepository.findAllByFileSystemIdInAndNameIgnoreCase(Arrays.asList(childrenIdsLong), currentFileName);
437-
438-
if (!alreadyExistingFilesWithSameName.isEmpty()) {
439-
return PreflightResponse.FILE_CAN_BE_OVERWRITEN;
440-
}
441-
return PreflightResponse.FILE_CAN_BE_CREATED;
442-
} else {
443-
// a folder already exists with the current path of the file, so you can't create it
444-
return PreflightResponse.FILE_CANT_BE_CREATED;
445387
}
446388
}
447389
}

0 commit comments

Comments
 (0)