diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2014-01-01 13:26:31 -0500 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2014-01-01 13:26:31 -0500 |
| commit | b9d17c9bc765a0c59d81db6277300a6860bf8421 (patch) | |
| tree | 8a7c538cb73c27b7e06f0055ce4f0bb45175e7aa /MediaBrowser.Api/EnvironmentService.cs | |
| parent | 88b638fbd69ed99bde7065f66af433b015977cb7 (diff) | |
add more methods to file system interface
Diffstat (limited to 'MediaBrowser.Api/EnvironmentService.cs')
| -rw-r--r-- | MediaBrowser.Api/EnvironmentService.cs | 89 |
1 files changed, 61 insertions, 28 deletions
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<List<FileSystemEntryInfo>> + { + /// <summary> + /// Gets or sets the path. + /// </summary> + /// <value>The path.</value> + [ApiMember(Name = "Path", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")] + public string Path { get; set; } + } + /// <summary> /// Class GetDrives /// </summary> @@ -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<string> + { + /// <summary> + /// Gets or sets the path. + /// </summary> + /// <value>The path.</value> + [ApiMember(Name = "Path", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")] + public string Path { get; set; } + } + /// <summary> /// Class EnvironmentService /// </summary> public class EnvironmentService : BaseApiService { + const char UncSeparator = '\\'; + /// <summary> /// The _network manager /// </summary> @@ -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); + } + /// <summary> /// Gets the specified request. /// </summary> @@ -154,26 +185,14 @@ namespace MediaBrowser.Api /// <returns>System.Object.</returns> 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); } /// <summary> - /// Gets the network computers. - /// </summary> - /// <returns>IEnumerable{FileSystemEntryInfo}.</returns> - private IEnumerable<FileSystemEntryInfo> GetNetworkDevices() - { - return _networkManager.GetNetworkDevices().Select(c => new FileSystemEntryInfo - { - Name = c, - Path = NetworkPrefix + c, - Type = FileSystemEntryType.NetworkComputer - }); - } - - /// <summary> /// Gets the name. /// </summary> /// <param name="drive">The drive.</param> @@ -223,7 +242,7 @@ namespace MediaBrowser.Api { return false; } - + return true; }); @@ -236,13 +255,27 @@ namespace MediaBrowser.Api }).ToList(); } - /// <summary> - /// Gets the network prefix. - /// </summary> - /// <value>The network prefix.</value> - 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; } } } |
