From 21a1753a08ea4d3f00e4433f26bd131dfe116b44 Mon Sep 17 00:00:00 2001 From: okanmercan Date: Thu, 7 Nov 2024 13:26:38 +0300 Subject: [PATCH 1/3] :bug: fix: file path selection bug. --- .../io/tofhir/server/model/FilePathNode.scala | 3 ++- .../fhir/FileSystemTreeStructureService.scala | 15 ++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/tofhir-server/src/main/scala/io/tofhir/server/model/FilePathNode.scala b/tofhir-server/src/main/scala/io/tofhir/server/model/FilePathNode.scala index 48a4de74..94926ef6 100644 --- a/tofhir-server/src/main/scala/io/tofhir/server/model/FilePathNode.scala +++ b/tofhir-server/src/main/scala/io/tofhir/server/model/FilePathNode.scala @@ -5,5 +5,6 @@ package io.tofhir.server.model * @param label The name of the file * @param isFolder True if the file is a folder * @param children The children of the folder + * @param path file path of the node */ -case class FilePathNode(label: String, isFolder: Boolean, children: List[FilePathNode]) +case class FilePathNode(label: String, isFolder: Boolean, children: List[FilePathNode], path: String) diff --git a/tofhir-server/src/main/scala/io/tofhir/server/service/fhir/FileSystemTreeStructureService.scala b/tofhir-server/src/main/scala/io/tofhir/server/service/fhir/FileSystemTreeStructureService.scala index fe6a8eea..dc216db9 100644 --- a/tofhir-server/src/main/scala/io/tofhir/server/service/fhir/FileSystemTreeStructureService.scala +++ b/tofhir-server/src/main/scala/io/tofhir/server/service/fhir/FileSystemTreeStructureService.scala @@ -21,14 +21,14 @@ class FileSystemTreeStructureService { /** * Get the folder tree structure of the given base path - * @param basePath The base path to start with + * @param basePathString The base path to start with * @param includeFiles if false, only folders will be included in the tree * @return */ - def getFolderTreeStructure(basePath: String, includeFiles: Boolean): FilePathNode = { + def getFolderTreeStructure(basePathString: String, includeFiles: Boolean): FilePathNode = { var fileCount = 0 // Track total number of files processed - def buildNode(path: Path): FilePathNode = { + def buildNode(path: Path, basePath: Path): FilePathNode = { if (fileCount >= FILE_LIMIT) { throw new IllegalStateException(s"File limit exceeded. The maximum number of files to process is $FILE_LIMIT.") } @@ -41,7 +41,7 @@ class FileSystemTreeStructureService { .filterNot(shouldBeExcluded) // Filter out hidden files, folders .map { p => fileCount += 1 - buildNode(p) + buildNode(p, basePath) } .toList } finally { @@ -50,14 +50,15 @@ class FileSystemTreeStructureService { } else { List.empty[FilePathNode] } - FilePathNode(path.getFileName.toString, Files.isDirectory(path), children) + FilePathNode(path.getFileName.toString, Files.isDirectory(path), children, basePath.relativize(path).toString.replaceAll("\\\\", "/")) } - val path = FileUtils.getPath(basePath) + val path = FileUtils.getPath(basePathString) + val basePath = FileUtils.getPath(basePathString) if(!path.toAbsolutePath.normalize().startsWith(FileUtils.getPath("").toAbsolutePath)) { throw new IllegalArgumentException("The given path is outside the root context path.") } - buildNode(path) + buildNode(path, basePath) } } From e2b83a0df45e9b7915a976995ce815aa9cac3d59 Mon Sep 17 00:00:00 2001 From: okanmercan Date: Fri, 8 Nov 2024 09:17:29 +0300 Subject: [PATCH 2/3] :recycle: refactor: Remove basePath from buildNode function. --- .../io/tofhir/server/model/FilePathNode.scala | 4 ++-- .../fhir/FileSystemTreeStructureService.scala | 20 +++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/tofhir-server/src/main/scala/io/tofhir/server/model/FilePathNode.scala b/tofhir-server/src/main/scala/io/tofhir/server/model/FilePathNode.scala index 94926ef6..c244dbc9 100644 --- a/tofhir-server/src/main/scala/io/tofhir/server/model/FilePathNode.scala +++ b/tofhir-server/src/main/scala/io/tofhir/server/model/FilePathNode.scala @@ -4,7 +4,7 @@ package io.tofhir.server.model * Represents a node for each file in the file system * @param label The name of the file * @param isFolder True if the file is a folder - * @param children The children of the folder * @param path file path of the node + * @param children The children of the folder */ -case class FilePathNode(label: String, isFolder: Boolean, children: List[FilePathNode], path: String) +case class FilePathNode(label: String, isFolder: Boolean, path: String, children: List[FilePathNode]) diff --git a/tofhir-server/src/main/scala/io/tofhir/server/service/fhir/FileSystemTreeStructureService.scala b/tofhir-server/src/main/scala/io/tofhir/server/service/fhir/FileSystemTreeStructureService.scala index dc216db9..a1342d4d 100644 --- a/tofhir-server/src/main/scala/io/tofhir/server/service/fhir/FileSystemTreeStructureService.scala +++ b/tofhir-server/src/main/scala/io/tofhir/server/service/fhir/FileSystemTreeStructureService.scala @@ -28,7 +28,13 @@ class FileSystemTreeStructureService { def getFolderTreeStructure(basePathString: String, includeFiles: Boolean): FilePathNode = { var fileCount = 0 // Track total number of files processed - def buildNode(path: Path, basePath: Path): FilePathNode = { + val path = FileUtils.getPath(basePathString) + val basePath = FileUtils.getPath(basePathString) + if(!path.toAbsolutePath.normalize().startsWith(FileUtils.getPath("").toAbsolutePath)) { + throw new IllegalArgumentException("The given path is outside the root context path.") + } + + def buildNode(path: Path): FilePathNode = { if (fileCount >= FILE_LIMIT) { throw new IllegalStateException(s"File limit exceeded. The maximum number of files to process is $FILE_LIMIT.") } @@ -41,7 +47,7 @@ class FileSystemTreeStructureService { .filterNot(shouldBeExcluded) // Filter out hidden files, folders .map { p => fileCount += 1 - buildNode(p, basePath) + buildNode(p) } .toList } finally { @@ -50,15 +56,9 @@ class FileSystemTreeStructureService { } else { List.empty[FilePathNode] } - FilePathNode(path.getFileName.toString, Files.isDirectory(path), children, basePath.relativize(path).toString.replaceAll("\\\\", "/")) - } - - val path = FileUtils.getPath(basePathString) - val basePath = FileUtils.getPath(basePathString) - if(!path.toAbsolutePath.normalize().startsWith(FileUtils.getPath("").toAbsolutePath)) { - throw new IllegalArgumentException("The given path is outside the root context path.") + FilePathNode(path.getFileName.toString, Files.isDirectory(path), basePath.relativize(path).toString.replaceAll("\\\\", "/"), children) } - buildNode(path, basePath) + buildNode(path) } } From b42ad385c7b1edfb876cb5fada7aa0b9641a29db Mon Sep 17 00:00:00 2001 From: okanmercan Date: Fri, 8 Nov 2024 13:13:20 +0300 Subject: [PATCH 3/3] :recycle: refactor: Remove path variable. --- .../server/service/fhir/FileSystemTreeStructureService.scala | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tofhir-server/src/main/scala/io/tofhir/server/service/fhir/FileSystemTreeStructureService.scala b/tofhir-server/src/main/scala/io/tofhir/server/service/fhir/FileSystemTreeStructureService.scala index a1342d4d..0650b2d2 100644 --- a/tofhir-server/src/main/scala/io/tofhir/server/service/fhir/FileSystemTreeStructureService.scala +++ b/tofhir-server/src/main/scala/io/tofhir/server/service/fhir/FileSystemTreeStructureService.scala @@ -28,9 +28,8 @@ class FileSystemTreeStructureService { def getFolderTreeStructure(basePathString: String, includeFiles: Boolean): FilePathNode = { var fileCount = 0 // Track total number of files processed - val path = FileUtils.getPath(basePathString) val basePath = FileUtils.getPath(basePathString) - if(!path.toAbsolutePath.normalize().startsWith(FileUtils.getPath("").toAbsolutePath)) { + if(!basePath.toAbsolutePath.normalize().startsWith(FileUtils.getPath("").toAbsolutePath)) { throw new IllegalArgumentException("The given path is outside the root context path.") } @@ -58,7 +57,7 @@ class FileSystemTreeStructureService { } FilePathNode(path.getFileName.toString, Files.isDirectory(path), basePath.relativize(path).toString.replaceAll("\\\\", "/"), children) } - buildNode(path) + buildNode(basePath) } }