8
8
import de .filefighter .rest .domain .filesystem .data .persistence .FileSystemRepository ;
9
9
import de .filefighter .rest .domain .filesystem .exceptions .FileSystemContentsNotAccessibleException ;
10
10
import de .filefighter .rest .domain .filesystem .exceptions .FileSystemItemCouldNotBeDeletedException ;
11
+ import de .filefighter .rest .domain .filesystem .exceptions .FileSystemItemCouldNotBeDownloadedException ;
11
12
import de .filefighter .rest .domain .filesystem .exceptions .FileSystemItemNotFoundException ;
12
13
import de .filefighter .rest .domain .filesystem .type .FileSystemType ;
13
14
import de .filefighter .rest .domain .filesystem .type .FileSystemTypeRepository ;
14
15
import de .filefighter .rest .domain .user .business .UserBusinessService ;
15
16
import de .filefighter .rest .domain .user .data .dto .User ;
16
17
import de .filefighter .rest .domain .user .exceptions .UserNotFoundException ;
17
- import lombok .AllArgsConstructor ;
18
18
import lombok .extern .log4j .Log4j2 ;
19
19
import org .springframework .stereotype .Service ;
20
20
21
21
import java .util .ArrayList ;
22
22
import java .util .List ;
23
+ import java .util .Objects ;
24
+ import java .util .stream .Collectors ;
23
25
24
26
@ Log4j2
25
27
@ Service
26
28
public class FileSystemBusinessService {
27
29
30
+ public static final String DELETION_FAILED_MSG = "Failed to delete FileSystemEntity with id " ;
28
31
private final FileSystemRepository fileSystemRepository ;
29
32
private final FileSystemHelperService fileSystemHelperService ;
30
33
private final FileSystemTypeRepository fileSystemTypeRepository ;
31
34
private final UserBusinessService userBusinessService ;
32
35
33
- public static final String DELETION_FAILED_MSG = "Failed to delete FileSystemEntity with id " ;
34
-
35
36
public FileSystemBusinessService (FileSystemRepository fileSystemRepository , FileSystemHelperService fileSystemHelperService , FileSystemTypeRepository fileSystemTypeRepository , UserBusinessService userBusinessService ) {
36
37
this .fileSystemRepository = fileSystemRepository ;
37
38
this .fileSystemHelperService = fileSystemHelperService ;
@@ -158,7 +159,7 @@ public List<FileSystemItem> deleteFileSystemItemById(long fsItemId, User authent
158
159
return returnList ;
159
160
}
160
161
161
- private RecursiveReturn recursivlyDeleteFileSystemEntity (FileSystemEntity parentEntity , User authenticatedUser , ArrayList <FileSystemItem > returnList ) {
162
+ private Pair < Boolean , Boolean > recursivlyDeleteFileSystemEntity (FileSystemEntity parentEntity , User authenticatedUser , ArrayList <FileSystemItem > returnList ) {
162
163
boolean foundNonDeletable = false ;
163
164
boolean foundInvisible = false ;
164
165
@@ -173,9 +174,9 @@ private RecursiveReturn recursivlyDeleteFileSystemEntity(FileSystemEntity parent
173
174
for (FileSystemEntity item : items ) {
174
175
if (fileSystemHelperService .userIsAllowedToInteractWithFileSystemEntity (item , authenticatedUser , InteractionType .READ )) {
175
176
if (fileSystemHelperService .userIsAllowedToInteractWithFileSystemEntity (item , authenticatedUser , InteractionType .DELETE )) {
176
- RecursiveReturn recursiveReturn = recursivlyDeleteFileSystemEntity (item , authenticatedUser , returnList );
177
- foundInvisible = recursiveReturn .foundInvisibleEntities || foundInvisible ;
178
- foundNonDeletable = recursiveReturn .foundNonDeletableEntities || foundNonDeletable ;
177
+ Pair < Boolean , Boolean > recursiveReturn = recursivlyDeleteFileSystemEntity (item , authenticatedUser , returnList );
178
+ foundInvisible = recursiveReturn .getFirst () || foundInvisible ;
179
+ foundNonDeletable = recursiveReturn .getSecond () || foundNonDeletable ;
179
180
} else {
180
181
// a entity could not be removed disable the deletion of the parent folder. (current Entity)
181
182
foundNonDeletable = true ;
@@ -206,12 +207,57 @@ private RecursiveReturn recursivlyDeleteFileSystemEntity(FileSystemEntity parent
206
207
returnList .add (fileSystemHelperService .createDTO (parentEntity , authenticatedUser , null ));
207
208
}
208
209
}
209
- return new RecursiveReturn (foundInvisible , foundNonDeletable );
210
+ return new Pair <> (foundInvisible , foundNonDeletable );
210
211
}
211
212
212
- @ AllArgsConstructor
213
- private static class RecursiveReturn {
214
- private final boolean foundInvisibleEntities ;
215
- private final boolean foundNonDeletableEntities ;
213
+ public Pair <List <FileSystemItem >, String > downloadFileSystemEntity (List <Long > ids , User authenticatedUser ) {
214
+ // validate input and check for parent
215
+ if (ids .isEmpty ())
216
+ return new Pair <>(new ArrayList <>(), null );
217
+
218
+ List <FileSystemEntity > uncheckedEntities = ids .stream ()
219
+ .filter (Objects ::nonNull )
220
+ .map (id -> {
221
+ FileSystemEntity possibleEntity = fileSystemRepository .findByFileSystemId (id );
222
+ if (null == possibleEntity )
223
+ throw new FileSystemItemCouldNotBeDownloadedException ("FileSystemEntity does not exist or you are not allowed to see the entity." );
224
+
225
+ return possibleEntity ;
226
+ }).collect (Collectors .toList ());
227
+
228
+ List <FileSystemEntity > checkedEntities = uncheckedEntities .stream ()
229
+ .filter (entity -> fileSystemHelperService .userIsAllowedToInteractWithFileSystemEntity (entity , authenticatedUser , InteractionType .READ ))
230
+ .collect (Collectors .toList ());
231
+
232
+ if (checkedEntities .size () != uncheckedEntities .size ()) {
233
+ log .debug ("Entities size and ids size does not match after validation. pre: {} / after: {}" , uncheckedEntities .size (), checkedEntities .size ());
234
+ throw new FileSystemItemCouldNotBeDownloadedException ("FileSystemEntity does not exist or you are not allowed to see the entity." );
235
+ }
236
+
237
+ boolean allEntitiesAreInRoot = checkedEntities .stream ().allMatch (entity -> !entity .isFile () && entity .getPath ().equals ("/" ));
238
+ boolean singleEntity = checkedEntities .size () == 1 ;
239
+
240
+ List <FileSystemItem > returnList = new ArrayList <>();
241
+ String zipName ;
242
+
243
+ if (singleEntity ) {
244
+ FileSystemEntity currentEntity = checkedEntities .get (0 );
245
+ zipName = fileSystemHelperService .getNameOfZipWhenOnlyOneEntityNeedsToBeDownloaded (currentEntity , allEntitiesAreInRoot );
246
+ fileSystemHelperService .getContentsOfFolderRecursivly (returnList , currentEntity , authenticatedUser , "" , false );
247
+
248
+ } else {
249
+ zipName = fileSystemHelperService .getNameOfZipWhenMultipleEntitiesNeedToBeDownloaded (checkedEntities , allEntitiesAreInRoot );
250
+ if (!allEntitiesAreInRoot ) {
251
+ long countOfDifferentParents = checkedEntities .stream ()
252
+ .map (entity -> fileSystemHelperService .getParentNameEntity ().apply (entity ))
253
+ .distinct ()
254
+ .count ();
255
+
256
+ if (countOfDifferentParents != 1 )
257
+ throw new FileSystemItemCouldNotBeDownloadedException ("FileSystemEntity need to have a common parent entity." );
258
+ }
259
+ checkedEntities .forEach (entity -> fileSystemHelperService .getContentsOfFolderRecursivly (returnList , entity , authenticatedUser , "" , true ));
260
+ }
261
+ return new Pair <>(returnList , zipName );
216
262
}
217
263
}
0 commit comments