You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Prowlarr/NzbDrone.Common/DirectoryLookupService.cs

63 lines
1.8 KiB

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace NzbDrone.Common
{
public interface IDirectoryLookupService
{
List<String> LookupSubDirectories(string query);
}
public class DirectoryLookupService : IDirectoryLookupService
{
private readonly IDiskProvider _diskProvider;
public DirectoryLookupService(IDiskProvider diskProvider)
{
_diskProvider = diskProvider;
}
public List<String> LookupSubDirectories(string query)
{
List<String> dirs = null;
try
{
//Windows (Including UNC)
var windowsSep = query.LastIndexOf('\\');
if (windowsSep > -1)
{
var path = query.Substring(0, windowsSep + 1);
var dirsList = _diskProvider.GetDirectories(path).ToList();
if (Path.GetPathRoot(path).Equals(path, StringComparison.InvariantCultureIgnoreCase))
{
var setToRemove = _diskProvider.SpecialFolders;
dirsList.RemoveAll(x => setToRemove.Contains(new DirectoryInfo(x.ToLowerInvariant()).Name));
}
dirs = dirsList;
}
//Unix
var index = query.LastIndexOf('/');
if (index > -1)
{
dirs = _diskProvider.GetDirectories(query.Substring(0, index + 1)).ToList();
}
}
catch (Exception)
{
//Swallow the exceptions so proper JSON is returned to the client (Empty results)
return new List<string>();
}
return dirs;
}
}
}