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

246 lines
8.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Web.Mvc;
using System.Linq;
using NLog;
using NzbDrone.Core.Helpers;
using NzbDrone.Core.Providers;
2011-04-04 05:50:12 +02: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 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 SyncProvider _syncProvider;
private readonly TvDbProvider _tvDbProvider;
private readonly DiskProvider _diskProvider;
2011-04-10 04:44:01 +02:00
public AddSeriesController(SyncProvider syncProvider, RootDirProvider rootFolderProvider,
ConfigProvider configProvider,
QualityProvider qualityProvider, TvDbProvider tvDbProvider,
SeriesProvider seriesProvider, JobProvider jobProvider,
DiskProvider diskProvider)
{
_syncProvider = syncProvider;
_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()
{
var rootDirs =_rootFolderProvider.GetAll().Select(r =>
new RootDirModel
{
Path = r.Path,
CleanPath = r.Path.Replace(Path.DirectorySeparatorChar, '|').Replace(Path.VolumeSeparatorChar, '^').Replace('\'', '`')
}).ToList();
ViewData["RootDirs"] = rootDirs;
ViewData["DirSep"] = Path.DirectorySeparatorChar.ToString().Replace(Path.DirectorySeparatorChar, '|');
var defaultQuality = _configProvider.DefaultQualityProfile;
var qualityProfiles = _qualityProvider.GetAllProfiles();
ViewData["quality"] = new SelectList(
qualityProfiles,
"QualityProfileId",
"Name",
defaultQuality);
return View();
}
public ActionResult Index()
{
var rootDirs = _rootFolderProvider.GetAll();
var profiles = _qualityProvider.GetAllProfiles();
var defaultQuality = Convert.ToInt32(_configProvider.DefaultQualityProfile);
var selectList = new SelectList(profiles, "QualityProfileId", "Name", defaultQuality);
ViewData["qualities"] = selectList;
ViewData["ShowRootDirs"] = false;
//There are no RootDirs Show the RootDirs Panel
if (rootDirs.Count == 0)
ViewData["ShowRootDirs"] = true;
return View(rootDirs);
}
public ActionResult AddExisting()
{
var rootDirs = _rootFolderProvider.GetAll();
var unmappedList = new List<String>();
foreach (var folder in rootDirs)
{
unmappedList.AddRange(_rootFolderProvider.GetUnmappedFolders(folder.Path));
}
return View(unmappedList);
}
public ActionResult RenderPartial(string path)
{
var suggestions = GetSuggestionList(new DirectoryInfo(path).Name);
ViewData["guid"] = Guid.NewGuid();
ViewData["path"] = path;
ViewData["javaPath"] = path.Replace(Path.DirectorySeparatorChar, '|').Replace(Path.VolumeSeparatorChar, '^').Replace('\'', '`');
var defaultQuality = _configProvider.DefaultQualityProfile;
var qualityProfiles = _qualityProvider.GetAllProfiles();
ViewData["quality"] = new SelectList(
qualityProfiles,
"QualityProfileId",
"Name",
2011-04-10 04:44:01 +02:00
defaultQuality);
return PartialView("AddSeriesItem", suggestions);
}
[HttpPost]
public JsonResult AddNewSeries(string rootPath, string seriesName, int seriesId, int qualityProfileId)
{
try
{
var path =
rootPath.Replace('|', Path.DirectorySeparatorChar).Replace('^', Path.VolumeSeparatorChar).Replace(
'`', '\'') +
Path.DirectorySeparatorChar + EpisodeRenameHelper.CleanFilename(seriesName);
//Create the folder for the new series and then Add it
_diskProvider.CreateDirectory(path);
_seriesProvider.AddSeries(path, seriesId, qualityProfileId);
ScanNewSeries();
return new JsonResult {Data = "ok"};
}
catch(Exception ex)
{
return new JsonResult { Data = "failed" };
}
}
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 04:44:01 +02:00
_seriesProvider.AddSeries(
path.Replace('|', Path.DirectorySeparatorChar).Replace('^', Path.VolumeSeparatorChar).Replace('`', '\''), seriesId,
2011-04-10 04:44:01 +02:00
qualityProfileId);
ScanNewSeries();
return new JsonResult { Data = "ok" };
}
[HttpPost]
public ActionResult _textLookUp(string text, int? filterMode)
{
var suggestions = GetSuggestionList(text);
return new JsonResult
2011-04-10 04:44:01 +02:00
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = suggestions
};
}
public SelectList GetSuggestionList(string searchString)
{
var dataVal = _tvDbProvider.SearchSeries(searchString);
int selectId = 0;
if (dataVal.Count != 0)
{
selectId = dataVal[0].Id;
}
return new SelectList(dataVal, "Id", "SeriesName", selectId);
}
//Root Directory
[HttpPost]
public JsonResult SaveRootDir(int id, string path)
{
try
{
_rootFolderProvider.Update(new RootDir { Id = id, 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" };
}
public PartialViewResult AddRootDir()
{
var rootDir = new RootDir { Path = String.Empty };
var id = _rootFolderProvider.Add(rootDir);
rootDir.Id = id;
var model = new RootDirModel();
model.Id = rootDir.Id;
model.Path = rootDir.Path;
model.SelectList = new SelectList(new List<string> { rootDir.Path }, rootDir.Path);
return PartialView("RootDir", model);
}
public ActionResult GetRootDirView(RootDir rootDir)
{
var model = new RootDirModel();
model.Id = rootDir.Id;
model.Path = rootDir.Path;
model.SelectList = new SelectList(new List<string> { rootDir.Path }, rootDir.Path);
return PartialView("RootDir", model);
}
public JsonResult DeleteRootDir(int rootDirId)
{
try
{
_rootFolderProvider.Remove(rootDirId);
}
catch (Exception)
{
return new JsonResult { Data = "failed" };
}
return new JsonResult { Data = "ok" };
}
}
2011-04-10 04:44:01 +02:00
}