mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2026-04-22 22:34:53 -04:00
New: Backend changes for new UI
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
@@ -10,13 +10,13 @@ namespace NzbDrone.Common.Disk
|
||||
{
|
||||
public interface IFileSystemLookupService
|
||||
{
|
||||
FileSystemResult LookupContents(string query, bool includeFiles);
|
||||
FileSystemResult LookupContents(string query, bool includeFiles, bool allowFoldersWithoutTrailingSlashes);
|
||||
}
|
||||
|
||||
public class FileSystemLookupService : IFileSystemLookupService
|
||||
{
|
||||
private readonly IDiskProvider _diskProvider;
|
||||
private readonly Logger _logger;
|
||||
private readonly IRuntimeInfo _runtimeInfo;
|
||||
|
||||
private readonly HashSet<string> _setToRemove = new HashSet<string>
|
||||
{
|
||||
@@ -48,20 +48,19 @@ namespace NzbDrone.Common.Disk
|
||||
"@eadir"
|
||||
};
|
||||
|
||||
public FileSystemLookupService(IDiskProvider diskProvider, Logger logger)
|
||||
public FileSystemLookupService(IDiskProvider diskProvider, IRuntimeInfo runtimeInfo)
|
||||
{
|
||||
_diskProvider = diskProvider;
|
||||
_logger = logger;
|
||||
_runtimeInfo = runtimeInfo;
|
||||
}
|
||||
|
||||
public FileSystemResult LookupContents(string query, bool includeFiles)
|
||||
public FileSystemResult LookupContents(string query, bool includeFiles, bool allowFoldersWithoutTrailingSlashes)
|
||||
{
|
||||
var result = new FileSystemResult();
|
||||
|
||||
if (query.IsNullOrWhiteSpace())
|
||||
{
|
||||
if (OsInfo.IsWindows)
|
||||
{
|
||||
var result = new FileSystemResult();
|
||||
result.Directories = GetDrives();
|
||||
|
||||
return result;
|
||||
@@ -70,56 +69,81 @@ namespace NzbDrone.Common.Disk
|
||||
query = "/";
|
||||
}
|
||||
|
||||
if (
|
||||
allowFoldersWithoutTrailingSlashes &&
|
||||
query.IsPathValid() &&
|
||||
_diskProvider.FolderExists(query))
|
||||
{
|
||||
return GetResult(query, includeFiles);
|
||||
}
|
||||
|
||||
var lastSeparatorIndex = query.LastIndexOf(Path.DirectorySeparatorChar);
|
||||
var path = query.Substring(0, lastSeparatorIndex + 1);
|
||||
|
||||
if (lastSeparatorIndex != -1)
|
||||
{
|
||||
try
|
||||
{
|
||||
result.Parent = GetParent(path);
|
||||
result.Directories = GetDirectories(path);
|
||||
|
||||
if (includeFiles)
|
||||
{
|
||||
result.Files = GetFiles(path);
|
||||
}
|
||||
}
|
||||
|
||||
catch (DirectoryNotFoundException)
|
||||
{
|
||||
return new FileSystemResult { Parent = GetParent(path) };
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
return new FileSystemResult();
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
return new FileSystemResult { Parent = GetParent(path) };
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return new FileSystemResult { Parent = GetParent(path) };
|
||||
}
|
||||
return GetResult(path, includeFiles);
|
||||
}
|
||||
|
||||
return result;
|
||||
return new FileSystemResult();
|
||||
}
|
||||
|
||||
private List<FileSystemModel> GetDrives()
|
||||
{
|
||||
return _diskProvider.GetMounts()
|
||||
.Where(d =>
|
||||
{
|
||||
// Fow Windows Services, exclude mapped network drives.
|
||||
if (_runtimeInfo.IsWindowsService)
|
||||
{
|
||||
return d.DriveType != DriveType.Network;
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.Select(d => new FileSystemModel
|
||||
{
|
||||
Type = FileSystemEntityType.Drive,
|
||||
Name = d.VolumeName,
|
||||
Path = d.RootDirectory,
|
||||
LastModified = null
|
||||
})
|
||||
{
|
||||
Type = FileSystemEntityType.Drive,
|
||||
Name = GetVolumeName(d),
|
||||
Path = d.RootDirectory,
|
||||
LastModified = null
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private FileSystemResult GetResult(string path, bool includeFiles)
|
||||
{
|
||||
var result = new FileSystemResult();
|
||||
|
||||
try
|
||||
{
|
||||
result.Parent = GetParent(path);
|
||||
result.Directories = GetDirectories(path);
|
||||
if (includeFiles)
|
||||
{
|
||||
result.Files = GetFiles(path);
|
||||
}
|
||||
}
|
||||
|
||||
catch (DirectoryNotFoundException)
|
||||
{
|
||||
return new FileSystemResult { Parent = GetParent(path) };
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
return new FileSystemResult();
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
return new FileSystemResult { Parent = GetParent(path) };
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return new FileSystemResult { Parent = GetParent(path) };
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<FileSystemModel> GetDirectories(string path)
|
||||
{
|
||||
var directories = _diskProvider.GetDirectoryInfos(path)
|
||||
@@ -154,7 +178,16 @@ namespace NzbDrone.Common.Disk
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private string GetDirectoryPath(string path)
|
||||
private static string GetVolumeName(IMount mountInfo)
|
||||
{
|
||||
if (mountInfo.VolumeLabel.IsNullOrWhiteSpace())
|
||||
{
|
||||
return mountInfo.Name;
|
||||
}
|
||||
return $"{mountInfo.Name} ({mountInfo.VolumeLabel})";
|
||||
}
|
||||
|
||||
private static string GetDirectoryPath(string path)
|
||||
{
|
||||
if (path.Last() != Path.DirectorySeparatorChar)
|
||||
{
|
||||
@@ -164,7 +197,7 @@ namespace NzbDrone.Common.Disk
|
||||
return path;
|
||||
}
|
||||
|
||||
private string GetParent(string path)
|
||||
private static string GetParent(string path)
|
||||
{
|
||||
var di = new DirectoryInfo(path);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user