Merge pull request #14 from Kareadita/feature/directories

Enhance Directory API to exclude System and Hidden folders
This commit is contained in:
Joseph Milazzo 2020-12-24 10:38:52 -06:00 committed by GitHub
commit 8522f536a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 13 deletions

View File

@ -42,18 +42,16 @@ namespace API.Controllers
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
[Authorize(Policy = "RequireAdminRole")]
[HttpGet("list")]
public ActionResult<IEnumerable<string>> GetDirectories(string path)
{
// TODO: We need some sort of validation other than our auth layer
_logger.Log(LogLevel.Debug, "Listing Directories for " + path);
if (string.IsNullOrEmpty(path))
{
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));
}

View File

@ -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();
//
// TODO: Put some checks in here along with API to ensure that we aren't passed a file, folder exists, etc.
return Directory.GetDirectories(@rootPath);
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;
}
}
}