2011-08-06 02:04:35 +00:00
using System ;
using System.Collections.Generic ;
using System.Web ;
using System.Web.Mvc ;
2011-10-27 05:46:54 +00:00
using NLog ;
2011-08-26 17:45:59 +00:00
using NzbDrone.Core.Model ;
2011-10-28 07:57:00 +00:00
using NzbDrone.Core.Model.Twitter ;
2011-08-26 17:45:59 +00:00
using NzbDrone.Core.Providers ;
2011-08-06 02:04:35 +00:00
using NzbDrone.Core.Providers.Jobs ;
2011-08-26 17:45:59 +00:00
using NzbDrone.Web.Models ;
2011-08-06 02:04:35 +00:00
namespace NzbDrone.Web.Controllers
{
public class CommandController : Controller
{
private readonly JobProvider _jobProvider ;
2011-08-26 17:45:59 +00:00
private readonly SabProvider _sabProvider ;
2011-10-27 05:46:54 +00:00
private readonly SmtpProvider _smtpProvider ;
2011-10-28 07:57:00 +00:00
private readonly TwitterProvider _twitterProvider ;
2011-08-06 02:04:35 +00:00
2011-10-27 05:46:54 +00:00
private static readonly Logger Logger = LogManager . GetCurrentClassLogger ( ) ;
public CommandController ( JobProvider jobProvider , SabProvider sabProvider ,
2011-10-28 07:57:00 +00:00
SmtpProvider smtpProvider , TwitterProvider twitterProvider )
2011-08-06 02:04:35 +00:00
{
_jobProvider = jobProvider ;
2011-08-26 17:45:59 +00:00
_sabProvider = sabProvider ;
2011-10-27 05:46:54 +00:00
_smtpProvider = smtpProvider ;
2011-10-28 07:57:00 +00:00
_twitterProvider = twitterProvider ;
2011-08-06 02:04:35 +00:00
}
public JsonResult RssSync ( )
{
_jobProvider . QueueJob ( typeof ( RssSyncJob ) ) ;
2011-08-23 03:52:08 +00:00
return new JsonResult { Data = "ok" , JsonRequestBehavior = JsonRequestBehavior . AllowGet } ;
2011-08-06 02:04:35 +00:00
}
2011-10-21 23:33:54 +00:00
public JsonResult BacklogSearch ( )
{
_jobProvider . QueueJob ( typeof ( BacklogSearchJob ) ) ;
return new JsonResult { Data = "ok" , JsonRequestBehavior = JsonRequestBehavior . AllowGet } ;
}
2011-11-26 07:53:16 +00:00
public JsonResult ScanDisk ( int seriesId )
2011-08-06 02:04:35 +00:00
{
//Syncs the episodes on disk for the specified series
_jobProvider . QueueJob ( typeof ( DiskScanJob ) , seriesId ) ;
2011-08-23 03:52:08 +00:00
return new JsonResult { Data = "ok" , JsonRequestBehavior = JsonRequestBehavior . AllowGet } ;
2011-08-06 02:04:35 +00:00
}
public JsonResult UpdateInfo ( int seriesId )
{
//Syncs the episodes on disk for the specified series
_jobProvider . QueueJob ( typeof ( UpdateInfoJob ) , seriesId ) ;
2011-08-23 03:52:08 +00:00
return new JsonResult { Data = "ok" , JsonRequestBehavior = JsonRequestBehavior . AllowGet } ;
2011-08-06 02:04:35 +00:00
}
2011-08-26 17:45:59 +00:00
[HttpPost]
public JsonResult GetSabnzbdCategories ( string host , int port , string apiKey , string username , string password )
{
try
{
return new JsonResult { Data = _sabProvider . GetCategories ( host , port , apiKey , username , password ) } ;
}
catch ( Exception ex )
{
2011-10-27 05:46:54 +00:00
Logger . Warn ( "Unable to get Categories from SABnzbd" ) ;
Logger . DebugException ( ex . Message , ex ) ;
return Json ( new NotificationResult { Title = "Failed" , Text = "Unable to get SABnzbd Categories" , NotificationType = NotificationType . Error } ) ;
2011-08-26 17:45:59 +00:00
}
}
2011-10-27 05:46:54 +00:00
[HttpPost]
public JsonResult SendTestEmail ( string server , int port , bool ssl , string username , string password , string fromAddress , string toAddresses )
{
if ( _smtpProvider . SendTestEmail ( server , port , ssl , username , password , fromAddress , toAddresses ) )
return Json ( new NotificationResult { Title = "Successfully sent test email." } ) ;
return Json ( new NotificationResult { Title = "Failed" , Text = "Unable to send Email, please check your settings" , NotificationType = NotificationType . Error } ) ;
}
2011-10-28 07:57:00 +00:00
public JsonResult GetTwitterAuthorization ( )
{
var result = _twitterProvider . GetAuthorization ( ) ;
if ( result = = null )
return Json ( new NotificationResult { Title = "Failed" , Text = "Unable to get Twitter Authorization" , NotificationType = NotificationType . Error } , JsonRequestBehavior . AllowGet ) ;
return new JsonResult { Data = result , JsonRequestBehavior = JsonRequestBehavior . AllowGet } ;
}
public JsonResult VerifyTwitterAuthorization ( string token , string verifier )
{
var result = _twitterProvider . GetAndSaveAccessToken ( token , verifier ) ;
if ( ! result )
return Json ( new NotificationResult { Title = "Failed" , Text = "Unable to verify Twitter Authorization" , NotificationType = NotificationType . Error } , JsonRequestBehavior . AllowGet ) ;
return Json ( new NotificationResult { Title = "Successfully verified Twitter Authorization." } , JsonRequestBehavior . AllowGet ) ;
}
2011-08-06 02:04:35 +00:00
}
}