aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api/HttpHandlers
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Api/HttpHandlers')
-rw-r--r--MediaBrowser.Api/HttpHandlers/AudioHandler.cs119
-rw-r--r--MediaBrowser.Api/HttpHandlers/BaseMediaHandler.cs255
-rw-r--r--MediaBrowser.Api/HttpHandlers/FavoriteStatusHandler.cs38
-rw-r--r--MediaBrowser.Api/HttpHandlers/GenreHandler.cs57
-rw-r--r--MediaBrowser.Api/HttpHandlers/GenresHandler.cs78
-rw-r--r--MediaBrowser.Api/HttpHandlers/ImageHandler.cs224
-rw-r--r--MediaBrowser.Api/HttpHandlers/ItemHandler.cs35
-rw-r--r--MediaBrowser.Api/HttpHandlers/ItemListHandler.cs84
-rw-r--r--MediaBrowser.Api/HttpHandlers/MovieSpecialFeaturesHandler.cs46
-rw-r--r--MediaBrowser.Api/HttpHandlers/PersonHandler.cs55
-rw-r--r--MediaBrowser.Api/HttpHandlers/PlayedStatusHandler.cs38
-rw-r--r--MediaBrowser.Api/HttpHandlers/PluginAssemblyHandler.cs38
-rw-r--r--MediaBrowser.Api/HttpHandlers/PluginConfigurationHandler.cs53
-rw-r--r--MediaBrowser.Api/HttpHandlers/PluginsHandler.cs38
-rw-r--r--MediaBrowser.Api/HttpHandlers/ServerConfigurationHandler.cs37
-rw-r--r--MediaBrowser.Api/HttpHandlers/StudioHandler.cs57
-rw-r--r--MediaBrowser.Api/HttpHandlers/StudiosHandler.cs78
-rw-r--r--MediaBrowser.Api/HttpHandlers/UserAuthenticationHandler.cs29
-rw-r--r--MediaBrowser.Api/HttpHandlers/UserHandler.cs29
-rw-r--r--MediaBrowser.Api/HttpHandlers/UserItemRatingHandler.cs46
-rw-r--r--MediaBrowser.Api/HttpHandlers/UsersHandler.cs25
-rw-r--r--MediaBrowser.Api/HttpHandlers/VideoHandler.cs424
-rw-r--r--MediaBrowser.Api/HttpHandlers/WeatherHandler.cs43
-rw-r--r--MediaBrowser.Api/HttpHandlers/YearHandler.cs55
-rw-r--r--MediaBrowser.Api/HttpHandlers/YearsHandler.cs75
25 files changed, 2056 insertions, 0 deletions
diff --git a/MediaBrowser.Api/HttpHandlers/AudioHandler.cs b/MediaBrowser.Api/HttpHandlers/AudioHandler.cs
new file mode 100644
index 000000000..9c16acd2e
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/AudioHandler.cs
@@ -0,0 +1,119 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Model.DTO;
+using System.Collections.Generic;
+using System.ComponentModel.Composition;
+using System.IO;
+using System.Net;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ /// <summary>
+ /// Supported output formats are: mp3,flac,ogg,wav,asf,wma,aac
+ /// </summary>
+ [Export(typeof(BaseHandler))]
+ public class AudioHandler : BaseMediaHandler<Audio, AudioOutputFormats>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("audio", request);
+ }
+
+ /// <summary>
+ /// We can output these formats directly, but we cannot encode to them.
+ /// </summary>
+ protected override IEnumerable<AudioOutputFormats> UnsupportedOutputEncodingFormats
+ {
+ get
+ {
+ return new AudioOutputFormats[] { AudioOutputFormats.Aac, AudioOutputFormats.Flac, AudioOutputFormats.Wma };
+ }
+ }
+
+ private int? GetMaxAcceptedBitRate(AudioOutputFormats audioFormat)
+ {
+ return GetMaxAcceptedBitRate(audioFormat.ToString());
+ }
+
+ private int? GetMaxAcceptedBitRate(string audioFormat)
+ {
+ if (audioFormat.Equals("mp3", System.StringComparison.OrdinalIgnoreCase))
+ {
+ return 320000;
+ }
+
+ return null;
+ }
+
+ /// <summary>
+ /// Determines whether or not the original file requires transcoding
+ /// </summary>
+ protected override bool RequiresConversion()
+ {
+ if (base.RequiresConversion())
+ {
+ return true;
+ }
+
+ string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
+
+ int? bitrate = GetMaxAcceptedBitRate(currentFormat);
+
+ // If the bitrate is greater than our desired bitrate, we need to transcode
+ if (bitrate.HasValue && bitrate.Value < LibraryItem.BitRate)
+ {
+ return true;
+ }
+
+ // If the number of channels is greater than our desired channels, we need to transcode
+ if (AudioChannels.HasValue && AudioChannels.Value < LibraryItem.Channels)
+ {
+ return true;
+ }
+
+ // If the sample rate is greater than our desired sample rate, we need to transcode
+ if (AudioSampleRate.HasValue && AudioSampleRate.Value < LibraryItem.SampleRate)
+ {
+ return true;
+ }
+
+ // Yay
+ return false;
+ }
+
+ /// <summary>
+ /// Creates arguments to pass to ffmpeg
+ /// </summary>
+ protected override string GetCommandLineArguments()
+ {
+ var audioTranscodeParams = new List<string>();
+
+ AudioOutputFormats outputFormat = GetConversionOutputFormat();
+
+ int? bitrate = GetMaxAcceptedBitRate(outputFormat);
+
+ if (bitrate.HasValue)
+ {
+ audioTranscodeParams.Add("-ab " + bitrate.Value);
+ }
+
+ int? channels = GetNumAudioChannelsParam(LibraryItem.Channels);
+
+ if (channels.HasValue)
+ {
+ audioTranscodeParams.Add("-ac " + channels.Value);
+ }
+
+ int? sampleRate = GetSampleRateParam(LibraryItem.SampleRate);
+
+ if (sampleRate.HasValue)
+ {
+ audioTranscodeParams.Add("-ar " + sampleRate.Value);
+ }
+
+ audioTranscodeParams.Add("-f " + outputFormat);
+
+ return "-i \"" + LibraryItem.Path + "\" -vn " + string.Join(" ", audioTranscodeParams.ToArray()) + " -";
+ }
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/BaseMediaHandler.cs b/MediaBrowser.Api/HttpHandlers/BaseMediaHandler.cs
new file mode 100644
index 000000000..96ef60681
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/BaseMediaHandler.cs
@@ -0,0 +1,255 @@
+using MediaBrowser.Common.Logging;
+using MediaBrowser.Common.Net;
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller;
+using MediaBrowser.Controller.Entities;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
+using System.Linq;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ public abstract class BaseMediaHandler<TBaseItemType, TOutputType> : BaseHandler
+ where TBaseItemType : BaseItem, new()
+ {
+ /// <summary>
+ /// Supported values: mp3,flac,ogg,wav,asf,wma,aac
+ /// </summary>
+ protected virtual IEnumerable<TOutputType> OutputFormats
+ {
+ get
+ {
+ return QueryString["outputformats"].Split(',').Select(o => (TOutputType)Enum.Parse(typeof(TOutputType), o, true));
+ }
+ }
+
+ /// <summary>
+ /// These formats can be outputted directly but cannot be encoded to
+ /// </summary>
+ protected virtual IEnumerable<TOutputType> UnsupportedOutputEncodingFormats
+ {
+ get
+ {
+ return new TOutputType[] { };
+ }
+ }
+
+ private TBaseItemType _libraryItem;
+ /// <summary>
+ /// Gets the library item that will be played, if any
+ /// </summary>
+ protected TBaseItemType LibraryItem
+ {
+ get
+ {
+ if (_libraryItem == null)
+ {
+ string id = QueryString["id"];
+
+ if (!string.IsNullOrEmpty(id))
+ {
+ _libraryItem = Kernel.Instance.GetItemById(Guid.Parse(id)) as TBaseItemType;
+ }
+ }
+
+ return _libraryItem;
+ }
+ }
+
+ public int? AudioChannels
+ {
+ get
+ {
+ string val = QueryString["audiochannels"];
+
+ if (string.IsNullOrEmpty(val))
+ {
+ return null;
+ }
+
+ return int.Parse(val);
+ }
+ }
+
+ public int? AudioSampleRate
+ {
+ get
+ {
+ string val = QueryString["audiosamplerate"];
+
+ if (string.IsNullOrEmpty(val))
+ {
+ return 44100;
+ }
+
+ return int.Parse(val);
+ }
+ }
+
+ protected override Task<ResponseInfo> GetResponseInfo()
+ {
+ ResponseInfo info = new ResponseInfo
+ {
+ ContentType = MimeTypes.GetMimeType("." + GetConversionOutputFormat()),
+ CompressResponse = false
+ };
+
+ return Task.FromResult<ResponseInfo>(info);
+ }
+
+ public override Task ProcessRequest(HttpListenerContext ctx)
+ {
+ HttpListenerContext = ctx;
+
+ if (!RequiresConversion())
+ {
+ return new StaticFileHandler { Path = LibraryItem.Path }.ProcessRequest(ctx);
+ }
+
+ return base.ProcessRequest(ctx);
+ }
+
+ protected abstract string GetCommandLineArguments();
+
+ /// <summary>
+ /// Gets the format we'll be converting to
+ /// </summary>
+ protected virtual TOutputType GetConversionOutputFormat()
+ {
+ return OutputFormats.First(f => !UnsupportedOutputEncodingFormats.Any(s => s.ToString().Equals(f.ToString(), StringComparison.OrdinalIgnoreCase)));
+ }
+
+ protected virtual bool RequiresConversion()
+ {
+ string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
+
+ if (OutputFormats.Any(f => currentFormat.EndsWith(f.ToString(), StringComparison.OrdinalIgnoreCase)))
+ {
+ // We can output these files directly, but we can't encode them
+ if (UnsupportedOutputEncodingFormats.Any(f => currentFormat.EndsWith(f.ToString(), StringComparison.OrdinalIgnoreCase)))
+ {
+ return false;
+ }
+ }
+ else
+ {
+ // If it's not in a format the consumer accepts, return true
+ return true;
+ }
+
+ return false;
+ }
+
+ private FileStream LogFileStream { get; set; }
+
+ protected async override Task WriteResponseToOutputStream(Stream stream)
+ {
+ var startInfo = new ProcessStartInfo{};
+
+ startInfo.CreateNoWindow = true;
+
+ startInfo.UseShellExecute = false;
+
+ // Must consume both or ffmpeg may hang due to deadlocks. See comments below.
+ startInfo.RedirectStandardOutput = true;
+ startInfo.RedirectStandardError = true;
+
+ startInfo.FileName = Kernel.Instance.ApplicationPaths.FFMpegPath;
+ startInfo.WorkingDirectory = Kernel.Instance.ApplicationPaths.FFMpegDirectory;
+ startInfo.Arguments = GetCommandLineArguments();
+
+ Logger.LogInfo(startInfo.FileName + " " + startInfo.Arguments);
+
+ var process = new Process{};
+ process.StartInfo = startInfo;
+
+ // FFMpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory.
+ LogFileStream = new FileStream(Path.Combine(Kernel.Instance.ApplicationPaths.LogDirectoryPath, "ffmpeg-" + Guid.NewGuid().ToString() + ".txt"), FileMode.Create);
+
+ process.EnableRaisingEvents = true;
+
+ process.Exited += ProcessExited;
+
+ try
+ {
+ process.Start();
+
+ // MUST read both stdout and stderr asynchronously or a deadlock may occurr
+
+ // Kick off two tasks
+ Task mediaTask = process.StandardOutput.BaseStream.CopyToAsync(stream);
+ Task debugLogTask = process.StandardError.BaseStream.CopyToAsync(LogFileStream);
+
+ await mediaTask.ConfigureAwait(false);
+ //await debugLogTask.ConfigureAwait(false);
+ }
+ catch (Exception ex)
+ {
+ Logger.LogException(ex);
+
+ // Hate having to do this
+ try
+ {
+ process.Kill();
+ }
+ catch
+ {
+ }
+ }
+ }
+
+ void ProcessExited(object sender, EventArgs e)
+ {
+ if (LogFileStream != null)
+ {
+ LogFileStream.Dispose();
+ }
+
+ var process = sender as Process;
+
+ Logger.LogInfo("FFMpeg exited with code " + process.ExitCode);
+
+ process.Dispose();
+ }
+
+ /// <summary>
+ /// Gets the number of audio channels to specify on the command line
+ /// </summary>
+ protected int? GetNumAudioChannelsParam(int libraryItemChannels)
+ {
+ // If the user requested a max number of channels
+ if (AudioChannels.HasValue)
+ {
+ // Only specify the param if we're going to downmix
+ if (AudioChannels.Value < libraryItemChannels)
+ {
+ return AudioChannels.Value;
+ }
+ }
+
+ return null;
+ }
+
+ /// <summary>
+ /// Gets the number of audio channels to specify on the command line
+ /// </summary>
+ protected int? GetSampleRateParam(int libraryItemSampleRate)
+ {
+ // If the user requested a max value
+ if (AudioSampleRate.HasValue)
+ {
+ // Only specify the param if we're going to downmix
+ if (AudioSampleRate.Value < libraryItemSampleRate)
+ {
+ return AudioSampleRate.Value;
+ }
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/FavoriteStatusHandler.cs b/MediaBrowser.Api/HttpHandlers/FavoriteStatusHandler.cs
new file mode 100644
index 000000000..19c175d8b
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/FavoriteStatusHandler.cs
@@ -0,0 +1,38 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Model.DTO;
+using System.ComponentModel.Composition;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ /// <summary>
+ /// Provides a handler to set user favorite status for an item
+ /// </summary>
+ [Export(typeof(BaseHandler))]
+ public class FavoriteStatusHandler : BaseSerializationHandler<DtoUserItemData>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("FavoriteStatus", request);
+ }
+
+ protected override Task<DtoUserItemData> GetObjectToSerialize()
+ {
+ // Get the item
+ BaseItem item = ApiService.GetItemById(QueryString["id"]);
+
+ // Get the user
+ User user = ApiService.GetUserById(QueryString["userid"], true);
+
+ // Get the user data for this item
+ UserItemData data = item.GetUserData(user, true);
+
+ // Set favorite status
+ data.IsFavorite = QueryString["isfavorite"] == "1";
+
+ return Task.FromResult(ApiService.GetDtoUserItemData(data));
+ }
+ }
+} \ No newline at end of file
diff --git a/MediaBrowser.Api/HttpHandlers/GenreHandler.cs b/MediaBrowser.Api/HttpHandlers/GenreHandler.cs
new file mode 100644
index 000000000..7cca2aea7
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/GenreHandler.cs
@@ -0,0 +1,57 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Model.DTO;
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.Composition;
+using System.Linq;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ /// <summary>
+ /// Gets a single genre
+ /// </summary>
+ [Export(typeof(BaseHandler))]
+ public class GenreHandler : BaseSerializationHandler<IbnItem>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("genre", request);
+ }
+
+ protected override Task<IbnItem> GetObjectToSerialize()
+ {
+ var parent = ApiService.GetItemById(QueryString["id"]) as Folder;
+ var user = ApiService.GetUserById(QueryString["userid"], true);
+
+ string name = QueryString["name"];
+
+ return GetGenre(parent, user, name);
+ }
+
+ /// <summary>
+ /// Gets a Genre
+ /// </summary>
+ private async Task<IbnItem> GetGenre(Folder parent, User user, string name)
+ {
+ int count = 0;
+
+ // Get all the allowed recursive children
+ IEnumerable<BaseItem> allItems = parent.GetRecursiveChildren(user);
+
+ foreach (var item in allItems)
+ {
+ if (item.Genres != null && item.Genres.Any(s => s.Equals(name, StringComparison.OrdinalIgnoreCase)))
+ {
+ count++;
+ }
+ }
+
+ // Get the original entity so that we can also supply the PrimaryImagePath
+ return ApiService.GetIbnItem(await Kernel.Instance.ItemController.GetGenre(name).ConfigureAwait(false), count);
+ }
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/GenresHandler.cs b/MediaBrowser.Api/HttpHandlers/GenresHandler.cs
new file mode 100644
index 000000000..4c5a9f4b7
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/GenresHandler.cs
@@ -0,0 +1,78 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Model.DTO;
+using System.Collections.Generic;
+using System.ComponentModel.Composition;
+using System.Linq;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ [Export(typeof(BaseHandler))]
+ public class GenresHandler : BaseSerializationHandler<IbnItem[]>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("genres", request);
+ }
+
+ protected override Task<IbnItem[]> GetObjectToSerialize()
+ {
+ var parent = ApiService.GetItemById(QueryString["id"]) as Folder;
+ User user = ApiService.GetUserById(QueryString["userid"], true);
+
+ return GetAllGenres(parent, user);
+ }
+
+ /// <summary>
+ /// Gets all genres from all recursive children of a folder
+ /// The CategoryInfo class is used to keep track of the number of times each genres appears
+ /// </summary>
+ private async Task<IbnItem[]> GetAllGenres(Folder parent, User user)
+ {
+ var data = new Dictionary<string, int>();
+
+ // Get all the allowed recursive children
+ IEnumerable<BaseItem> allItems = parent.GetRecursiveChildren(user);
+
+ foreach (var item in allItems)
+ {
+ // Add each genre from the item to the data dictionary
+ // If the genre already exists, increment the count
+ if (item.Genres == null)
+ {
+ continue;
+ }
+
+ foreach (string val in item.Genres)
+ {
+ if (!data.ContainsKey(val))
+ {
+ data.Add(val, 1);
+ }
+ else
+ {
+ data[val]++;
+ }
+ }
+ }
+
+ // Get the Genre objects
+ Genre[] entities = await Task.WhenAll(data.Keys.Select(key => Kernel.Instance.ItemController.GetGenre(key))).ConfigureAwait(false);
+
+ // Convert to an array of IBNItem
+ var items = new IbnItem[entities.Length];
+
+ for (int i = 0; i < entities.Length; i++)
+ {
+ Genre e = entities[i];
+
+ items[i] = ApiService.GetIbnItem(e, data[e.Name]);
+ }
+
+ return items;
+ }
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/ImageHandler.cs b/MediaBrowser.Api/HttpHandlers/ImageHandler.cs
new file mode 100644
index 000000000..4aa367fb7
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/ImageHandler.cs
@@ -0,0 +1,224 @@
+using MediaBrowser.Common.Net;
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller;
+using MediaBrowser.Controller.Drawing;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Model.Entities;
+using System;
+using System.ComponentModel.Composition;
+using System.IO;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ [Export(typeof(BaseHandler))]
+ public class ImageHandler : BaseHandler
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("image", request);
+ }
+
+ private string _imagePath;
+
+ private async Task<string> GetImagePath()
+ {
+ _imagePath = _imagePath ?? await DiscoverImagePath();
+
+ return _imagePath;
+ }
+
+ private BaseEntity _sourceEntity;
+
+ private async Task<BaseEntity> GetSourceEntity()
+ {
+ if (_sourceEntity == null)
+ {
+ if (!string.IsNullOrEmpty(QueryString["personname"]))
+ {
+ _sourceEntity =
+ await Kernel.Instance.ItemController.GetPerson(QueryString["personname"]).ConfigureAwait(false);
+ }
+
+ else if (!string.IsNullOrEmpty(QueryString["genre"]))
+ {
+ _sourceEntity =
+ await Kernel.Instance.ItemController.GetGenre(QueryString["genre"]).ConfigureAwait(false);
+ }
+
+ else if (!string.IsNullOrEmpty(QueryString["year"]))
+ {
+ _sourceEntity =
+ await
+ Kernel.Instance.ItemController.GetYear(int.Parse(QueryString["year"])).ConfigureAwait(false);
+ }
+
+ else if (!string.IsNullOrEmpty(QueryString["studio"]))
+ {
+ _sourceEntity =
+ await Kernel.Instance.ItemController.GetStudio(QueryString["studio"]).ConfigureAwait(false);
+ }
+
+ else if (!string.IsNullOrEmpty(QueryString["userid"]))
+ {
+ _sourceEntity = ApiService.GetUserById(QueryString["userid"], false);
+ }
+
+ else
+ {
+ _sourceEntity = ApiService.GetItemById(QueryString["id"]);
+ }
+ }
+
+ return _sourceEntity;
+ }
+
+ private async Task<string> DiscoverImagePath()
+ {
+ var entity = await GetSourceEntity().ConfigureAwait(false);
+
+ return ImageProcessor.GetImagePath(entity, ImageType, ImageIndex);
+ }
+
+ protected async override Task<ResponseInfo> GetResponseInfo()
+ {
+ string path = await GetImagePath().ConfigureAwait(false);
+
+ ResponseInfo info = new ResponseInfo
+ {
+ CacheDuration = TimeSpan.FromDays(365),
+ ContentType = MimeTypes.GetMimeType(path)
+ };
+
+ DateTime? date = File.GetLastWriteTimeUtc(path);
+
+ // If the file does not exist it will return jan 1, 1601
+ // http://msdn.microsoft.com/en-us/library/system.io.file.getlastwritetimeutc.aspx
+ if (date.Value.Year == 1601)
+ {
+ if (!File.Exists(path))
+ {
+ info.StatusCode = 404;
+ date = null;
+ }
+ }
+
+ info.DateLastModified = date;
+
+ return info;
+ }
+
+ private int ImageIndex
+ {
+ get
+ {
+ string val = QueryString["index"];
+
+ if (string.IsNullOrEmpty(val))
+ {
+ return 0;
+ }
+
+ return int.Parse(val);
+ }
+ }
+
+ private int? Height
+ {
+ get
+ {
+ string val = QueryString["height"];
+
+ if (string.IsNullOrEmpty(val))
+ {
+ return null;
+ }
+
+ return int.Parse(val);
+ }
+ }
+
+ private int? Width
+ {
+ get
+ {
+ string val = QueryString["width"];
+
+ if (string.IsNullOrEmpty(val))
+ {
+ return null;
+ }
+
+ return int.Parse(val);
+ }
+ }
+
+ private int? MaxHeight
+ {
+ get
+ {
+ string val = QueryString["maxheight"];
+
+ if (string.IsNullOrEmpty(val))
+ {
+ return null;
+ }
+
+ return int.Parse(val);
+ }
+ }
+
+ private int? MaxWidth
+ {
+ get
+ {
+ string val = QueryString["maxwidth"];
+
+ if (string.IsNullOrEmpty(val))
+ {
+ return null;
+ }
+
+ return int.Parse(val);
+ }
+ }
+
+ private int? Quality
+ {
+ get
+ {
+ string val = QueryString["quality"];
+
+ if (string.IsNullOrEmpty(val))
+ {
+ return null;
+ }
+
+ return int.Parse(val);
+ }
+ }
+
+ private ImageType ImageType
+ {
+ get
+ {
+ string imageType = QueryString["type"];
+
+ if (string.IsNullOrEmpty(imageType))
+ {
+ return ImageType.Primary;
+ }
+
+ return (ImageType)Enum.Parse(typeof(ImageType), imageType, true);
+ }
+ }
+
+ protected override async Task WriteResponseToOutputStream(Stream stream)
+ {
+ var entity = await GetSourceEntity().ConfigureAwait(false);
+
+ ImageProcessor.ProcessImage(entity, ImageType, ImageIndex, stream, Width, Height, MaxWidth, MaxHeight, Quality);
+ }
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/ItemHandler.cs b/MediaBrowser.Api/HttpHandlers/ItemHandler.cs
new file mode 100644
index 000000000..60b328d1a
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/ItemHandler.cs
@@ -0,0 +1,35 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Model.DTO;
+using System.ComponentModel.Composition;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ /// <summary>
+ /// Provides a handler to retrieve a single item
+ /// </summary>
+ [Export(typeof(BaseHandler))]
+ public class ItemHandler : BaseSerializationHandler<DtoBaseItem>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("item", request);
+ }
+
+ protected override Task<DtoBaseItem> GetObjectToSerialize()
+ {
+ User user = ApiService.GetUserById(QueryString["userid"], true);
+
+ BaseItem item = ApiService.GetItemById(QueryString["id"]);
+
+ if (item == null)
+ {
+ return null;
+ }
+
+ return ApiService.GetDtoBaseItem(item, user);
+ }
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/ItemListHandler.cs b/MediaBrowser.Api/HttpHandlers/ItemListHandler.cs
new file mode 100644
index 000000000..d236e546b
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/ItemListHandler.cs
@@ -0,0 +1,84 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Model.DTO;
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.Composition;
+using System.Linq;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ [Export(typeof(BaseHandler))]
+ public class ItemListHandler : BaseSerializationHandler<DtoBaseItem[]>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("itemlist", request);
+ }
+
+ protected override Task<DtoBaseItem[]> GetObjectToSerialize()
+ {
+ User user = ApiService.GetUserById(QueryString["userid"], true);
+
+ return Task.WhenAll(GetItemsToSerialize(user).Select(i => ApiService.GetDtoBaseItem(i, user, includeChildren: false, includePeople: false)));
+ }
+
+ private IEnumerable<BaseItem> GetItemsToSerialize(User user)
+ {
+ var parent = ApiService.GetItemById(ItemId) as Folder;
+
+ if (ListType.Equals("inprogressitems", StringComparison.OrdinalIgnoreCase))
+ {
+ return parent.GetInProgressItems(user);
+ }
+ if (ListType.Equals("recentlyaddeditems", StringComparison.OrdinalIgnoreCase))
+ {
+ return parent.GetRecentlyAddedItems(user);
+ }
+ if (ListType.Equals("recentlyaddedunplayeditems", StringComparison.OrdinalIgnoreCase))
+ {
+ return parent.GetRecentlyAddedUnplayedItems(user);
+ }
+ if (ListType.Equals("itemswithgenre", StringComparison.OrdinalIgnoreCase))
+ {
+ return parent.GetItemsWithGenre(QueryString["name"], user);
+ }
+ if (ListType.Equals("itemswithyear", StringComparison.OrdinalIgnoreCase))
+ {
+ return parent.GetItemsWithYear(int.Parse(QueryString["year"]), user);
+ }
+ if (ListType.Equals("itemswithstudio", StringComparison.OrdinalIgnoreCase))
+ {
+ return parent.GetItemsWithStudio(QueryString["name"], user);
+ }
+ if (ListType.Equals("itemswithperson", StringComparison.OrdinalIgnoreCase))
+ {
+ return parent.GetItemsWithPerson(QueryString["name"], null, user);
+ }
+ if (ListType.Equals("favorites", StringComparison.OrdinalIgnoreCase))
+ {
+ return parent.GetFavoriteItems(user);
+ }
+
+ throw new InvalidOperationException();
+ }
+
+ protected string ItemId
+ {
+ get
+ {
+ return QueryString["id"];
+ }
+ }
+
+ private string ListType
+ {
+ get
+ {
+ return QueryString["listtype"] ?? string.Empty;
+ }
+ }
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/MovieSpecialFeaturesHandler.cs b/MediaBrowser.Api/HttpHandlers/MovieSpecialFeaturesHandler.cs
new file mode 100644
index 000000000..3ab78ee8d
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/MovieSpecialFeaturesHandler.cs
@@ -0,0 +1,46 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Entities.Movies;
+using MediaBrowser.Model.DTO;
+using System.ComponentModel.Composition;
+using System.Linq;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ /// <summary>
+ /// This handler retrieves special features for movies
+ /// </summary>
+ [Export(typeof(BaseHandler))]
+ public class MovieSpecialFeaturesHandler : BaseSerializationHandler<DtoBaseItem[]>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("MovieSpecialFeatures", request);
+ }
+
+ protected override Task<DtoBaseItem[]> GetObjectToSerialize()
+ {
+ User user = ApiService.GetUserById(QueryString["userid"], true);
+
+ var movie = ApiService.GetItemById(ItemId) as Movie;
+
+ // If none
+ if (movie.SpecialFeatures == null)
+ {
+ return Task.FromResult(new DtoBaseItem[] { });
+ }
+
+ return Task.WhenAll(movie.SpecialFeatures.Select(i => ApiService.GetDtoBaseItem(i, user, includeChildren: false)));
+ }
+
+ protected string ItemId
+ {
+ get
+ {
+ return QueryString["id"];
+ }
+ }
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/PersonHandler.cs b/MediaBrowser.Api/HttpHandlers/PersonHandler.cs
new file mode 100644
index 000000000..fbbd88a11
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/PersonHandler.cs
@@ -0,0 +1,55 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Model.DTO;
+using System.Collections.Generic;
+using System.ComponentModel.Composition;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ /// <summary>
+ /// Gets a single Person
+ /// </summary>
+ [Export(typeof(BaseHandler))]
+ public class PersonHandler : BaseSerializationHandler<IbnItem>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("person", request);
+ }
+
+ protected override Task<IbnItem> GetObjectToSerialize()
+ {
+ var parent = ApiService.GetItemById(QueryString["id"]) as Folder;
+ var user = ApiService.GetUserById(QueryString["userid"], true);
+
+ string name = QueryString["name"];
+
+ return GetPerson(parent, user, name);
+ }
+
+ /// <summary>
+ /// Gets a Person
+ /// </summary>
+ private async Task<IbnItem> GetPerson(Folder parent, User user, string name)
+ {
+ int count = 0;
+
+ // Get all the allowed recursive children
+ IEnumerable<BaseItem> allItems = parent.GetRecursiveChildren(user);
+
+ foreach (var item in allItems)
+ {
+ if (item.People != null && item.People.ContainsKey(name))
+ {
+ count++;
+ }
+ }
+
+ // Get the original entity so that we can also supply the PrimaryImagePath
+ return ApiService.GetIbnItem(await Kernel.Instance.ItemController.GetPerson(name).ConfigureAwait(false), count);
+ }
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/PlayedStatusHandler.cs b/MediaBrowser.Api/HttpHandlers/PlayedStatusHandler.cs
new file mode 100644
index 000000000..c010bcb02
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/PlayedStatusHandler.cs
@@ -0,0 +1,38 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Model.DTO;
+using System.ComponentModel.Composition;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ /// <summary>
+ /// Provides a handler to set played status for an item
+ /// </summary>
+ [Export(typeof(BaseHandler))]
+ public class PlayedStatusHandler : BaseSerializationHandler<DtoUserItemData>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("PlayedStatus", request);
+ }
+
+ protected override Task<DtoUserItemData> GetObjectToSerialize()
+ {
+ // Get the item
+ BaseItem item = ApiService.GetItemById(QueryString["id"]);
+
+ // Get the user
+ User user = ApiService.GetUserById(QueryString["userid"], true);
+
+ bool wasPlayed = QueryString["played"] == "1";
+
+ item.SetPlayedStatus(user, wasPlayed);
+
+ UserItemData data = item.GetUserData(user, true);
+
+ return Task.FromResult(ApiService.GetDtoUserItemData(data));
+ }
+ }
+} \ No newline at end of file
diff --git a/MediaBrowser.Api/HttpHandlers/PluginAssemblyHandler.cs b/MediaBrowser.Api/HttpHandlers/PluginAssemblyHandler.cs
new file mode 100644
index 000000000..47f08c8c3
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/PluginAssemblyHandler.cs
@@ -0,0 +1,38 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller;
+using System;
+using System.ComponentModel.Composition;
+using System.IO;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ [Export(typeof(BaseHandler))]
+ class PluginAssemblyHandler : BaseHandler
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("pluginassembly", request);
+ }
+
+ protected override Task<ResponseInfo> GetResponseInfo()
+ {
+ throw new NotImplementedException();
+ }
+
+ protected override Task WriteResponseToOutputStream(Stream stream)
+ {
+ throw new NotImplementedException();
+ }
+
+ public override Task ProcessRequest(HttpListenerContext ctx)
+ {
+ string filename = ctx.Request.QueryString["assemblyfilename"];
+
+ string path = Path.Combine(Kernel.Instance.ApplicationPaths.PluginsPath, filename);
+
+ return new StaticFileHandler { Path = path }.ProcessRequest(ctx);
+ }
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/PluginConfigurationHandler.cs b/MediaBrowser.Api/HttpHandlers/PluginConfigurationHandler.cs
new file mode 100644
index 000000000..dc363956f
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/PluginConfigurationHandler.cs
@@ -0,0 +1,53 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Common.Plugins;
+using MediaBrowser.Controller;
+using MediaBrowser.Model.Plugins;
+using System;
+using System.ComponentModel.Composition;
+using System.Linq;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ [Export(typeof(BaseHandler))]
+ public class PluginConfigurationHandler : BaseSerializationHandler<BasePluginConfiguration>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("pluginconfiguration", request);
+ }
+
+ private BasePlugin _plugin;
+ private BasePlugin Plugin
+ {
+ get
+ {
+ if (_plugin == null)
+ {
+ string name = QueryString["assemblyfilename"];
+
+ _plugin = Kernel.Instance.Plugins.First(p => p.AssemblyFileName.Equals(name, StringComparison.OrdinalIgnoreCase));
+ }
+
+ return _plugin;
+ }
+ }
+
+ protected override Task<BasePluginConfiguration> GetObjectToSerialize()
+ {
+ return Task.FromResult(Plugin.Configuration);
+ }
+
+ protected override async Task<ResponseInfo> GetResponseInfo()
+ {
+ var info = await base.GetResponseInfo().ConfigureAwait(false);
+
+ info.DateLastModified = Plugin.ConfigurationDateLastModified;
+
+ info.CacheDuration = TimeSpan.FromDays(7);
+
+ return info;
+ }
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/PluginsHandler.cs b/MediaBrowser.Api/HttpHandlers/PluginsHandler.cs
new file mode 100644
index 000000000..a1b37ecab
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/PluginsHandler.cs
@@ -0,0 +1,38 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller;
+using MediaBrowser.Model.DTO;
+using System.Collections.Generic;
+using System.ComponentModel.Composition;
+using System.Linq;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ /// <summary>
+ /// Provides information about installed plugins
+ /// </summary>
+ [Export(typeof(BaseHandler))]
+ public class PluginsHandler : BaseSerializationHandler<IEnumerable<PluginInfo>>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("plugins", request);
+ }
+
+ protected override Task<IEnumerable<PluginInfo>> GetObjectToSerialize()
+ {
+ var plugins = Kernel.Instance.Plugins.Select(p => new PluginInfo
+ {
+ Name = p.Name,
+ Enabled = p.Enabled,
+ DownloadToUI = p.DownloadToUi,
+ Version = p.Version.ToString(),
+ AssemblyFileName = p.AssemblyFileName,
+ ConfigurationDateLastModified = p.ConfigurationDateLastModified
+ });
+
+ return Task.FromResult(plugins);
+ }
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/ServerConfigurationHandler.cs b/MediaBrowser.Api/HttpHandlers/ServerConfigurationHandler.cs
new file mode 100644
index 000000000..48c6761b1
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/ServerConfigurationHandler.cs
@@ -0,0 +1,37 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller;
+using MediaBrowser.Model.Configuration;
+using System;
+using System.ComponentModel.Composition;
+using System.IO;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ [Export(typeof(BaseHandler))]
+ class ServerConfigurationHandler : BaseSerializationHandler<ServerConfiguration>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("serverconfiguration", request);
+ }
+
+ protected override Task<ServerConfiguration> GetObjectToSerialize()
+ {
+ return Task.FromResult(Kernel.Instance.Configuration);
+ }
+
+ protected override async Task<ResponseInfo> GetResponseInfo()
+ {
+ var info = await base.GetResponseInfo().ConfigureAwait(false);
+
+ info.DateLastModified =
+ File.GetLastWriteTimeUtc(Kernel.Instance.ApplicationPaths.SystemConfigurationFilePath);
+
+ info.CacheDuration = TimeSpan.FromDays(7);
+
+ return info;
+ }
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/StudioHandler.cs b/MediaBrowser.Api/HttpHandlers/StudioHandler.cs
new file mode 100644
index 000000000..6576e2cfe
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/StudioHandler.cs
@@ -0,0 +1,57 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Model.DTO;
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.Composition;
+using System.Linq;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ /// <summary>
+ /// Gets a single studio
+ /// </summary>
+ [Export(typeof(BaseHandler))]
+ public class StudioHandler : BaseSerializationHandler<IbnItem>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("studio", request);
+ }
+
+ protected override Task<IbnItem> GetObjectToSerialize()
+ {
+ var parent = ApiService.GetItemById(QueryString["id"]) as Folder;
+ var user = ApiService.GetUserById(QueryString["userid"], true);
+
+ string name = QueryString["name"];
+
+ return GetStudio(parent, user, name);
+ }
+
+ /// <summary>
+ /// Gets a Studio
+ /// </summary>
+ private async Task<IbnItem> GetStudio(Folder parent, User user, string name)
+ {
+ int count = 0;
+
+ // Get all the allowed recursive children
+ IEnumerable<BaseItem> allItems = parent.GetRecursiveChildren(user);
+
+ foreach (var item in allItems)
+ {
+ if (item.Studios != null && item.Studios.Any(s => s.Equals(name, StringComparison.OrdinalIgnoreCase)))
+ {
+ count++;
+ }
+ }
+
+ // Get the original entity so that we can also supply the PrimaryImagePath
+ return ApiService.GetIbnItem(await Kernel.Instance.ItemController.GetStudio(name).ConfigureAwait(false), count);
+ }
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/StudiosHandler.cs b/MediaBrowser.Api/HttpHandlers/StudiosHandler.cs
new file mode 100644
index 000000000..4377a0f43
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/StudiosHandler.cs
@@ -0,0 +1,78 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Model.DTO;
+using System.Collections.Generic;
+using System.ComponentModel.Composition;
+using System.Linq;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ [Export(typeof(BaseHandler))]
+ public class StudiosHandler : BaseSerializationHandler<IbnItem[]>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("studios", request);
+ }
+
+ protected override Task<IbnItem[]> GetObjectToSerialize()
+ {
+ var parent = ApiService.GetItemById(QueryString["id"]) as Folder;
+ var user = ApiService.GetUserById(QueryString["userid"], true);
+
+ return GetAllStudios(parent, user);
+ }
+
+ /// <summary>
+ /// Gets all studios from all recursive children of a folder
+ /// The CategoryInfo class is used to keep track of the number of times each studio appears
+ /// </summary>
+ private async Task<IbnItem[]> GetAllStudios(Folder parent, User user)
+ {
+ var data = new Dictionary<string, int>();
+
+ // Get all the allowed recursive children
+ IEnumerable<BaseItem> allItems = parent.GetRecursiveChildren(user);
+
+ foreach (var item in allItems)
+ {
+ // Add each studio from the item to the data dictionary
+ // If the studio already exists, increment the count
+ if (item.Studios == null)
+ {
+ continue;
+ }
+
+ foreach (string val in item.Studios)
+ {
+ if (!data.ContainsKey(val))
+ {
+ data.Add(val, 1);
+ }
+ else
+ {
+ data[val]++;
+ }
+ }
+ }
+
+ // Get the Studio objects
+ Studio[] entities = await Task.WhenAll(data.Keys.Select(key => Kernel.Instance.ItemController.GetStudio(key))).ConfigureAwait(false);
+
+ // Convert to an array of IBNItem
+ var items = new IbnItem[entities.Length];
+
+ for (int i = 0; i < entities.Length; i++)
+ {
+ Studio e = entities[i];
+
+ items[i] = ApiService.GetIbnItem(e, data[e.Name]);
+ }
+
+ return items;
+ }
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/UserAuthenticationHandler.cs b/MediaBrowser.Api/HttpHandlers/UserAuthenticationHandler.cs
new file mode 100644
index 000000000..fa9d97598
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/UserAuthenticationHandler.cs
@@ -0,0 +1,29 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Model.Authentication;
+using System.ComponentModel.Composition;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ [Export(typeof(BaseHandler))]
+ class UserAuthenticationHandler : BaseSerializationHandler<AuthenticationResult>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("UserAuthentication", request);
+ }
+
+ protected override async Task<AuthenticationResult> GetObjectToSerialize()
+ {
+ string userId = await GetFormValue("userid").ConfigureAwait(false);
+ User user = ApiService.GetUserById(userId, false);
+
+ string password = await GetFormValue("password").ConfigureAwait(false);
+
+ return Kernel.Instance.AuthenticateUser(user, password);
+ }
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/UserHandler.cs b/MediaBrowser.Api/HttpHandlers/UserHandler.cs
new file mode 100644
index 000000000..bc9286204
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/UserHandler.cs
@@ -0,0 +1,29 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Model.DTO;
+using System.ComponentModel.Composition;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ [Export(typeof(BaseHandler))]
+ class UserHandler : BaseSerializationHandler<DtoUser>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("user", request);
+ }
+
+ protected override Task<DtoUser> GetObjectToSerialize()
+ {
+ string id = QueryString["id"];
+
+ User user = string.IsNullOrEmpty(id) ? ApiService.GetDefaultUser(false) : ApiService.GetUserById(id, false);
+
+ DtoUser dto = ApiService.GetDtoUser(user);
+
+ return Task.FromResult(dto);
+ }
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/UserItemRatingHandler.cs b/MediaBrowser.Api/HttpHandlers/UserItemRatingHandler.cs
new file mode 100644
index 000000000..aed0804b6
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/UserItemRatingHandler.cs
@@ -0,0 +1,46 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Model.DTO;
+using System.ComponentModel.Composition;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ /// <summary>
+ /// Provides a handler to set a user's rating for an item
+ /// </summary>
+ [Export(typeof(BaseHandler))]
+ public class UserItemRatingHandler : BaseSerializationHandler<DtoUserItemData>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("UserItemRating", request);
+ }
+
+ protected override Task<DtoUserItemData> GetObjectToSerialize()
+ {
+ // Get the item
+ BaseItem item = ApiService.GetItemById(QueryString["id"]);
+
+ // Get the user
+ User user = ApiService.GetUserById(QueryString["userid"], true);
+
+ // Get the user data for this item
+ UserItemData data = item.GetUserData(user, true);
+
+ // If clearing the rating, set it to null
+ if (QueryString["clear"] == "1")
+ {
+ data.Rating = null;
+ }
+
+ else
+ {
+ data.Likes = QueryString["likes"] == "1";
+ }
+
+ return Task.FromResult(ApiService.GetDtoUserItemData(data));
+ }
+ }
+} \ No newline at end of file
diff --git a/MediaBrowser.Api/HttpHandlers/UsersHandler.cs b/MediaBrowser.Api/HttpHandlers/UsersHandler.cs
new file mode 100644
index 000000000..3fc3a7d58
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/UsersHandler.cs
@@ -0,0 +1,25 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller;
+using MediaBrowser.Model.DTO;
+using System.Collections.Generic;
+using System.ComponentModel.Composition;
+using System.Linq;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ [Export(typeof(BaseHandler))]
+ class UsersHandler : BaseSerializationHandler<IEnumerable<DtoUser>>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("users", request);
+ }
+
+ protected override Task<IEnumerable<DtoUser>> GetObjectToSerialize()
+ {
+ return Task.FromResult(Kernel.Instance.Users.Select(u => ApiService.GetDtoUser(u)));
+ }
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/VideoHandler.cs b/MediaBrowser.Api/HttpHandlers/VideoHandler.cs
new file mode 100644
index 000000000..e34a1b41f
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/VideoHandler.cs
@@ -0,0 +1,424 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller.Drawing;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Model.DTO;
+using MediaBrowser.Model.Entities;
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.Composition;
+using System.Drawing;
+using System.Linq;
+using System.Net;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ /// <summary>
+ /// Supported output formats: mkv,m4v,mp4,asf,wmv,mov,webm,ogv,3gp,avi,ts,flv
+ /// </summary>
+ [Export(typeof(BaseHandler))]
+ class VideoHandler : BaseMediaHandler<Video, VideoOutputFormats>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("video", request);
+ }
+
+ /// <summary>
+ /// We can output these files directly, but we can't encode them
+ /// </summary>
+ protected override IEnumerable<VideoOutputFormats> UnsupportedOutputEncodingFormats
+ {
+ get
+ {
+ // mp4, 3gp, mov - muxer does not support non-seekable output
+ // avi, mov, mkv, m4v - can't stream these when encoding. the player will try to download them completely before starting playback.
+ // wmv - can't seem to figure out the output format name
+ return new VideoOutputFormats[] { VideoOutputFormats.Mp4, VideoOutputFormats.ThreeGp, VideoOutputFormats.M4V, VideoOutputFormats.Mkv, VideoOutputFormats.Avi, VideoOutputFormats.Mov, VideoOutputFormats.Wmv };
+ }
+ }
+
+ /// <summary>
+ /// Determines whether or not we can just output the original file directly
+ /// </summary>
+ protected override bool RequiresConversion()
+ {
+ if (base.RequiresConversion())
+ {
+ return true;
+ }
+
+ // See if the video requires conversion
+ if (RequiresVideoConversion())
+ {
+ return true;
+ }
+
+ // See if the audio requires conversion
+ AudioStream audioStream = (LibraryItem.AudioStreams ?? new List<AudioStream>()).FirstOrDefault();
+
+ if (audioStream != null)
+ {
+ if (RequiresAudioConversion(audioStream))
+ {
+ return true;
+ }
+ }
+
+ // Yay
+ return false;
+ }
+
+ /// <summary>
+ /// Translates the output file extension to the format param that follows "-f" on the ffmpeg command line
+ /// </summary>
+ private string GetFfMpegOutputFormat(VideoOutputFormats outputFormat)
+ {
+ if (outputFormat == VideoOutputFormats.Mkv)
+ {
+ return "matroska";
+ }
+ if (outputFormat == VideoOutputFormats.Ts)
+ {
+ return "mpegts";
+ }
+ if (outputFormat == VideoOutputFormats.Ogv)
+ {
+ return "ogg";
+ }
+
+ return outputFormat.ToString().ToLower();
+ }
+
+ /// <summary>
+ /// Creates arguments to pass to ffmpeg
+ /// </summary>
+ protected override string GetCommandLineArguments()
+ {
+ VideoOutputFormats outputFormat = GetConversionOutputFormat();
+
+ return string.Format("-i \"{0}\" -threads 0 {1} {2} -f {3} -",
+ LibraryItem.Path,
+ GetVideoArguments(outputFormat),
+ GetAudioArguments(outputFormat),
+ GetFfMpegOutputFormat(outputFormat)
+ );
+ }
+
+ /// <summary>
+ /// Gets video arguments to pass to ffmpeg
+ /// </summary>
+ private string GetVideoArguments(VideoOutputFormats outputFormat)
+ {
+ // Get the output codec name
+ string codec = GetVideoCodec(outputFormat);
+
+ string args = "-vcodec " + codec;
+
+ // If we're encoding video, add additional params
+ if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
+ {
+ // Add resolution params, if specified
+ if (Width.HasValue || Height.HasValue || MaxHeight.HasValue || MaxWidth.HasValue)
+ {
+ Size size = DrawingUtils.Resize(LibraryItem.Width, LibraryItem.Height, Width, Height, MaxWidth, MaxHeight);
+
+ args += string.Format(" -s {0}x{1}", size.Width, size.Height);
+ }
+ }
+
+ return args;
+ }
+
+ /// <summary>
+ /// Gets audio arguments to pass to ffmpeg
+ /// </summary>
+ private string GetAudioArguments(VideoOutputFormats outputFormat)
+ {
+ AudioStream audioStream = (LibraryItem.AudioStreams ?? new List<AudioStream>()).FirstOrDefault();
+
+ // If the video doesn't have an audio stream, return empty
+ if (audioStream == null)
+ {
+ return string.Empty;
+ }
+
+ // Get the output codec name
+ string codec = GetAudioCodec(audioStream, outputFormat);
+
+ string args = "-acodec " + codec;
+
+ // If we're encoding audio, add additional params
+ if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
+ {
+ // Add the number of audio channels
+ int? channels = GetNumAudioChannelsParam(codec, audioStream.Channels);
+
+ if (channels.HasValue)
+ {
+ args += " -ac " + channels.Value;
+ }
+
+ // Add the audio sample rate
+ int? sampleRate = GetSampleRateParam(audioStream.SampleRate);
+
+ if (sampleRate.HasValue)
+ {
+ args += " -ar " + sampleRate.Value;
+ }
+
+ }
+
+ return args;
+ }
+
+ /// <summary>
+ /// Gets the name of the output video codec
+ /// </summary>
+ private string GetVideoCodec(VideoOutputFormats outputFormat)
+ {
+ // Some output containers require specific codecs
+
+ if (outputFormat == VideoOutputFormats.Webm)
+ {
+ // Per webm specification, it must be vpx
+ return "libvpx";
+ }
+ if (outputFormat == VideoOutputFormats.Asf)
+ {
+ return "wmv2";
+ }
+ if (outputFormat == VideoOutputFormats.Wmv)
+ {
+ return "wmv2";
+ }
+ if (outputFormat == VideoOutputFormats.Ogv)
+ {
+ return "libtheora";
+ }
+
+ // Skip encoding when possible
+ if (!RequiresVideoConversion())
+ {
+ return "copy";
+ }
+
+ return "libx264";
+ }
+
+ /// <summary>
+ /// Gets the name of the output audio codec
+ /// </summary>
+ private string GetAudioCodec(AudioStream audioStream, VideoOutputFormats outputFormat)
+ {
+ // Some output containers require specific codecs
+
+ if (outputFormat == VideoOutputFormats.Webm)
+ {
+ // Per webm specification, it must be vorbis
+ return "libvorbis";
+ }
+ if (outputFormat == VideoOutputFormats.Asf)
+ {
+ return "wmav2";
+ }
+ if (outputFormat == VideoOutputFormats.Wmv)
+ {
+ return "wmav2";
+ }
+ if (outputFormat == VideoOutputFormats.Ogv)
+ {
+ return "libvorbis";
+ }
+
+ // Skip encoding when possible
+ if (!RequiresAudioConversion(audioStream))
+ {
+ return "copy";
+ }
+
+ return "libvo_aacenc";
+ }
+
+ /// <summary>
+ /// Gets the number of audio channels to specify on the command line
+ /// </summary>
+ private int? GetNumAudioChannelsParam(string audioCodec, int libraryItemChannels)
+ {
+ if (libraryItemChannels > 2)
+ {
+ if (audioCodec.Equals("libvo_aacenc"))
+ {
+ // libvo_aacenc currently only supports two channel output
+ return 2;
+ }
+ if (audioCodec.Equals("wmav2"))
+ {
+ // wmav2 currently only supports two channel output
+ return 2;
+ }
+ }
+
+ return GetNumAudioChannelsParam(libraryItemChannels);
+ }
+
+ /// <summary>
+ /// Determines if the video stream requires encoding
+ /// </summary>
+ private bool RequiresVideoConversion()
+ {
+ // Check dimensions
+
+ // If a specific width is required, validate that
+ if (Width.HasValue)
+ {
+ if (Width.Value != LibraryItem.Width)
+ {
+ return true;
+ }
+ }
+
+ // If a specific height is required, validate that
+ if (Height.HasValue)
+ {
+ if (Height.Value != LibraryItem.Height)
+ {
+ return true;
+ }
+ }
+
+ // If a max width is required, validate that
+ if (MaxWidth.HasValue)
+ {
+ if (MaxWidth.Value < LibraryItem.Width)
+ {
+ return true;
+ }
+ }
+
+ // If a max height is required, validate that
+ if (MaxHeight.HasValue)
+ {
+ if (MaxHeight.Value < LibraryItem.Height)
+ {
+ return true;
+ }
+ }
+
+ // If the codec is already h264, don't encode
+ if (LibraryItem.Codec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 || LibraryItem.Codec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1)
+ {
+ return false;
+ }
+
+ return false;
+ }
+
+ /// <summary>
+ /// Determines if the audio stream requires encoding
+ /// </summary>
+ private bool RequiresAudioConversion(AudioStream audio)
+ {
+
+ // If the input stream has more audio channels than the client can handle, we need to encode
+ if (AudioChannels.HasValue)
+ {
+ if (audio.Channels > AudioChannels.Value)
+ {
+ return true;
+ }
+ }
+
+ // Aac, ac-3 and mp3 are all pretty much universally supported. No need to encode them
+
+ if (audio.Codec.IndexOf("aac", StringComparison.OrdinalIgnoreCase) != -1)
+ {
+ return false;
+ }
+
+ if (audio.Codec.IndexOf("ac-3", StringComparison.OrdinalIgnoreCase) != -1 || audio.Codec.IndexOf("ac3", StringComparison.OrdinalIgnoreCase) != -1)
+ {
+ return false;
+ }
+
+ if (audio.Codec.IndexOf("mpeg", StringComparison.OrdinalIgnoreCase) != -1 || audio.Codec.IndexOf("mp3", StringComparison.OrdinalIgnoreCase) != -1)
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ /// <summary>
+ /// Gets the fixed output video height, in pixels
+ /// </summary>
+ private int? Height
+ {
+ get
+ {
+ string val = QueryString["height"];
+
+ if (string.IsNullOrEmpty(val))
+ {
+ return null;
+ }
+
+ return int.Parse(val);
+ }
+ }
+
+ /// <summary>
+ /// Gets the fixed output video width, in pixels
+ /// </summary>
+ private int? Width
+ {
+ get
+ {
+ string val = QueryString["width"];
+
+ if (string.IsNullOrEmpty(val))
+ {
+ return null;
+ }
+
+ return int.Parse(val);
+ }
+ }
+
+ /// <summary>
+ /// Gets the maximum output video height, in pixels
+ /// </summary>
+ private int? MaxHeight
+ {
+ get
+ {
+ string val = QueryString["maxheight"];
+
+ if (string.IsNullOrEmpty(val))
+ {
+ return null;
+ }
+
+ return int.Parse(val);
+ }
+ }
+
+ /// <summary>
+ /// Gets the maximum output video width, in pixels
+ /// </summary>
+ private int? MaxWidth
+ {
+ get
+ {
+ string val = QueryString["maxwidth"];
+
+ if (string.IsNullOrEmpty(val))
+ {
+ return null;
+ }
+
+ return int.Parse(val);
+ }
+ }
+
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/WeatherHandler.cs b/MediaBrowser.Api/HttpHandlers/WeatherHandler.cs
new file mode 100644
index 000000000..378e89067
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/WeatherHandler.cs
@@ -0,0 +1,43 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller;
+using MediaBrowser.Model.Weather;
+using System;
+using System.ComponentModel.Composition;
+using System.Linq;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ [Export(typeof(BaseHandler))]
+ class WeatherHandler : BaseSerializationHandler<WeatherInfo>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("weather", request);
+ }
+
+ protected override Task<WeatherInfo> GetObjectToSerialize()
+ {
+ // If a specific zip code was requested on the query string, use that. Otherwise use the value from configuration
+
+ string zipCode = QueryString["zipcode"];
+
+ if (string.IsNullOrWhiteSpace(zipCode))
+ {
+ zipCode = Kernel.Instance.Configuration.WeatherZipCode;
+ }
+
+ return Kernel.Instance.WeatherProviders.First().GetWeatherInfoAsync(zipCode);
+ }
+
+ protected override async Task<ResponseInfo> GetResponseInfo()
+ {
+ var info = await base.GetResponseInfo().ConfigureAwait(false);
+
+ info.CacheDuration = TimeSpan.FromMinutes(15);
+
+ return info;
+ }
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/YearHandler.cs b/MediaBrowser.Api/HttpHandlers/YearHandler.cs
new file mode 100644
index 000000000..dbd1d25be
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/YearHandler.cs
@@ -0,0 +1,55 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Model.DTO;
+using System.Collections.Generic;
+using System.ComponentModel.Composition;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ /// <summary>
+ /// Gets a single year
+ /// </summary>
+ [Export(typeof(BaseHandler))]
+ public class YearHandler : BaseSerializationHandler<IbnItem>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("year", request);
+ }
+
+ protected override Task<IbnItem> GetObjectToSerialize()
+ {
+ var parent = ApiService.GetItemById(QueryString["id"]) as Folder;
+ var user = ApiService.GetUserById(QueryString["userid"], true);
+
+ string year = QueryString["year"];
+
+ return GetYear(parent, user, int.Parse(year));
+ }
+
+ /// <summary>
+ /// Gets a Year
+ /// </summary>
+ private async Task<IbnItem> GetYear(Folder parent, User user, int year)
+ {
+ int count = 0;
+
+ // Get all the allowed recursive children
+ IEnumerable<BaseItem> allItems = parent.GetRecursiveChildren(user);
+
+ foreach (var item in allItems)
+ {
+ if (item.ProductionYear.HasValue && item.ProductionYear.Value == year)
+ {
+ count++;
+ }
+ }
+
+ // Get the original entity so that we can also supply the PrimaryImagePath
+ return ApiService.GetIbnItem(await Kernel.Instance.ItemController.GetYear(year).ConfigureAwait(false), count);
+ }
+ }
+}
diff --git a/MediaBrowser.Api/HttpHandlers/YearsHandler.cs b/MediaBrowser.Api/HttpHandlers/YearsHandler.cs
new file mode 100644
index 000000000..7c90768e8
--- /dev/null
+++ b/MediaBrowser.Api/HttpHandlers/YearsHandler.cs
@@ -0,0 +1,75 @@
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Model.DTO;
+using System.Collections.Generic;
+using System.ComponentModel.Composition;
+using System.Linq;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ [Export(typeof(BaseHandler))]
+ public class YearsHandler : BaseSerializationHandler<IbnItem[]>
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("years", request);
+ }
+
+ protected override Task<IbnItem[]> GetObjectToSerialize()
+ {
+ var parent = ApiService.GetItemById(QueryString["id"]) as Folder;
+ User user = ApiService.GetUserById(QueryString["userid"], true);
+
+ return GetAllYears(parent, user);
+ }
+
+ /// <summary>
+ /// Gets all years from all recursive children of a folder
+ /// The CategoryInfo class is used to keep track of the number of times each year appears
+ /// </summary>
+ private async Task<IbnItem[]> GetAllYears(Folder parent, User user)
+ {
+ var data = new Dictionary<int, int>();
+
+ // Get all the allowed recursive children
+ IEnumerable<BaseItem> allItems = parent.GetRecursiveChildren(user);
+
+ foreach (var item in allItems)
+ {
+ // Add the year from the item to the data dictionary
+ // If the year already exists, increment the count
+ if (item.ProductionYear == null)
+ {
+ continue;
+ }
+
+ if (!data.ContainsKey(item.ProductionYear.Value))
+ {
+ data.Add(item.ProductionYear.Value, 1);
+ }
+ else
+ {
+ data[item.ProductionYear.Value]++;
+ }
+ }
+
+ // Get the Year objects
+ Year[] entities = await Task.WhenAll(data.Keys.Select(key => Kernel.Instance.ItemController.GetYear(key))).ConfigureAwait(false);
+
+ // Convert to an array of IBNItem
+ var items = new IbnItem[entities.Length];
+
+ for (int i = 0; i < entities.Length; i++)
+ {
+ Year e = entities[i];
+
+ items[i] = ApiService.GetIbnItem(e, data[int.Parse(e.Name)]);
+ }
+
+ return items;
+ }
+ }
+}