jellyfin/MediaBrowser.Api/EnvironmentService.cs

303 lines
10 KiB
C#
Raw Normal View History

2016-01-30 05:54:42 +01:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Net;
2013-02-21 02:33:05 +01:00
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Net;
2013-02-21 02:33:05 +01:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2016-10-25 21:02:04 +02:00
using MediaBrowser.Model.Services;
2013-02-21 02:33:05 +01:00
namespace MediaBrowser.Api
{
/// <summary>
/// Class GetDirectoryContents
/// </summary>
2014-03-28 20:58:18 +01:00
[Route("/Environment/DirectoryContents", "GET", Summary = "Gets the contents of a given directory in the file system")]
2013-02-21 02:33:05 +01:00
public class GetDirectoryContents : IReturn<List<FileSystemEntryInfo>>
{
/// <summary>
/// Gets or sets the path.
/// </summary>
/// <value>The path.</value>
2013-03-08 20:14:09 +01:00
[ApiMember(Name = "Path", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
2013-02-21 02:33:05 +01:00
public string Path { get; set; }
2013-03-08 20:14:09 +01:00
2013-02-21 02:33:05 +01: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 23:41:38 +01: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 02:33:05 +01:00
public bool IncludeFiles { get; set; }
2013-03-08 20:14:09 +01:00
2013-02-21 02:33:05 +01: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 23:41:38 +01: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 02:33:05 +01:00
public bool IncludeDirectories { get; set; }
2013-03-08 20:14:09 +01:00
2013-02-21 02:33:05 +01: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 23:41:38 +01: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 02:33:05 +01:00
public bool IncludeHidden { get; set; }
2016-01-30 05:54:42 +01:00
public GetDirectoryContents()
{
IncludeHidden = true;
}
2013-02-21 02:33:05 +01:00
}
2014-03-28 20:58:18 +01:00
[Route("/Environment/NetworkShares", "GET", Summary = "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-21 02:33:05 +01:00
/// <summary>
/// Class GetDrives
/// </summary>
2014-03-28 20:58:18 +01:00
[Route("/Environment/Drives", "GET", Summary = "Gets available drives from the server's file system")]
2013-02-21 02:33:05 +01:00
public class GetDrives : IReturn<List<FileSystemEntryInfo>>
{
}
2013-09-06 22:25:03 +02:00
/// <summary>
/// Class GetNetworkComputers
/// </summary>
2014-03-28 20:58:18 +01:00
[Route("/Environment/NetworkDevices", "GET", Summary = "Gets a list of devices on the network")]
2013-09-06 22:25:03 +02:00
public class GetNetworkDevices : IReturn<List<FileSystemEntryInfo>>
{
}
2014-03-28 20:58:18 +01:00
[Route("/Environment/ParentPath", "GET", Summary = "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; }
}
public class DefaultDirectoryBrowserInfo
{
public string Path { get; set; }
}
[Route("/Environment/DefaultDirectoryBrowser", "GET", Summary = "Gets the parent path of a given path")]
public class GetDefaultDirectoryBrowser : IReturn<DefaultDirectoryBrowserInfo>
{
}
2013-02-21 02:33:05 +01:00
/// <summary>
/// Class EnvironmentService
/// </summary>
2014-12-16 06:46:54 +01:00
[Authenticated(Roles = "Admin", AllowBeforeStartupWizard = true)]
2013-03-16 06:52:33 +01:00
public class EnvironmentService : BaseApiService
2013-02-21 02:33:05 +01:00
{
const char UncSeparator = '\\';
2016-11-01 04:07:45 +01:00
const string UncSeparatorString = "\\";
/// <summary>
/// The _network manager
/// </summary>
private readonly INetworkManager _networkManager;
2015-09-15 03:16:31 +02:00
private IFileSystem _fileSystem;
/// <summary>
/// Initializes a new instance of the <see cref="EnvironmentService" /> class.
/// </summary>
/// <param name="networkManager">The network manager.</param>
2015-09-15 03:16:31 +02:00
public EnvironmentService(INetworkManager networkManager, IFileSystem fileSystem)
{
if (networkManager == null)
{
throw new ArgumentNullException("networkManager");
}
_networkManager = networkManager;
2015-09-15 03:16:31 +02:00
_fileSystem = fileSystem;
}
public object Get(GetDefaultDirectoryBrowser request)
{
var result = new DefaultDirectoryBrowserInfo();
2016-10-29 20:45:07 +02:00
try
{
2016-10-29 20:45:07 +02:00
var qnap = "/share/CACHEDEV1_DATA";
2016-11-01 04:07:45 +01:00
if (_fileSystem.DirectoryExists(qnap))
{
2016-10-29 20:45:07 +02:00
result.Path = qnap;
2016-07-23 22:04:04 +02:00
}
2016-10-29 20:45:07 +02:00
}
catch
{
2016-07-23 22:04:04 +02:00
}
return ToOptimizedResult(result);
}
2013-02-21 02:33:05 +01:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetDirectoryContents request)
{
var path = request.Path;
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException("Path");
}
2016-11-01 04:07:45 +01:00
var networkPrefix = UncSeparatorString + UncSeparatorString;
2013-10-01 23:18:31 +02:00
if (path.StartsWith(networkPrefix, StringComparison.OrdinalIgnoreCase) && path.LastIndexOf(UncSeparator) == 1)
2013-02-21 02:33:05 +01:00
{
return ToOptimizedSerializedResultUsingCache(GetNetworkShares(path).OrderBy(i => i.Path).ToList());
2013-02-21 02:33:05 +01:00
}
2015-09-26 04:31:13 +02:00
return ToOptimizedSerializedResultUsingCache(GetFileSystemEntries(request).OrderBy(i => i.Path).ToList());
2013-02-21 02:33:05 +01:00
}
public object Get(GetNetworkShares request)
{
var path = request.Path;
var shares = GetNetworkShares(path).OrderBy(i => i.Path).ToList();
return ToOptimizedSerializedResultUsingCache(shares);
}
2013-02-21 02:33:05 +01: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 ToOptimizedSerializedResultUsingCache(result);
2013-02-21 02:33:05 +01:00
}
/// <summary>
/// Gets the list that is returned when an empty path is supplied
/// </summary>
/// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
private IEnumerable<FileSystemEntryInfo> GetDrives()
{
2016-11-01 04:07:45 +01:00
return _fileSystem.GetDrives().Select(d => new FileSystemEntryInfo
2013-02-21 02:33:05 +01:00
{
2016-11-01 04:07:45 +01:00
Name = d.Name,
Path = d.FullName,
2013-02-21 02:33:05 +01:00
Type = FileSystemEntryType.Directory
});
}
2013-09-06 22:25:03 +02:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetNetworkDevices request)
{
var result = _networkManager.GetNetworkDevices()
.OrderBy(i => i.Path)
.ToList();
2013-09-06 22:25:03 +02:00
return ToOptimizedSerializedResultUsingCache(result);
2013-09-06 22:25:03 +02:00
}
2013-02-21 02:33:05 +01:00
/// <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 02:33:05 +01:00
{
Name = c.Name,
Path = Path.Combine(path, c.Name),
2013-02-21 02:33:05 +01: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)
{
2015-08-15 22:01:40 +02:00
// using EnumerateFileSystemInfos doesn't handle reparse points (symlinks)
2015-09-15 03:16:31 +02:00
var entries = _fileSystem.GetFileSystemEntries(request.Path).Where(i =>
2013-02-21 02:33:05 +01:00
{
2016-10-25 21:02:04 +02:00
if (!request.IncludeHidden && i.IsHidden)
{
return false;
}
2015-11-12 21:51:39 +01:00
var isDirectory = i.IsDirectory;
if (!request.IncludeFiles && !isDirectory)
{
return false;
}
if (!request.IncludeDirectories && isDirectory)
{
return false;
}
return true;
});
2013-02-21 02:33:05 +01:00
return entries.Select(f => new FileSystemEntryInfo
2013-02-21 02:33:05 +01:00
{
Name = f.Name,
Path = f.FullName,
2015-11-12 21:51:39 +01:00
Type = f.IsDirectory ? FileSystemEntryType.Directory : FileSystemEntryType.File
}).ToList();
2013-02-21 02:33:05 +01:00
}
public object Get(GetParentPath request)
2013-02-21 02:33:05 +01: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-21 02:33:05 +01:00
}
}
}