2011-04-01 06:36:34 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2012-09-19 06:06:09 +00:00
|
|
|
|
using System.Globalization;
|
2011-04-01 06:36:34 +00:00
|
|
|
|
using System.IO;
|
2011-05-28 07:16:16 +00:00
|
|
|
|
using System.Linq;
|
2011-09-04 03:05:44 +00:00
|
|
|
|
using System.Web.Mvc;
|
2012-08-30 00:57:23 +00:00
|
|
|
|
using NLog;
|
2011-11-13 04:07:06 +00:00
|
|
|
|
using NzbDrone.Common;
|
2011-12-02 01:33:17 +00:00
|
|
|
|
using NzbDrone.Core.Jobs;
|
2011-04-01 06:36:34 +00:00
|
|
|
|
using NzbDrone.Core.Providers;
|
2011-04-04 03:50:12 +00:00
|
|
|
|
using NzbDrone.Core.Providers.Core;
|
2011-05-31 06:13:46 +00:00
|
|
|
|
using NzbDrone.Core.Repository;
|
2012-01-19 03:58:32 +00:00
|
|
|
|
using NzbDrone.Web.Filters;
|
2011-04-01 06:36:34 +00:00
|
|
|
|
using NzbDrone.Web.Models;
|
2012-10-17 00:08:47 +00:00
|
|
|
|
using TvdbLib.Exceptions;
|
2011-04-01 06:36:34 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Web.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class AddSeriesController : Controller
|
|
|
|
|
{
|
2011-04-10 01:34:36 +00:00
|
|
|
|
private readonly ConfigProvider _configProvider;
|
2011-04-08 04:11:45 +00:00
|
|
|
|
private readonly QualityProvider _qualityProvider;
|
2011-04-10 02:44:01 +00:00
|
|
|
|
private readonly RootDirProvider _rootFolderProvider;
|
2011-04-08 23:55:23 +00:00
|
|
|
|
private readonly SeriesProvider _seriesProvider;
|
2011-04-20 07:44:13 +00:00
|
|
|
|
private readonly JobProvider _jobProvider;
|
2011-04-10 02:44:01 +00:00
|
|
|
|
private readonly TvDbProvider _tvDbProvider;
|
2011-05-28 07:16:16 +00:00
|
|
|
|
private readonly DiskProvider _diskProvider;
|
2011-04-01 06:36:34 +00:00
|
|
|
|
|
2012-08-30 00:57:23 +00:00
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2011-06-14 01:23:04 +00:00
|
|
|
|
public AddSeriesController(RootDirProvider rootFolderProvider,
|
2011-04-10 02:44:01 +00:00
|
|
|
|
ConfigProvider configProvider,
|
|
|
|
|
QualityProvider qualityProvider, TvDbProvider tvDbProvider,
|
2011-05-28 07:16:16 +00:00
|
|
|
|
SeriesProvider seriesProvider, JobProvider jobProvider,
|
|
|
|
|
DiskProvider diskProvider)
|
2011-04-01 06:36:34 +00:00
|
|
|
|
{
|
2011-06-14 01:23:04 +00:00
|
|
|
|
|
2011-04-01 06:36:34 +00:00
|
|
|
|
_rootFolderProvider = rootFolderProvider;
|
|
|
|
|
_configProvider = configProvider;
|
|
|
|
|
_qualityProvider = qualityProvider;
|
|
|
|
|
_tvDbProvider = tvDbProvider;
|
|
|
|
|
_seriesProvider = seriesProvider;
|
2011-04-20 07:44:13 +00:00
|
|
|
|
_jobProvider = jobProvider;
|
2011-05-28 07:16:16 +00:00
|
|
|
|
_diskProvider = diskProvider;
|
2011-04-01 06:36:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-19 16:18:03 +00:00
|
|
|
|
public ActionResult Index()
|
2011-04-01 06:36:34 +00:00
|
|
|
|
{
|
2012-09-19 16:18:03 +00:00
|
|
|
|
return View();
|
2011-04-01 06:36:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActionResult AddNew()
|
|
|
|
|
{
|
2011-07-08 03:27:11 +00:00
|
|
|
|
ViewData["RootDirs"] = _rootFolderProvider.GetAll().Select(c => c.Path).OrderBy(e => e).ToList();
|
2011-04-01 06:36:34 +00:00
|
|
|
|
|
2011-05-28 07:16:16 +00:00
|
|
|
|
var defaultQuality = _configProvider.DefaultQualityProfile;
|
2011-07-08 05:41:08 +00:00
|
|
|
|
var qualityProfiles = _qualityProvider.All();
|
2011-04-01 06:36:34 +00:00
|
|
|
|
|
2011-10-19 15:16:34 +00:00
|
|
|
|
ViewData["qualityProfiles"] = new SelectList(
|
2011-05-28 07:16:16 +00:00
|
|
|
|
qualityProfiles,
|
|
|
|
|
"QualityProfileId",
|
|
|
|
|
"Name",
|
|
|
|
|
defaultQuality);
|
2011-04-01 06:36:34 +00:00
|
|
|
|
|
2011-05-28 07:16:16 +00:00
|
|
|
|
return View();
|
2011-04-01 06:36:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-07-27 22:59:48 +00:00
|
|
|
|
public ActionResult ExistingSeries()
|
2011-05-31 20:50:19 +00:00
|
|
|
|
{
|
2011-07-27 22:59:48 +00:00
|
|
|
|
var result = new ExistingSeriesModel();
|
2011-05-31 20:50:19 +00:00
|
|
|
|
|
|
|
|
|
var unmappedList = new List<String>();
|
|
|
|
|
|
2011-07-27 22:59:48 +00:00
|
|
|
|
foreach (var folder in _rootFolderProvider.GetAll())
|
2011-04-01 06:36:34 +00:00
|
|
|
|
{
|
2011-05-20 03:47:07 +00:00
|
|
|
|
unmappedList.AddRange(_rootFolderProvider.GetUnmappedFolders(folder.Path));
|
2011-04-01 06:36:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-09-09 03:52:25 +00:00
|
|
|
|
result.ExistingSeries = new List<Tuple<string, string, int>>();
|
2011-04-01 06:36:34 +00:00
|
|
|
|
|
2011-07-27 22:59:48 +00:00
|
|
|
|
foreach (var folder in unmappedList)
|
|
|
|
|
{
|
|
|
|
|
var foldername = new DirectoryInfo(folder).Name;
|
2011-04-01 06:36:34 +00:00
|
|
|
|
|
2012-08-30 00:57:23 +00:00
|
|
|
|
try
|
2011-07-27 22:59:48 +00:00
|
|
|
|
{
|
2012-08-30 00:57:23 +00:00
|
|
|
|
var tvdbResult = _tvDbProvider.SearchSeries(foldername).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
var title = String.Empty;
|
|
|
|
|
var seriesId = 0;
|
|
|
|
|
if (tvdbResult != null)
|
|
|
|
|
{
|
|
|
|
|
title = tvdbResult.SeriesName;
|
2013-01-31 00:42:11 +00:00
|
|
|
|
seriesId = tvdbResult.id;
|
2012-08-30 00:57:23 +00:00
|
|
|
|
}
|
2011-04-04 01:31:27 +00:00
|
|
|
|
|
2012-08-30 00:57:23 +00:00
|
|
|
|
result.ExistingSeries.Add(new Tuple<string, string, int>(folder, title, seriesId));
|
|
|
|
|
}
|
2013-01-22 05:48:06 +00:00
|
|
|
|
catch (Exception ex)
|
2012-08-30 00:57:23 +00:00
|
|
|
|
{
|
|
|
|
|
logger.WarnException("Failed to connect to TheTVDB to search for: " + foldername, ex);
|
|
|
|
|
return View();
|
|
|
|
|
}
|
2011-07-27 22:59:48 +00:00
|
|
|
|
}
|
2011-04-05 02:48:46 +00:00
|
|
|
|
|
2011-07-27 22:59:48 +00:00
|
|
|
|
var defaultQuality = Convert.ToInt32(_configProvider.DefaultQualityProfile);
|
|
|
|
|
result.Quality = new SelectList(_qualityProvider.All(), "QualityProfileId", "Name", defaultQuality);
|
2011-04-04 01:31:27 +00:00
|
|
|
|
|
2011-07-27 22:59:48 +00:00
|
|
|
|
return View(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2012-10-01 00:05:16 +00:00
|
|
|
|
[JsonErrorFilter]
|
2012-09-20 15:37:40 +00:00
|
|
|
|
public JsonResult AddNewSeries(string path, string seriesName, int seriesId, int qualityProfileId, string startDate)
|
2011-07-27 22:59:48 +00:00
|
|
|
|
{
|
2013-01-22 05:48:06 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(path) || String.Equals(path, "null", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
return JsonNotificationResult.Error("Couldn't add " + seriesName, "You need a valid root folder");
|
2012-02-27 03:25:57 +00:00
|
|
|
|
|
2011-07-27 22:59:48 +00:00
|
|
|
|
path = Path.Combine(path, MediaFileProvider.CleanFilename(seriesName));
|
2011-09-09 23:28:21 +00:00
|
|
|
|
|
|
|
|
|
//Create the folder for the new series
|
|
|
|
|
//Use the created folder name when adding the series
|
|
|
|
|
path = _diskProvider.CreateDirectory(path);
|
|
|
|
|
|
2012-09-20 15:37:40 +00:00
|
|
|
|
return AddExistingSeries(path, seriesName, seriesId, qualityProfileId, startDate);
|
2011-04-01 06:36:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 07:16:16 +00:00
|
|
|
|
[HttpPost]
|
2012-01-19 05:01:47 +00:00
|
|
|
|
[JsonErrorFilter]
|
2012-09-20 15:37:40 +00:00
|
|
|
|
public JsonResult AddExistingSeries(string path, string seriesName, int seriesId, int qualityProfileId, string startDate)
|
2011-05-28 07:16:16 +00:00
|
|
|
|
{
|
2012-01-13 16:54:20 +00:00
|
|
|
|
if (seriesId == 0 || String.IsNullOrWhiteSpace(seriesName))
|
2012-01-19 05:01:47 +00:00
|
|
|
|
return JsonNotificationResult.Error("Add Existing series failed.", "Invalid Series information");
|
2012-01-13 16:54:20 +00:00
|
|
|
|
|
2012-09-19 06:06:09 +00:00
|
|
|
|
DateTime? date = null;
|
2013-01-22 05:48:06 +00:00
|
|
|
|
|
2012-09-20 15:37:40 +00:00
|
|
|
|
if (!String.IsNullOrWhiteSpace(startDate))
|
|
|
|
|
date = DateTime.Parse(startDate, null, DateTimeStyles.RoundtripKind);
|
2012-09-19 06:06:09 +00:00
|
|
|
|
|
2013-01-22 05:48:06 +00:00
|
|
|
|
_seriesProvider.AddSeries(seriesName, path, seriesId, qualityProfileId, date);
|
2012-09-19 16:18:03 +00:00
|
|
|
|
_jobProvider.QueueJob(typeof(ImportNewSeriesJob));
|
2011-05-31 06:13:46 +00:00
|
|
|
|
|
2012-01-19 05:01:47 +00:00
|
|
|
|
return JsonNotificationResult.Info(seriesName, "Was added successfully");
|
2011-05-28 07:16:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-07-27 22:59:48 +00:00
|
|
|
|
[HttpGet]
|
2012-10-01 00:05:16 +00:00
|
|
|
|
[JsonErrorFilter]
|
2011-09-09 03:26:48 +00:00
|
|
|
|
public JsonResult LookupSeries(string term)
|
2011-05-31 06:13:46 +00:00
|
|
|
|
{
|
2012-09-15 03:24:34 +00:00
|
|
|
|
|
2013-01-31 00:42:11 +00:00
|
|
|
|
return JsonNotificationResult.Info("Lookup Failed", "Unknown error while connecting to TheTVDB");
|
2013-01-22 05:48:06 +00:00
|
|
|
|
|
2011-05-31 06:13:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-07-01 07:23:07 +00:00
|
|
|
|
public ActionResult RootList()
|
2011-06-30 06:15:06 +00:00
|
|
|
|
{
|
2011-07-01 07:52:44 +00:00
|
|
|
|
IEnumerable<String> rootDir = _rootFolderProvider.GetAll().Select(c => c.Path).OrderBy(e => e);
|
2011-07-01 07:23:07 +00:00
|
|
|
|
return PartialView("RootList", rootDir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActionResult RootDir()
|
|
|
|
|
{
|
|
|
|
|
return PartialView("RootDir");
|
2011-06-30 06:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-19 16:18:03 +00:00
|
|
|
|
[HttpPost]
|
|
|
|
|
[JsonErrorFilter]
|
|
|
|
|
public JsonResult SaveRootDir(string path)
|
|
|
|
|
{
|
|
|
|
|
if (String.IsNullOrWhiteSpace(path))
|
|
|
|
|
JsonNotificationResult.Error("Can't add root folder", "Path can not be empty");
|
|
|
|
|
|
|
|
|
|
_rootFolderProvider.Add(new RootDir { Path = path });
|
|
|
|
|
|
|
|
|
|
return JsonNotificationResult.Info("Root Folder saved", "Root folder saved successfully.");
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-19 05:01:47 +00:00
|
|
|
|
[JsonErrorFilter]
|
2011-06-30 06:15:06 +00:00
|
|
|
|
public JsonResult DeleteRootDir(string path)
|
2011-05-31 06:13:46 +00:00
|
|
|
|
{
|
|
|
|
|
|
2012-01-19 05:01:47 +00:00
|
|
|
|
var id = _rootFolderProvider.GetAll().Where(c => c.Path == path).First().Id;
|
|
|
|
|
_rootFolderProvider.Remove(id);
|
2011-05-31 06:13:46 +00:00
|
|
|
|
|
2012-01-19 05:01:47 +00:00
|
|
|
|
return null;
|
2011-05-31 06:13:46 +00:00
|
|
|
|
}
|
2011-04-01 06:36:34 +00:00
|
|
|
|
}
|
2011-04-10 02:44:01 +00:00
|
|
|
|
}
|