2011-12-19 00:24:29 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2011-11-18 06:52:50 +00:00
|
|
|
|
using System.Web.Mvc;
|
2011-11-13 04:07:06 +00:00
|
|
|
|
using NzbDrone.Common;
|
2012-10-01 00:05:16 +00:00
|
|
|
|
using NzbDrone.Web.Filters;
|
2011-06-09 01:45:06 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Web.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class DirectoryController : Controller
|
|
|
|
|
{
|
|
|
|
|
private readonly DiskProvider _diskProvider;
|
|
|
|
|
|
|
|
|
|
public DirectoryController(DiskProvider diskProvider)
|
|
|
|
|
{
|
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2012-01-19 05:01:47 +00:00
|
|
|
|
public JsonResult _autoCompletePath(string text, int? filterMode)
|
2011-06-09 01:45:06 +00:00
|
|
|
|
{
|
|
|
|
|
var data = GetDirectories(text);
|
|
|
|
|
|
|
|
|
|
return new JsonResult
|
|
|
|
|
{
|
|
|
|
|
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
|
|
|
|
|
Data = data
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-30 06:15:06 +00:00
|
|
|
|
[HttpGet]
|
2012-10-01 00:05:16 +00:00
|
|
|
|
[JsonErrorFilter]
|
2011-09-09 03:26:48 +00:00
|
|
|
|
public JsonResult GetDirectories(string term)
|
2011-06-09 01:45:06 +00:00
|
|
|
|
{
|
2011-11-18 06:52:50 +00:00
|
|
|
|
IEnumerable<string> dirs = null;
|
2011-06-10 21:55:58 +00:00
|
|
|
|
try
|
2011-06-09 01:45:06 +00:00
|
|
|
|
{
|
2011-06-10 21:55:58 +00:00
|
|
|
|
//Windows (Including UNC)
|
2011-09-09 03:26:48 +00:00
|
|
|
|
var windowsSep = term.LastIndexOf('\\');
|
2011-06-10 21:55:58 +00:00
|
|
|
|
|
|
|
|
|
if (windowsSep > -1)
|
|
|
|
|
{
|
2011-09-09 03:26:48 +00:00
|
|
|
|
dirs = _diskProvider.GetDirectories(term.Substring(0, windowsSep + 1));
|
2011-06-10 21:55:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Unix
|
2011-09-09 03:26:48 +00:00
|
|
|
|
var index = term.LastIndexOf('/');
|
2011-06-10 21:55:58 +00:00
|
|
|
|
|
|
|
|
|
if (index > -1)
|
|
|
|
|
{
|
2011-09-09 03:26:48 +00:00
|
|
|
|
dirs = _diskProvider.GetDirectories(term.Substring(0, index + 1));
|
2011-06-10 21:55:58 +00:00
|
|
|
|
}
|
2011-06-09 01:45:06 +00:00
|
|
|
|
}
|
2012-02-05 06:34:36 +00:00
|
|
|
|
catch (Exception)
|
2011-06-09 01:45:06 +00:00
|
|
|
|
{
|
2011-12-19 00:24:29 +00:00
|
|
|
|
return Json(new List<string>(), JsonRequestBehavior.AllowGet);
|
2011-06-10 21:55:58 +00:00
|
|
|
|
//Swallow the exceptions so proper JSON is returned to the client (Empty results)
|
2011-06-09 01:45:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-10-01 00:05:16 +00:00
|
|
|
|
if (dirs == null)
|
|
|
|
|
throw new Exception("A valid path was not provided");
|
|
|
|
|
|
2011-07-01 20:59:25 +00:00
|
|
|
|
return Json(dirs, JsonRequestBehavior.AllowGet);
|
2011-06-09 01:45:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|