Skip to content

Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.

Enhanced DirectoryService to exclude System and Hidden folders.

Joseph Milazzob89915701548
diff --git a/API/Controllers/LibraryController.cs b/API/Controllers/LibraryController.cs
index 0c6e45d99..4800a8cbb 100644
--- a/API/Controllers/LibraryController.cs
+++ b/API/Controllers/LibraryController.cs
@@ -53,7 +53,7 @@ namespace API.Controllers
return Ok(Directory.GetLogicalDrives());
}
- if (!Directory.Exists(@path)) return BadRequest("This is not a valid path");
+ if (!Directory.Exists(path)) return BadRequest("This is not a valid path");
return Ok(_directoryService.ListDirectory(path));
}
diff --git a/API/Services/DirectoryService.cs b/API/Services/DirectoryService.cs
index 1b333b74c..49de3db48 100644
--- a/API/Services/DirectoryService.cs
+++ b/API/Services/DirectoryService.cs
@@ -1,5 +1,8 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
+using System.Collections.Immutable;
using System.IO;
+using System.Linq;
using System.Threading.Tasks;
using API.Interfaces;
@@ -7,15 +10,22 @@ namespace API.Services
{
public class DirectoryService : IDirectoryService
{
+ /// <summary>
+ /// Lists out top-level folders for a given directory. Filters out System and Hidden folders.
+ /// </summary>
+ /// <param name="rootPath">Absolute path </param>
+ /// <returns>List of folder names</returns>
public IEnumerable<string> ListDirectory(string rootPath)
{
- // TODO: Filter out Hidden and System folders
- // DirectoryInfo di = new DirectoryInfo(@path);
- // var dirs = di.GetDirectories()
- // .Where(dir => (dir.Attributes & FileAttributes.Hidden & FileAttributes.System) == 0).ToImmutableList();
- //
-
- return Directory.GetDirectories(@rootPath);
+ // TODO: Put some checks in here along with API to ensure that we aren't passed a file, folder exists, etc.
+
+ var di = new DirectoryInfo(rootPath);
+ var dirs = di.GetDirectories()
+ .Where(dir => !(dir.Attributes.HasFlag(FileAttributes.Hidden) || dir.Attributes.HasFlag(FileAttributes.System)))
+ .Select(d => d.Name).ToImmutableList();
+
+
+ return dirs;
}
}
}
\ No newline at end of file