2013-02-06 07:28:56 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Nancy;
|
2013-02-23 21:35:26 +01:00
|
|
|
|
using NzbDrone.Api.Extensions;
|
2013-02-06 07:28:56 +01:00
|
|
|
|
using NzbDrone.Common;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Api.Directories
|
|
|
|
|
{
|
|
|
|
|
public class DirectoryModule : NzbDroneApiModule
|
|
|
|
|
{
|
2013-05-11 01:53:50 +02:00
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
2013-02-06 07:28:56 +01:00
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
public DirectoryModule(IDiskProvider diskProvider)
|
2013-02-06 07:28:56 +01:00
|
|
|
|
: base("/directories")
|
|
|
|
|
{
|
|
|
|
|
_diskProvider = diskProvider;
|
2013-02-16 00:38:53 +01:00
|
|
|
|
Get["/"] = x => GetDirectories();
|
2013-02-06 07:28:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Response GetDirectories()
|
|
|
|
|
{
|
2013-02-16 00:38:53 +01:00
|
|
|
|
if (!Request.Query.query.HasValue)
|
2013-02-06 07:28:56 +01:00
|
|
|
|
return new List<string>().AsResponse();
|
|
|
|
|
|
2013-02-16 00:38:53 +01:00
|
|
|
|
string query = Request.Query.query.Value;
|
2013-02-06 07:28:56 +01:00
|
|
|
|
|
|
|
|
|
IEnumerable<String> dirs = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//Windows (Including UNC)
|
|
|
|
|
var windowsSep = query.LastIndexOf('\\');
|
|
|
|
|
|
|
|
|
|
if (windowsSep > -1)
|
|
|
|
|
{
|
|
|
|
|
dirs = _diskProvider.GetDirectories(query.Substring(0, windowsSep + 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Unix
|
|
|
|
|
var index = query.LastIndexOf('/');
|
|
|
|
|
|
|
|
|
|
if (index > -1)
|
|
|
|
|
{
|
|
|
|
|
dirs = _diskProvider.GetDirectories(query.Substring(0, index + 1));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
//Swallow the exceptions so proper JSON is returned to the client (Empty results)
|
|
|
|
|
return new List<string>().AsResponse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dirs == null)
|
|
|
|
|
throw new Exception("A valid path was not provided");
|
|
|
|
|
|
|
|
|
|
return dirs.AsResponse();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|