jellyfin-server/MediaBrowser.Api/EnvironmentService.cs

240 lines
8.7 KiB
C#
Raw Normal View History

2013-03-07 05:34:00 +00:00
using MediaBrowser.Common.Net;
2013-02-22 04:23:06 +00:00
using MediaBrowser.Controller.IO;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Net;
2013-02-21 01:33:05 +00:00
using ServiceStack.ServiceHost;
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")]
[Api(Description = "Gets the contents of a given directory in the file system")]
2013-02-21 01:33:05 +00:00
public class GetDirectoryContents : IReturn<List<FileSystemEntryInfo>>
{
/// <summary>
/// Gets or sets the path.
/// </summary>
/// <value>The path.</value>
2013-03-08 19:14:09 +00:00
[ApiMember(Name = "Path", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
2013-02-21 01:33:05 +00:00
public string Path { get; set; }
2013-03-08 19:14:09 +00:00
2013-02-21 01:33:05 +00: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 22:41:38 +00: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-21 01:33:05 +00:00
public bool IncludeFiles { get; set; }
2013-03-08 19:14:09 +00:00
2013-02-21 01:33:05 +00: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 22:41:38 +00: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-21 01:33:05 +00:00
public bool IncludeDirectories { get; set; }
2013-03-08 19:14:09 +00:00
2013-02-21 01:33:05 +00: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 22:41:38 +00: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-21 01:33:05 +00:00
public bool IncludeHidden { get; set; }
2013-03-08 19:14:09 +00:00
public GetDirectoryContents()
{
IncludeDirectories = true;
IncludeFiles = true;
}
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Class GetDrives
/// </summary>
[Route("/Environment/Drives", "GET")]
2013-03-08 19:14:09 +00:00
[ServiceStack.ServiceHost.Api(Description = "Gets available drives from the server's file system")]
2013-02-21 01:33:05 +00:00
public class GetDrives : IReturn<List<FileSystemEntryInfo>>
{
}
/// <summary>
/// Class GetNetworkComputers
/// </summary>
2013-03-08 19:14:09 +00:00
[Route("/Environment/NetworkDevices", "GET")]
[ServiceStack.ServiceHost.Api(Description = "Gets a list of devices on the network")]
public class GetNetworkDevices : IReturn<List<FileSystemEntryInfo>>
2013-02-21 01:33:05 +00:00
{
}
/// <summary>
/// Class EnvironmentService
/// </summary>
2013-03-16 05:52:33 +00:00
public class EnvironmentService : BaseApiService
2013-02-21 01:33:05 +00: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-21 01:33:05 +00: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");
}
if (path.StartsWith(NetworkPrefix, StringComparison.OrdinalIgnoreCase) && path.LastIndexOf('\\') == 1)
2013-02-21 01:33:05 +00:00
{
return ToOptimizedResult(GetNetworkShares(path).ToList());
2013-02-21 01:33:05 +00:00
}
// Reject invalid input
if (!Path.IsPathRooted(path))
2013-02-21 01:33:05 +00:00
{
throw new ArgumentException(string.Format("Invalid path: {0}", path));
2013-02-21 01:33:05 +00:00
}
return ToOptimizedResult(GetFileSystemEntries(request).ToList());
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetDrives request)
{
var result = GetDrives().ToList();
return ToOptimizedResult(result);
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2013-03-08 19:14:09 +00:00
public object Get(GetNetworkDevices request)
2013-02-21 01:33:05 +00:00
{
2013-03-08 19:14:09 +00:00
var result = GetNetworkDevices().ToList();
return ToOptimizedResult(result);
2013-02-21 01:33:05 +00: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
});
}
/// <summary>
/// Gets the network computers.
/// </summary>
/// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
2013-03-08 19:14:09 +00:00
private IEnumerable<FileSystemEntryInfo> GetNetworkDevices()
2013-02-21 01:33:05 +00:00
{
return _networkManager.GetNetworkDevices().Select(c => new FileSystemEntryInfo
2013-02-21 01:33:05 +00:00
{
Name = c,
Path = NetworkPrefix + c,
Type = FileSystemEntryType.NetworkComputer
});
}
/// <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)
{
return _networkManager.GetNetworkShares(path).Where(s => s.ShareType == NetworkShareType.Disk).Select(c => new FileSystemEntryInfo
2013-02-21 01:33:05 +00:00
{
Name = c.Name,
Path = Path.Combine(path, c.Name),
2013-02-21 01:33:05 +00: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)
{
var fileSystemEntries = FileSystem.GetFileSystemEntries(request.Path, "*", request.IncludeFiles, request.IncludeDirectories).Where(f => !f.IsSystemFile);
if (!request.IncludeHidden)
{
fileSystemEntries = fileSystemEntries.Where(f => !f.IsHidden);
}
return fileSystemEntries.Select(f => new FileSystemEntryInfo
{
Name = f.cFileName,
Path = f.Path,
Type = f.IsDirectory ? FileSystemEntryType.Directory : FileSystemEntryType.File
});
}
/// <summary>
/// Gets the network prefix.
/// </summary>
/// <value>The network prefix.</value>
private string NetworkPrefix
{
get { return Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture) + Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture); }
}
}
}