1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-01 16:32:56 +01:00
Sonarr/NzbDrone.Web/Controllers/AddSeriesController.cs

211 lines
7.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
2011-11-13 05:07:06 +01:00
using NzbDrone.Common;
2011-12-02 02:33:17 +01:00
using NzbDrone.Core.Jobs;
using NzbDrone.Core.Providers;
2011-04-04 05:50:12 +02:00
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository;
using NzbDrone.Web.Filters;
using NzbDrone.Web.Models;
namespace NzbDrone.Web.Controllers
{
public class AddSeriesController : Controller
{
private readonly ConfigProvider _configProvider;
private readonly QualityProvider _qualityProvider;
2011-04-10 04:44:01 +02:00
private readonly RootDirProvider _rootFolderProvider;
2011-04-09 01:55:23 +02:00
private readonly SeriesProvider _seriesProvider;
private readonly JobProvider _jobProvider;
2011-04-10 04:44:01 +02:00
private readonly TvDbProvider _tvDbProvider;
private readonly DiskProvider _diskProvider;
public AddSeriesController(RootDirProvider rootFolderProvider,
2011-04-10 04:44:01 +02: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 EmptyResult ScanNewSeries()
{
_jobProvider.QueueJob(typeof(ImportNewSeriesJob));
return new EmptyResult();
}
public ActionResult AddNew()
{
2011-07-08 05:27:11 +02:00
ViewData["RootDirs"] = _rootFolderProvider.GetAll().Select(c => c.Path).OrderBy(e => e).ToList();
var defaultQuality = _configProvider.DefaultQualityProfile;
2011-07-08 07:41:08 +02:00
var qualityProfiles = _qualityProvider.All();
ViewData["qualityProfiles"] = new SelectList(
qualityProfiles,
"QualityProfileId",
"Name",
defaultQuality);
return View();
}
public ActionResult Index()
{
2011-07-28 00:59:48 +02:00
return View();
}
2011-07-28 00:59:48 +02:00
public ActionResult ExistingSeries()
{
2011-07-28 00:59:48 +02:00
var result = new ExistingSeriesModel();
var unmappedList = new List<String>();
2011-07-28 00:59:48 +02:00
foreach (var folder in _rootFolderProvider.GetAll())
{
unmappedList.AddRange(_rootFolderProvider.GetUnmappedFolders(folder.Path));
}
result.ExistingSeries = new List<Tuple<string, string, int>>();
2011-07-28 00:59:48 +02:00
foreach (var folder in unmappedList)
{
var foldername = new DirectoryInfo(folder).Name;
var tvdbResult = _tvDbProvider.SearchSeries(foldername).FirstOrDefault();
2011-07-28 00:59:48 +02:00
var title = String.Empty;
var seriesId = 0;
2011-07-28 00:59:48 +02:00
if (tvdbResult != null)
{
title = tvdbResult.SeriesName;
seriesId = tvdbResult.Id;
2011-07-28 00:59:48 +02:00
}
result.ExistingSeries.Add(new Tuple<string, string, int>(folder, title, seriesId));
2011-07-28 00:59:48 +02:00
}
2011-07-28 00:59:48 +02:00
var defaultQuality = Convert.ToInt32(_configProvider.DefaultQualityProfile);
result.Quality = new SelectList(_qualityProvider.All(), "QualityProfileId", "Name", defaultQuality);
2011-07-28 00:59:48 +02:00
return View(result);
}
[HttpPost]
public JsonResult AddNewSeries(string path, string seriesName, int seriesId, int qualityProfileId)
2011-07-28 00:59:48 +02:00
{
if (string.IsNullOrWhiteSpace(path) || String.Equals(path,"null",StringComparison.InvariantCultureIgnoreCase))
return JsonNotificationResult.Error("Couldn't add " + seriesName, "You need a valid root folder");
2011-07-28 00:59:48 +02:00
path = Path.Combine(path, MediaFileProvider.CleanFilename(seriesName));
//Create the folder for the new series
//Use the created folder name when adding the series
path = _diskProvider.CreateDirectory(path);
return AddExistingSeries(path, seriesName, seriesId, qualityProfileId);
}
[HttpPost]
[JsonErrorFilter]
public JsonResult AddExistingSeries(string path, string seriesName, int seriesId, int qualityProfileId)
{
if (seriesId == 0 || String.IsNullOrWhiteSpace(seriesName))
return JsonNotificationResult.Error("Add Existing series failed.", "Invalid Series information");
2012-02-29 09:25:41 +01:00
_seriesProvider.AddSeries(seriesName,path, seriesId, qualityProfileId);
ScanNewSeries();
return JsonNotificationResult.Info(seriesName, "Was added successfully");
}
[HttpPost]
public JsonResult QuickAddNewSeries(string seriesName, int seriesId, int qualityProfileId)
{
var path = _rootFolderProvider.GetMostFreeRootDir();
path = Path.Combine(path, MediaFileProvider.CleanFilename(seriesName));
//Create the folder for the new series
//Use the created folder name when adding the series
path = _diskProvider.CreateDirectory(path);
return AddExistingSeries(path, seriesName, seriesId, qualityProfileId);
}
[ChildActionOnly]
public ActionResult QuickAdd()
{
var defaultQuality = _configProvider.DefaultQualityProfile;
var qualityProfiles = _qualityProvider.All();
ViewData["qualityProfiles"] = new SelectList(
qualityProfiles,
"QualityProfileId",
"Name",
defaultQuality);
return PartialView();
}
[HttpPost]
[JsonErrorFilter]
2011-06-30 08:15:06 +02:00
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 foler saved successfully.");
}
2011-07-28 00:59:48 +02:00
[HttpGet]
public JsonResult LookupSeries(string term)
{
var tvDbResults = _tvDbProvider.SearchSeries(term).Select(r => new TvDbSearchResultModel
{
Id = r.Id,
Title = r.SeriesName,
DisplayedTitle = r.FirstAired.Year > 1900 && !r.SeriesName.EndsWith("(" + r.FirstAired.Year + ")")
?string.Format("{0} ({1})", r.SeriesName, r.FirstAired.Year)
:r.SeriesName,
Banner = r.Banner.BannerPath
}).ToList();
return Json(tvDbResults, JsonRequestBehavior.AllowGet);
}
2011-07-01 09:23:07 +02:00
public ActionResult RootList()
2011-06-30 08:15:06 +02:00
{
2011-07-01 09:52:44 +02:00
IEnumerable<String> rootDir = _rootFolderProvider.GetAll().Select(c => c.Path).OrderBy(e => e);
2011-07-01 09:23:07 +02:00
return PartialView("RootList", rootDir);
}
public ActionResult RootDir()
{
return PartialView("RootDir");
2011-06-30 08:15:06 +02:00
}
[JsonErrorFilter]
2011-06-30 08:15:06 +02:00
public JsonResult DeleteRootDir(string path)
{
var id = _rootFolderProvider.GetAll().Where(c => c.Path == path).First().Id;
_rootFolderProvider.Remove(id);
return null;
}
}
2011-04-10 04:44:01 +02:00
}