1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-14 11:23:42 +02:00
Sonarr/NzbDrone.Api/Directories/DirectoryLookupService.cs
2013-08-05 15:10:31 -07:00

58 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NzbDrone.Common;
namespace NzbDrone.Api.Directories
{
public interface IDirectoryLookupService
{
List<string> LookupSubDirectories(string query);
}
public class DirectoryLookupService : IDirectoryLookupService
{
private readonly IDiskProvider _diskProvider;
private readonly HashSet<string> _setToRemove = new HashSet<string> { "$Recycle.Bin", "System Volume Information" };
public DirectoryLookupService(IDiskProvider diskProvider)
{
_diskProvider = diskProvider;
}
public List<string> LookupSubDirectories(string query)
{
var dirs = new List<string>();
var lastSeparatorIndex = query.LastIndexOf(Path.DirectorySeparatorChar);
var path = query.Substring(0, lastSeparatorIndex + 1);
if (lastSeparatorIndex != -1)
{
dirs = GetSubDirectories(path);
dirs.RemoveAll(x => _setToRemove.Contains(new DirectoryInfo(x).Name));
}
return dirs;
}
private List<string> GetSubDirectories(string path)
{
try
{
return _diskProvider.GetDirectories(path).ToList();
}
catch (DirectoryNotFoundException)
{
return new List<string>();
}
catch (ArgumentException)
{
return new List<string>();
}
}
}
}