Radarr/src/NzbDrone.Api/FileSystem/FileSystemModule.cs

72 lines
2.4 KiB
C#
Raw Normal View History

2018-11-23 07:03:32 +00:00
using System;
2015-03-04 00:42:37 +00:00
using System.IO;
using System.Linq;
using Nancy;
2018-11-23 07:03:32 +00:00
using Radarr.Http.Extensions;
using NzbDrone.Common.Disk;
2015-03-04 00:42:37 +00:00
using NzbDrone.Common.Extensions;
using NzbDrone.Core.MediaFiles;
namespace NzbDrone.Api.FileSystem
{
public class FileSystemModule : NzbDroneApiModule
{
private readonly IFileSystemLookupService _fileSystemLookupService;
2015-03-04 00:42:37 +00:00
private readonly IDiskProvider _diskProvider;
private readonly IDiskScanService _diskScanService;
2015-03-04 00:42:37 +00:00
public FileSystemModule(IFileSystemLookupService fileSystemLookupService,
IDiskProvider diskProvider,
IDiskScanService diskScanService)
: base("/filesystem")
{
_fileSystemLookupService = fileSystemLookupService;
2015-03-04 00:42:37 +00:00
_diskProvider = diskProvider;
_diskScanService = diskScanService;
Get["/"] = x => GetContents();
2015-03-04 00:42:37 +00:00
Get["/type"] = x => GetEntityType();
Get["/mediafiles"] = x => GetMediaFiles();
}
private Response GetContents()
{
var pathQuery = Request.Query.path;
2018-11-23 07:03:32 +00:00
var includeFiles = Request.GetBooleanQueryParameter("includeFiles");
var allowFoldersWithoutTrailingSlashes = Request.GetBooleanQueryParameter("allowFoldersWithoutTrailingSlashes");
2018-11-23 07:03:32 +00:00
return _fileSystemLookupService.LookupContents((string)pathQuery.Value, includeFiles, allowFoldersWithoutTrailingSlashes).AsResponse();
}
2015-03-04 00:42:37 +00:00
private Response GetEntityType()
{
var pathQuery = Request.Query.path;
var path = (string)pathQuery.Value;
if (_diskProvider.FileExists(path))
{
return new { type = "file" }.AsResponse();
}
//Return folder even if it doesn't exist on disk to avoid leaking anything from the UI about the underlying system
return new { type = "folder" }.AsResponse();
}
private Response GetMediaFiles()
{
var pathQuery = Request.Query.path;
var path = (string)pathQuery.Value;
if (!_diskProvider.FolderExists(path))
{
return new string[0].AsResponse();
}
return _diskScanService.GetVideoFiles(path).Select(f => new {
Path = f,
RelativePath = path.GetRelativePath(f),
Name = Path.GetFileName(f)
}).AsResponse();
}
}
2018-11-23 07:03:32 +00:00
}