From b9d17c9bc765a0c59d81db6277300a6860bf8421 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 1 Jan 2014 13:26:31 -0500 Subject: add more methods to file system interface --- MediaBrowser.Api/EnvironmentService.cs | 89 +++++++++++++++++++++++----------- 1 file changed, 61 insertions(+), 28 deletions(-) (limited to 'MediaBrowser.Api/EnvironmentService.cs') diff --git a/MediaBrowser.Api/EnvironmentService.cs b/MediaBrowser.Api/EnvironmentService.cs index dfdd0daf0..aac708520 100644 --- a/MediaBrowser.Api/EnvironmentService.cs +++ b/MediaBrowser.Api/EnvironmentService.cs @@ -46,6 +46,18 @@ namespace MediaBrowser.Api public bool IncludeHidden { get; set; } } + [Route("/Environment/NetworkShares", "GET")] + [Api(Description = "Gets shares from a network device")] + public class GetNetworkShares : IReturn> + { + /// + /// Gets or sets the path. + /// + /// The path. + [ApiMember(Name = "Path", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")] + public string Path { get; set; } + } + /// /// Class GetDrives /// @@ -64,11 +76,25 @@ namespace MediaBrowser.Api { } + [Route("/Environment/ParentPath", "GET")] + [Api(Description = "Gets the parent path of a given path")] + public class GetParentPath : IReturn + { + /// + /// Gets or sets the path. + /// + /// The path. + [ApiMember(Name = "Path", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")] + public string Path { get; set; } + } + /// /// Class EnvironmentService /// public class EnvironmentService : BaseApiService { + const char UncSeparator = '\\'; + /// /// The _network manager /// @@ -105,13 +131,9 @@ namespace MediaBrowser.Api throw new ArgumentNullException("Path"); } - // If it's not a drive trim trailing slashes. - if (!path.EndsWith(":\\")) - { - path = path.TrimEnd('\\'); - } + var networkPrefix = UncSeparator.ToString(CultureInfo.InvariantCulture) + UncSeparator.ToString(CultureInfo.InvariantCulture); - if (path.StartsWith(NetworkPrefix, StringComparison.OrdinalIgnoreCase) && path.LastIndexOf('\\') == 1) + if (path.StartsWith(networkPrefix, StringComparison.OrdinalIgnoreCase) && path.LastIndexOf(UncSeparator) == 1) { return ToOptimizedResult(GetNetworkShares(path).OrderBy(i => i.Path).ToList()); } @@ -119,6 +141,15 @@ namespace MediaBrowser.Api return ToOptimizedResult(GetFileSystemEntries(request).OrderBy(i => i.Path).ToList()); } + public object Get(GetNetworkShares request) + { + var path = request.Path; + + var shares = GetNetworkShares(path).OrderBy(i => i.Path).ToList(); + + return ToOptimizedResult(shares); + } + /// /// Gets the specified request. /// @@ -154,25 +185,13 @@ namespace MediaBrowser.Api /// System.Object. public object Get(GetNetworkDevices request) { - var result = GetNetworkDevices().OrderBy(i => i.Path).ToList(); + var result = _networkManager.GetNetworkDevices() + .OrderBy(i => i.Path) + .ToList(); return ToOptimizedResult(result); } - /// - /// Gets the network computers. - /// - /// IEnumerable{FileSystemEntryInfo}. - private IEnumerable GetNetworkDevices() - { - return _networkManager.GetNetworkDevices().Select(c => new FileSystemEntryInfo - { - Name = c, - Path = NetworkPrefix + c, - Type = FileSystemEntryType.NetworkComputer - }); - } - /// /// Gets the name. /// @@ -223,7 +242,7 @@ namespace MediaBrowser.Api { return false; } - + return true; }); @@ -236,13 +255,27 @@ namespace MediaBrowser.Api }).ToList(); } - /// - /// Gets the network prefix. - /// - /// The network prefix. - private string NetworkPrefix + public object Get(GetParentPath request) { - get { return Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture) + Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture); } + var parent = Path.GetDirectoryName(request.Path); + + if (string.IsNullOrEmpty(parent)) + { + // Check if unc share + var index = request.Path.LastIndexOf(UncSeparator); + + if (index != -1 && request.Path.IndexOf(UncSeparator) == 0) + { + parent = request.Path.Substring(0, index); + + if (string.IsNullOrWhiteSpace(parent.TrimStart(UncSeparator))) + { + parent = null; + } + } + } + + return parent; } } } -- cgit v1.2.3