mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2025-09-17 17:14:18 +02:00
cleaned up DirectoryLookupService
This commit is contained in:
57
NzbDrone.Api/Directories/DirectoryLookupService.cs
Normal file
57
NzbDrone.Api/Directories/DirectoryLookupService.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
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>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user