Enhanced DirectoryService to exclude System and Hidden folders.

This commit is contained in:
Joseph Milazzo 2020-12-24 09:14:48 -06:00
parent fbe2daac6a
commit b899157015
2 changed files with 19 additions and 9 deletions

View File

@ -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));
}

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;
}
}
}