2017-09-04 02:20:56 +00:00
|
|
|
using System.Linq;
|
2020-01-03 12:49:24 +00:00
|
|
|
using Lidarr.Http.Extensions;
|
2017-09-04 02:20:56 +00:00
|
|
|
using Nancy;
|
|
|
|
using NzbDrone.Common.Disk;
|
|
|
|
using NzbDrone.Common.Extensions;
|
|
|
|
using NzbDrone.Core.MediaFiles;
|
|
|
|
|
2017-10-31 01:28:29 +00:00
|
|
|
namespace Lidarr.Api.V1.FileSystem
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
2017-10-31 01:28:29 +00:00
|
|
|
public class FileSystemModule : LidarrV1Module
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
|
|
|
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;
|
2020-01-03 12:49:24 +00:00
|
|
|
Get("/", x => GetContents());
|
|
|
|
Get("/type", x => GetEntityType());
|
|
|
|
Get("/mediafiles", x => GetMediaFiles());
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 20:32:51 +00:00
|
|
|
private object GetContents()
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
|
|
|
var pathQuery = Request.Query.path;
|
|
|
|
var includeFiles = Request.GetBooleanQueryParameter("includeFiles");
|
2018-08-11 00:46:34 +00:00
|
|
|
var allowFoldersWithoutTrailingSlashes = Request.GetBooleanQueryParameter("allowFoldersWithoutTrailingSlashes");
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2019-09-12 20:32:51 +00:00
|
|
|
return _fileSystemLookupService.LookupContents((string)pathQuery.Value, includeFiles, allowFoldersWithoutTrailingSlashes);
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 20:32:51 +00:00
|
|
|
private object GetEntityType()
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
|
|
|
var pathQuery = Request.Query.path;
|
|
|
|
var path = (string)pathQuery.Value;
|
|
|
|
|
|
|
|
if (_diskProvider.FileExists(path))
|
|
|
|
{
|
2019-09-12 20:32:51 +00:00
|
|
|
return new { type = "file" };
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Return folder even if it doesn't exist on disk to avoid leaking anything from the UI about the underlying system
|
2019-09-12 20:32:51 +00:00
|
|
|
return new { type = "folder" };
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 20:32:51 +00:00
|
|
|
private object GetMediaFiles()
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
|
|
|
var pathQuery = Request.Query.path;
|
|
|
|
var path = (string)pathQuery.Value;
|
|
|
|
|
|
|
|
if (!_diskProvider.FolderExists(path))
|
|
|
|
{
|
2019-09-12 20:32:51 +00:00
|
|
|
return new string[0];
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
2020-01-03 12:49:24 +00:00
|
|
|
return _diskScanService.GetAudioFiles(path).Select(f => new
|
|
|
|
{
|
2019-06-08 19:13:58 +00:00
|
|
|
Path = f.FullName,
|
|
|
|
RelativePath = path.GetRelativePath(f.FullName),
|
|
|
|
Name = f.Name
|
2019-09-12 20:32:51 +00:00
|
|
|
});
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|