Lidarr/src/Lidarr.Api.V1/FileSystem/FileSystemModule.cs

71 lines
2.4 KiB
C#
Raw Normal View History

2017-09-04 02:20:56 +00:00
using System.Linq;
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;
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");
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
}
return _diskScanService.GetAudioFiles(path).Select(f => new
{
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
}
}
}