diff --git a/services/src/main/java/org/fao/geonet/api/maintenance/DatadirCleaner.java b/services/src/main/java/org/fao/geonet/api/maintenance/DatadirCleaner.java index 5240278ea3..ae5d647eb8 100644 --- a/services/src/main/java/org/fao/geonet/api/maintenance/DatadirCleaner.java +++ b/services/src/main/java/org/fao/geonet/api/maintenance/DatadirCleaner.java @@ -1,6 +1,8 @@ package org.fao.geonet.api.maintenance; import io.swagger.v3.oas.annotations.tags.Tag; +import org.fao.geonet.kernel.GeonetworkDataDirectory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.security.access.prepost.PreAuthorize; @@ -10,11 +12,19 @@ import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.stream.Stream; + @RequestMapping(value = {"/{portal}/api/maintenance"}) @Tag(name = "maintenance") @Controller("maintenance") public class DatadirCleaner { + @Autowired + GeonetworkDataDirectory geonetworkDataDirectory; + @io.swagger.v3.oas.annotations.Operation(summary = "Clean data dir") @RequestMapping( path = "/cleanDatadir", @@ -24,6 +34,25 @@ public class DatadirCleaner { @PreAuthorize("hasAuthority('UserAdmin')") @ResponseBody public Object cleanDataDir() { + cleanFile(); return "cleaned"; } + + public void cleanFile() { + Path rootPath = geonetworkDataDirectory.getMetadataDataDir(); + + listFilesEatingException(rootPath) // + .flatMap(this::listFilesEatingException) + .map(Path::toString) + .forEach(System.err::println); + } + + private Stream listFilesEatingException(Path path) { + try { + return Files.list(path); + } catch (IOException e) { + e.printStackTrace(); + return Stream.of(); + } + } }