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