1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-23 02:05:27 +02:00
Sonarr/NzbDrone.Web/Controllers/SystemController.cs

211 lines
7.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using System.Web.Script.Serialization;
2011-11-13 07:07:06 +03:00
using NzbDrone.Common;
using NzbDrone.Core;
using NzbDrone.Core.Helpers;
2011-12-02 04:33:17 +03:00
using NzbDrone.Core.Jobs;
2011-04-22 09:23:29 +03:00
using NzbDrone.Core.Providers;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Providers.DownloadClients;
using NzbDrone.Web.Models;
2011-04-22 09:23:29 +03:00
namespace NzbDrone.Web.Controllers
{
public class SystemController : Controller
{
private readonly JobProvider _jobProvider;
private readonly IndexerProvider _indexerProvider;
private readonly ConfigProvider _configProvider;
private readonly DiskProvider _diskProvider;
2012-01-27 08:05:09 +03:00
private readonly BackupProvider _backupProvider;
private readonly StatsProvider _statsProvider;
2011-04-22 09:23:29 +03:00
public SystemController(JobProvider jobProvider, IndexerProvider indexerProvider,
ConfigProvider configProvider, DiskProvider diskProvider,
BackupProvider backupProvider, StatsProvider statsProvider)
2011-04-22 09:23:29 +03:00
{
_jobProvider = jobProvider;
_indexerProvider = indexerProvider;
_configProvider = configProvider;
_diskProvider = diskProvider;
2012-01-27 08:05:09 +03:00
_backupProvider = backupProvider;
_statsProvider = statsProvider;
2011-04-22 09:23:29 +03:00
}
public ActionResult Index()
{
return View();
}
public ActionResult Logs()
{
return View();
}
2011-04-22 09:23:29 +03:00
public ActionResult Jobs()
{
2012-02-10 09:17:53 +03:00
var queue = _jobProvider.Queue.Select(c => new JobQueueItemModel
{
Name = c.JobType.Name,
2012-09-10 22:04:17 +03:00
Options = c.Options
});
2012-02-10 09:17:53 +03:00
var serializedQueue = new JavaScriptSerializer().Serialize(queue);
ViewData["Queue"] = serializedQueue;
var jobs = _jobProvider.All().Select(j => new JobModel
{
Id = j.Id,
Enable = j.Enable,
TypeName = j.TypeName,
Name = j.Name,
Interval = j.Interval,
LastExecution = j.LastExecution.ToString(),
Success = j.Success
2012-02-10 09:17:53 +03:00
}).OrderBy(j => j.Interval);
var serializedJobs = new JavaScriptSerializer().Serialize(jobs);
return View((object)serializedJobs);
2011-04-22 09:23:29 +03:00
}
public ActionResult Indexers()
{
2012-02-10 09:26:48 +03:00
var indexers = _indexerProvider.All();
var serialized = new JavaScriptSerializer().Serialize(indexers);
return View((object)serialized);
2011-04-22 09:23:29 +03:00
}
public ActionResult Config()
{
var config = _configProvider.All();
var serialized = new JavaScriptSerializer().Serialize(config);
return View((object)serialized);
}
//PostDownloadView
public ActionResult PendingProcessing()
{
ViewData["DropDir"] = _configProvider.DownloadClientTvDirectory;
var dropDir = _configProvider.DownloadClientTvDirectory;
var subFolders = _diskProvider.GetDirectories(dropDir);
var models = new List<PendingProcessingModel>();
//Get the CreationTime and Files
foreach (var folder in subFolders)
{
var model = new PendingProcessingModel();
model.Name = new DirectoryInfo(folder).Name;
model.Created = _diskProvider.DirectoryDateCreated(folder).ToString();
model.Path = folder.Replace(Path.DirectorySeparatorChar, '|').Replace(Path.VolumeSeparatorChar, '^').Replace('\'', '`');
var files = _diskProvider.GetFileInfos(folder, "*.*", SearchOption.AllDirectories);
var fileResult = "<div><div style=\"width: 600px; display: inline-block;\"><b>Name</b></div><div style=\"display: inline-block;\"><b>Size</b></div></div>";
foreach (var fileInfo in files)
{
fileResult += String.Format("<div><div style=\"width: 600px; display: inline-block;\">{0}</div><div style=\"display: inline-block;\">{1}</div></div>", fileInfo.Name,
fileInfo.Length.ToBestFileSize(1));
}
model.Files = fileResult;
models.Add(model);
}
var serialized = new JavaScriptSerializer().Serialize(models);
return View((object)serialized);
}
public JsonResult RenamePendingProcessing(string path)
{
path = path.Replace('|', Path.DirectorySeparatorChar).Replace('^', Path.VolumeSeparatorChar).Replace('`', '\'');
var di = new DirectoryInfo(path);
var dropDir = di.Parent.FullName;
var folder = di.Name;
if (!folder.StartsWith("_UNPACK_") && !folder.StartsWith("_FAILED_"))
return new JsonResult { Data = "no change" };
folder = folder.Substring(8);
var newPath = dropDir + Path.DirectorySeparatorChar + folder;
_diskProvider.MoveDirectory(path, newPath);
return new JsonResult { Data = "ok" };
}
public JsonResult RunJob(string typeName)
{
if (!_jobProvider.QueueJob(typeName))
return JsonNotificationResult.Oops("Invalid Job Name");
return JsonNotificationResult.Info("Job Queued");
}
public ActionResult Backup()
{
2012-01-27 08:05:09 +03:00
var file = _backupProvider.CreateBackupZip();
var fileInfo = new FileInfo(file);
return File(fileInfo.FullName, "application/binary", fileInfo.Name);
}
public ActionResult Stats()
{
var model = _statsProvider.GetStats();
return View(model);
}
public JsonResult Restart()
{
_jobProvider.QueueJob(typeof(AppRestartJob));
return JsonNotificationResult.Info("NzbDrone will restart shortly");
}
public JsonResult Shutdown()
{
_jobProvider.QueueJob(typeof(AppShutdownJob));
return JsonNotificationResult.Info("NzbDrone will shutdown shortly");
}
public JsonResult SelectConfigAjax()
{
var config = _configProvider.All();
return Json(new
{
iTotalRecords = config.Count(),
iTotalDisplayRecords = config.Count(),
aaData = config
}, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public string SaveConfigAjax(string id, string value)
{
_configProvider.SetValue(id, value);
return value;
}
[HttpPost]
public string InsertConfigAjax(string key, string value)
{
_configProvider.SetValue(key, value);
return key;
}
2011-04-22 09:23:29 +03:00
}
}