2013-03-06 22:34:00 -07:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2013-02-23 10:54:51 -07:00
|
|
|
|
using MediaBrowser.Model.Net;
|
2013-12-07 08:52:38 -07:00
|
|
|
|
using ServiceStack;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class GetDirectoryContents
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Route("/Environment/DirectoryContents", "GET")]
|
2013-03-23 19:45:00 -07:00
|
|
|
|
[Api(Description = "Gets the contents of a given directory in the file system")]
|
2013-02-20 18:33:05 -07:00
|
|
|
|
public class GetDirectoryContents : IReturn<List<FileSystemEntryInfo>>
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The path.</value>
|
2013-03-08 12:14:09 -07:00
|
|
|
|
[ApiMember(Name = "Path", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
|
2013-02-20 18:33:05 -07:00
|
|
|
|
public string Path { get; set; }
|
2013-03-08 12:14:09 -07:00
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets a value indicating whether [include files].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value><c>true</c> if [include files]; otherwise, <c>false</c>.</value>
|
2013-03-08 15:41:38 -07:00
|
|
|
|
[ApiMember(Name = "IncludeFiles", Description = "An optional filter to include or exclude files from the results. true/false", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
|
2013-02-20 18:33:05 -07:00
|
|
|
|
public bool IncludeFiles { get; set; }
|
2013-03-08 12:14:09 -07:00
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets a value indicating whether [include directories].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value><c>true</c> if [include directories]; otherwise, <c>false</c>.</value>
|
2013-03-08 15:41:38 -07:00
|
|
|
|
[ApiMember(Name = "IncludeDirectories", Description = "An optional filter to include or exclude folders from the results. true/false", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
|
2013-02-20 18:33:05 -07:00
|
|
|
|
public bool IncludeDirectories { get; set; }
|
2013-03-08 12:14:09 -07:00
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets a value indicating whether [include hidden].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value><c>true</c> if [include hidden]; otherwise, <c>false</c>.</value>
|
2013-03-08 15:41:38 -07:00
|
|
|
|
[ApiMember(Name = "IncludeHidden", Description = "An optional filter to include or exclude hidden files and folders. true/false", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
|
2013-02-20 18:33:05 -07:00
|
|
|
|
public bool IncludeHidden { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-01 11:26:31 -07:00
|
|
|
|
[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; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class GetDrives
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Route("/Environment/Drives", "GET")]
|
2013-04-10 08:32:09 -07:00
|
|
|
|
[Api(Description = "Gets available drives from the server's file system")]
|
2013-02-20 18:33:05 -07:00
|
|
|
|
public class GetDrives : IReturn<List<FileSystemEntryInfo>>
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-06 13:25:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class GetNetworkComputers
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Route("/Environment/NetworkDevices", "GET")]
|
|
|
|
|
[Api(Description = "Gets a list of devices on the network")]
|
|
|
|
|
public class GetNetworkDevices : IReturn<List<FileSystemEntryInfo>>
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-01 11:26:31 -07:00
|
|
|
|
[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; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class EnvironmentService
|
|
|
|
|
/// </summary>
|
2013-03-15 22:52:33 -07:00
|
|
|
|
public class EnvironmentService : BaseApiService
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2014-01-01 11:26:31 -07:00
|
|
|
|
const char UncSeparator = '\\';
|
|
|
|
|
|
2013-02-23 10:54:51 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _network manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly INetworkManager _networkManager;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="EnvironmentService" /> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="networkManager">The network manager.</param>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">networkManager</exception>
|
|
|
|
|
public EnvironmentService(INetworkManager networkManager)
|
|
|
|
|
{
|
|
|
|
|
if (networkManager == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("networkManager");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_networkManager = networkManager;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">Path</exception>
|
|
|
|
|
/// <exception cref="System.ArgumentException"></exception>
|
|
|
|
|
public object Get(GetDirectoryContents request)
|
|
|
|
|
{
|
|
|
|
|
var path = request.Path;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Path");
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-01 11:26:31 -07:00
|
|
|
|
var networkPrefix = UncSeparator.ToString(CultureInfo.InvariantCulture) + UncSeparator.ToString(CultureInfo.InvariantCulture);
|
2013-10-01 14:18:31 -07:00
|
|
|
|
|
2014-01-01 11:26:31 -07:00
|
|
|
|
if (path.StartsWith(networkPrefix, StringComparison.OrdinalIgnoreCase) && path.LastIndexOf(UncSeparator) == 1)
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2014-02-03 21:04:19 -07:00
|
|
|
|
return ToOptimizedSerializedResultUsingCache(GetNetworkShares(path).OrderBy(i => i.Path).ToList());
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 21:04:19 -07:00
|
|
|
|
return ToOptimizedSerializedResultUsingCache(GetFileSystemEntries(request).OrderBy(i => i.Path).ToList());
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-01 11:26:31 -07:00
|
|
|
|
public object Get(GetNetworkShares request)
|
|
|
|
|
{
|
|
|
|
|
var path = request.Path;
|
|
|
|
|
|
|
|
|
|
var shares = GetNetworkShares(path).OrderBy(i => i.Path).ToList();
|
|
|
|
|
|
2014-02-03 21:04:19 -07:00
|
|
|
|
return ToOptimizedSerializedResultUsingCache(shares);
|
2014-01-01 11:26:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
|
public object Get(GetDrives request)
|
|
|
|
|
{
|
2013-02-28 12:32:41 -07:00
|
|
|
|
var result = GetDrives().ToList();
|
|
|
|
|
|
2014-02-03 21:04:19 -07:00
|
|
|
|
return ToOptimizedSerializedResultUsingCache(result);
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the list that is returned when an empty path is supplied
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
|
|
|
|
|
private IEnumerable<FileSystemEntryInfo> GetDrives()
|
|
|
|
|
{
|
|
|
|
|
// Only include drives in the ready state or this method could end up being very slow, waiting for drives to timeout
|
|
|
|
|
return DriveInfo.GetDrives().Where(d => d.IsReady).Select(d => new FileSystemEntryInfo
|
|
|
|
|
{
|
|
|
|
|
Name = GetName(d),
|
|
|
|
|
Path = d.RootDirectory.FullName,
|
|
|
|
|
Type = FileSystemEntryType.Directory
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-06 13:25:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
|
public object Get(GetNetworkDevices request)
|
|
|
|
|
{
|
2014-01-01 11:26:31 -07:00
|
|
|
|
var result = _networkManager.GetNetworkDevices()
|
|
|
|
|
.OrderBy(i => i.Path)
|
|
|
|
|
.ToList();
|
2013-09-06 13:25:03 -07:00
|
|
|
|
|
2014-02-03 21:04:19 -07:00
|
|
|
|
return ToOptimizedSerializedResultUsingCache(result);
|
2013-09-06 13:25:03 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the name.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="drive">The drive.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
private string GetName(DriveInfo drive)
|
|
|
|
|
{
|
|
|
|
|
return drive.Name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the network shares.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
|
|
|
|
|
private IEnumerable<FileSystemEntryInfo> GetNetworkShares(string path)
|
|
|
|
|
{
|
2013-02-23 10:54:51 -07:00
|
|
|
|
return _networkManager.GetNetworkShares(path).Where(s => s.ShareType == NetworkShareType.Disk).Select(c => new FileSystemEntryInfo
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2013-02-23 10:54:51 -07:00
|
|
|
|
Name = c.Name,
|
|
|
|
|
Path = Path.Combine(path, c.Name),
|
2013-02-20 18:33:05 -07:00
|
|
|
|
Type = FileSystemEntryType.NetworkShare
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the file system entries.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
/// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
|
|
|
|
|
private IEnumerable<FileSystemEntryInfo> GetFileSystemEntries(GetDirectoryContents request)
|
|
|
|
|
{
|
2013-04-27 22:29:27 -07:00
|
|
|
|
var entries = new DirectoryInfo(request.Path).EnumerateFileSystemInfos().Where(i =>
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2013-04-27 16:09:44 -07:00
|
|
|
|
if (!request.IncludeHidden && i.Attributes.HasFlag(FileAttributes.Hidden))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var isDirectory = i.Attributes.HasFlag(FileAttributes.Directory);
|
|
|
|
|
|
|
|
|
|
if (!request.IncludeFiles && !isDirectory)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!request.IncludeDirectories && isDirectory)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-01-01 11:26:31 -07:00
|
|
|
|
|
2013-04-27 16:09:44 -07:00
|
|
|
|
return true;
|
|
|
|
|
});
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2013-04-27 16:09:44 -07:00
|
|
|
|
return entries.Select(f => new FileSystemEntryInfo
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2013-04-27 16:09:44 -07:00
|
|
|
|
Name = f.Name,
|
|
|
|
|
Path = f.FullName,
|
|
|
|
|
Type = f.Attributes.HasFlag(FileAttributes.Directory) ? FileSystemEntryType.Directory : FileSystemEntryType.File
|
|
|
|
|
|
|
|
|
|
}).ToList();
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-01 11:26:31 -07:00
|
|
|
|
public object Get(GetParentPath request)
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2014-01-01 11:26:31 -07:00
|
|
|
|
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;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|