1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-14 11:23:42 +02:00
Sonarr/NzbDrone.Web/Controllers/AddSeriesController.cs

210 lines
6.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using NLog;
using NzbDrone.Core.Providers;
2011-04-04 06:50:12 +03:00
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Providers.Jobs;
using NzbDrone.Core.Repository;
using NzbDrone.Web.Models;
namespace NzbDrone.Web.Controllers
{
public class AddSeriesController : Controller
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly ConfigProvider _configProvider;
private readonly QualityProvider _qualityProvider;
2011-04-10 05:44:01 +03:00
private readonly RootDirProvider _rootFolderProvider;
2011-04-09 02:55:23 +03:00
private readonly SeriesProvider _seriesProvider;
private readonly JobProvider _jobProvider;
2011-04-10 05:44:01 +03:00
private readonly TvDbProvider _tvDbProvider;
private readonly DiskProvider _diskProvider;
public AddSeriesController(RootDirProvider rootFolderProvider,
2011-04-10 05:44:01 +03:00
ConfigProvider configProvider,
QualityProvider qualityProvider, TvDbProvider tvDbProvider,
SeriesProvider seriesProvider, JobProvider jobProvider,
DiskProvider diskProvider)
{
_rootFolderProvider = rootFolderProvider;
_configProvider = configProvider;
_qualityProvider = qualityProvider;
_tvDbProvider = tvDbProvider;
_seriesProvider = seriesProvider;
_jobProvider = jobProvider;
_diskProvider = diskProvider;
}
[HttpPost]
public JsonResult ScanNewSeries()
{
_jobProvider.QueueJob(typeof(ImportNewSeriesJob));
return new JsonResult();
}
public ActionResult AddNew()
{
2011-07-08 06:27:11 +03:00
ViewData["RootDirs"] = _rootFolderProvider.GetAll().Select(c => c.Path).OrderBy(e => e).ToList();
var defaultQuality = _configProvider.DefaultQualityProfile;
2011-07-08 08:41:08 +03:00
var qualityProfiles = _qualityProvider.All();
2011-07-08 06:27:11 +03:00
ViewData["qualityList"] = qualityProfiles;
ViewData["quality"] = new SelectList(
qualityProfiles,
"QualityProfileId",
"Name",
defaultQuality);
return View();
}
public ActionResult Index()
{
2011-07-28 01:59:48 +03:00
return View();
}
2011-07-28 01:59:48 +03:00
public ActionResult ExistingSeries()
{
2011-07-28 01:59:48 +03:00
var result = new ExistingSeriesModel();
var unmappedList = new List<String>();
2011-07-28 01:59:48 +03:00
foreach (var folder in _rootFolderProvider.GetAll())
{
unmappedList.AddRange(_rootFolderProvider.GetUnmappedFolders(folder.Path));
}
result.ExistingSeries = new List<Tuple<string, string, int>>();
2011-07-28 01:59:48 +03:00
foreach (var folder in unmappedList)
{
var foldername = new DirectoryInfo(folder).Name;
var tvdbResult = _tvDbProvider.SearchSeries(foldername).FirstOrDefault();
2011-07-28 01:59:48 +03:00
var title = String.Empty;
var seriesId = 0;
2011-07-28 01:59:48 +03:00
if (tvdbResult != null)
{
title = tvdbResult.SeriesName;
seriesId = tvdbResult.Id;
2011-07-28 01:59:48 +03:00
}
result.ExistingSeries.Add(new Tuple<string, string, int>(folder, title, seriesId));
2011-07-28 01:59:48 +03:00
}
2011-07-28 01:59:48 +03:00
var defaultQuality = Convert.ToInt32(_configProvider.DefaultQualityProfile);
result.Quality = new SelectList(_qualityProvider.All(), "QualityProfileId", "Name", defaultQuality);
2011-07-28 01:59:48 +03:00
return View(result);
}
[HttpPost]
public JsonResult AddNewSeries(string path, string seriesName, int seriesId, int qualityProfileId)
2011-07-28 01:59:48 +03:00
{
path = Path.Combine(path, MediaFileProvider.CleanFilename(seriesName));
return AddExistingSeries(path, seriesName, seriesId, qualityProfileId);
}
[HttpPost]
public JsonResult AddExistingSeries(string path, string seriesName, int seriesId, int qualityProfileId)
{
try
{
//Create the folder for the new series and then Add it
_diskProvider.CreateDirectory(path);
_seriesProvider.AddSeries(path, seriesId, qualityProfileId);
ScanNewSeries();
2011-08-22 04:00:12 +03:00
return Json(new NotificationResult() { Title = seriesName, Text = "Was added successfully" });
}
2011-08-22 04:00:12 +03:00
catch (Exception ex)
{
2011-08-22 04:00:12 +03:00
return Json(new NotificationResult() { Title = "Failed", Text = ex.Message, NotificationType = NotificationType.Error});
}
}
public JsonResult AddSeries(string path, int seriesId, int qualityProfileId)
{
//Get TVDB Series Name
//Create new folder for series
//Add the new series to the Database
2011-04-10 05:44:01 +03:00
_seriesProvider.AddSeries(
path.Replace('|', Path.DirectorySeparatorChar).Replace('^', Path.VolumeSeparatorChar).Replace('`', '\''), seriesId,
2011-04-10 05:44:01 +03:00
qualityProfileId);
ScanNewSeries();
return new JsonResult { Data = "ok" };
}
//Root Directory
[HttpPost]
2011-06-30 09:15:06 +03:00
public JsonResult SaveRootDir(string path)
{
if (String.IsNullOrWhiteSpace(path))
return new JsonResult { Data = "failed" };
try
{
2011-06-30 09:15:06 +03:00
_rootFolderProvider.Add(new RootDir { Path = path });
}
catch (Exception ex)
{
Logger.Debug("Failed to save Root Dir");
Logger.DebugException(ex.Message, ex);
return new JsonResult { Data = "failed" };
}
return new JsonResult { Data = "ok" };
}
2011-07-28 01:59:48 +03:00
[HttpGet]
public JsonResult LookupSeries(string term)
{
var tvDbResults = _tvDbProvider.SearchSeries(term).Select(r => new TvDbSearchResultModel
{
Id = r.Id,
Title = r.SeriesName,
FirstAired = r.FirstAired.ToShortDateString()
}).ToList();
return Json(tvDbResults, JsonRequestBehavior.AllowGet);
}
2011-07-01 10:23:07 +03:00
public ActionResult RootList()
2011-06-30 09:15:06 +03:00
{
2011-07-01 10:52:44 +03:00
IEnumerable<String> rootDir = _rootFolderProvider.GetAll().Select(c => c.Path).OrderBy(e => e);
2011-07-01 10:23:07 +03:00
return PartialView("RootList", rootDir);
}
public ActionResult RootDir()
{
return PartialView("RootDir");
2011-06-30 09:15:06 +03:00
}
public JsonResult DeleteRootDir(string path)
{
try
{
2011-06-30 09:15:06 +03:00
var id = _rootFolderProvider.GetAll().Where(c => c.Path == path).First().Id;
_rootFolderProvider.Remove(id);
}
catch (Exception)
{
return new JsonResult { Data = "failed" };
}
return new JsonResult { Data = "ok" };
}
}
2011-04-10 05:44:01 +03:00
}