using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MediaBrowser.Model.Services;
namespace MediaBrowser.Api
{
///
/// Class GetDirectoryContents
///
[Route("/Environment/DirectoryContents", "GET", Summary = "Gets the contents of a given directory in the file system")]
public class GetDirectoryContents : IReturn>
{
///
/// Gets or sets the path.
///
/// The path.
[ApiMember(Name = "Path", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
public string Path { get; set; }
///
/// Gets or sets a value indicating whether [include files].
///
/// true if [include files]; otherwise, false.
[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")]
public bool IncludeFiles { get; set; }
///
/// Gets or sets a value indicating whether [include directories].
///
/// true if [include directories]; otherwise, false.
[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")]
public bool IncludeDirectories { get; set; }
///
/// Gets or sets a value indicating whether [include hidden].
///
/// true if [include hidden]; otherwise, false.
[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")]
public bool IncludeHidden { get; set; }
public GetDirectoryContents()
{
IncludeHidden = true;
}
}
[Route("/Environment/ValidatePath", "POST", Summary = "Gets the contents of a given directory in the file system")]
public class ValidatePath
{
///
/// Gets or sets the path.
///
/// The path.
[ApiMember(Name = "Path", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Path { get; set; }
public bool ValidateWriteable { get; set; }
public bool? IsFile { get; set; }
}
[Route("/Environment/NetworkShares", "GET", Summary = "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
///
[Route("/Environment/Drives", "GET", Summary = "Gets available drives from the server's file system")]
public class GetDrives : IReturn>
{
}
///
/// Class GetNetworkComputers
///
[Route("/Environment/NetworkDevices", "GET", Summary = "Gets a list of devices on the network")]
public class GetNetworkDevices : IReturn>
{
}
[Route("/Environment/ParentPath", "GET", Summary = "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; }
}
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
{
}
///
/// Class EnvironmentService
///
[Authenticated(Roles = "Admin", AllowBeforeStartupWizard = true)]
public class EnvironmentService : BaseApiService
{
const char UncSeparator = '\\';
const string UncSeparatorString = "\\";
///
/// The _network manager
///
private readonly INetworkManager _networkManager;
private readonly IFileSystem _fileSystem;
///
/// Initializes a new instance of the class.
///
/// The network manager.
public EnvironmentService(INetworkManager networkManager, IFileSystem fileSystem)
{
if (networkManager == null)
{
throw new ArgumentNullException("networkManager");
}
_networkManager = networkManager;
_fileSystem = fileSystem;
}
public void Post(ValidatePath request)
{
if (request.IsFile.HasValue)
{
if (request.IsFile.Value)
{
if (!_fileSystem.FileExists(request.Path))
{
throw new FileNotFoundException("File not found", request.Path);
}
}
else
{
if (!_fileSystem.DirectoryExists(request.Path))
{
throw new FileNotFoundException("File not found", request.Path);
}
}
}
else
{
if (!_fileSystem.FileExists(request.Path) && !_fileSystem.DirectoryExists(request.Path))
{
throw new FileNotFoundException("Path not found", request.Path);
}
if (request.ValidateWriteable)
{
EnsureWriteAccess(request.Path);
}
}
}
protected void EnsureWriteAccess(string path)
{
var file = Path.Combine(path, Guid.NewGuid().ToString());
_fileSystem.WriteAllText(file, string.Empty);
_fileSystem.DeleteFile(file);
}
public object Get(GetDefaultDirectoryBrowser request)
{
var result = new DefaultDirectoryBrowserInfo();
try
{
var qnap = "/share/CACHEDEV1_DATA";
if (_fileSystem.DirectoryExists(qnap))
{
result.Path = qnap;
}
}
catch
{
}
return ToOptimizedResult(result);
}
///
/// Gets the specified request.
///
/// The request.
/// System.Object.
public object Get(GetDirectoryContents request)
{
var path = request.Path;
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException("Path");
}
var networkPrefix = UncSeparatorString + UncSeparatorString;
if (path.StartsWith(networkPrefix, StringComparison.OrdinalIgnoreCase) && path.LastIndexOf(UncSeparator) == 1)
{
return ToOptimizedSerializedResultUsingCache(GetNetworkShares(path).OrderBy(i => i.Path).ToList());
}
return ToOptimizedSerializedResultUsingCache(GetFileSystemEntries(request).ToList());
}
public object Get(GetNetworkShares request)
{
var path = request.Path;
var shares = GetNetworkShares(path).OrderBy(i => i.Path).ToList();
return ToOptimizedSerializedResultUsingCache(shares);
}
///
/// Gets the specified request.
///
/// The request.
/// System.Object.
public object Get(GetDrives request)
{
var result = GetDrives().ToList();
return ToOptimizedSerializedResultUsingCache(result);
}
///
/// Gets the list that is returned when an empty path is supplied
///
/// IEnumerable{FileSystemEntryInfo}.
private IEnumerable GetDrives()
{
return _fileSystem.GetDrives().Select(d => new FileSystemEntryInfo
{
Name = d.Name,
Path = d.FullName,
Type = FileSystemEntryType.Directory
});
}
///
/// Gets the specified request.
///
/// The request.
/// System.Object.
public object Get(GetNetworkDevices request)
{
var result = _networkManager.GetNetworkDevices().ToList();
return ToOptimizedSerializedResultUsingCache(result);
}
///
/// Gets the network shares.
///
/// The path.
/// IEnumerable{FileSystemEntryInfo}.
private IEnumerable GetNetworkShares(string path)
{
return _networkManager.GetNetworkShares(path).Where(s => s.ShareType == NetworkShareType.Disk).Select(c => new FileSystemEntryInfo
{
Name = c.Name,
Path = Path.Combine(path, c.Name),
Type = FileSystemEntryType.NetworkShare
});
}
///
/// Gets the file system entries.
///
/// The request.
/// IEnumerable{FileSystemEntryInfo}.
private IEnumerable GetFileSystemEntries(GetDirectoryContents request)
{
var entries = _fileSystem.GetFileSystemEntries(request.Path).Where(i =>
{
if (!request.IncludeHidden && i.IsHidden)
{
return false;
}
var isDirectory = i.IsDirectory;
if (!request.IncludeFiles && !isDirectory)
{
return false;
}
if (!request.IncludeDirectories && isDirectory)
{
return false;
}
return true;
});
return entries.Select(f => new FileSystemEntryInfo
{
Name = f.Name,
Path = f.FullName,
Type = f.IsDirectory ? FileSystemEntryType.Directory : FileSystemEntryType.File
});
}
public object Get(GetParentPath request)
{
var parent = _fileSystem.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;
}
}
}