diff options
| author | LukePulverenti <luke.pulverenti@gmail.com> | 2013-02-20 20:33:05 -0500 |
|---|---|---|
| committer | LukePulverenti <luke.pulverenti@gmail.com> | 2013-02-20 20:33:05 -0500 |
| commit | 767cdc1f6f6a63ce997fc9476911e2c361f9d402 (patch) | |
| tree | 49add55976f895441167c66cfa95e5c7688d18ce /MediaBrowser.Api/HttpHandlers | |
| parent | 845554722efaed872948a9e0f7202e3ef52f1b6e (diff) | |
Pushing missing changes
Diffstat (limited to 'MediaBrowser.Api/HttpHandlers')
27 files changed, 375 insertions, 2056 deletions
diff --git a/MediaBrowser.Api/HttpHandlers/AudioHandler.cs b/MediaBrowser.Api/HttpHandlers/AudioHandler.cs deleted file mode 100644 index 9c16acd2ef..0000000000 --- a/MediaBrowser.Api/HttpHandlers/AudioHandler.cs +++ /dev/null @@ -1,119 +0,0 @@ -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 deleted file mode 100644 index 96ef606813..0000000000 --- a/MediaBrowser.Api/HttpHandlers/BaseMediaHandler.cs +++ /dev/null @@ -1,255 +0,0 @@ -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 deleted file mode 100644 index 19c175d8b8..0000000000 --- a/MediaBrowser.Api/HttpHandlers/FavoriteStatusHandler.cs +++ /dev/null @@ -1,38 +0,0 @@ -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 deleted file mode 100644 index 7cca2aea76..0000000000 --- a/MediaBrowser.Api/HttpHandlers/GenreHandler.cs +++ /dev/null @@ -1,57 +0,0 @@ -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 deleted file mode 100644 index 4c5a9f4b7f..0000000000 --- a/MediaBrowser.Api/HttpHandlers/GenresHandler.cs +++ /dev/null @@ -1,78 +0,0 @@ -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 deleted file mode 100644 index 4aa367fb7e..0000000000 --- a/MediaBrowser.Api/HttpHandlers/ImageHandler.cs +++ /dev/null @@ -1,224 +0,0 @@ -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 deleted file mode 100644 index 60b328d1a3..0000000000 --- a/MediaBrowser.Api/HttpHandlers/ItemHandler.cs +++ /dev/null @@ -1,35 +0,0 @@ -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 deleted file mode 100644 index d236e546b2..0000000000 --- a/MediaBrowser.Api/HttpHandlers/ItemListHandler.cs +++ /dev/null @@ -1,84 +0,0 @@ -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 deleted file mode 100644 index 3ab78ee8d1..0000000000 --- a/MediaBrowser.Api/HttpHandlers/MovieSpecialFeaturesHandler.cs +++ /dev/null @@ -1,46 +0,0 @@ -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 deleted file mode 100644 index fbbd88a11d..0000000000 --- a/MediaBrowser.Api/HttpHandlers/PersonHandler.cs +++ /dev/null @@ -1,55 +0,0 @@ -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/PlaybackCheckInHandler.cs b/MediaBrowser.Api/HttpHandlers/PlaybackCheckInHandler.cs new file mode 100644 index 0000000000..c6f11e5bbc --- /dev/null +++ b/MediaBrowser.Api/HttpHandlers/PlaybackCheckInHandler.cs @@ -0,0 +1,112 @@ +using MediaBrowser.Common.Net.Handlers; +using MediaBrowser.Controller; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Model.Connectivity; +using MediaBrowser.Model.DTO; +using System; +using System.ComponentModel.Composition; +using System.Threading.Tasks; + +namespace MediaBrowser.Api.HttpHandlers +{ + /// <summary> + /// Provides a handler to set played status for an item + /// </summary> + [Export(typeof(IHttpServerHandler))] + public class PlaybackCheckInHandler : BaseSerializationHandler<Kernel, DtoUserItemData> + { + /// <summary> + /// Gets the object to serialize. + /// </summary> + /// <returns>Task{DtoUserItemData}.</returns> + protected override async Task<DtoUserItemData> GetObjectToSerialize() + { + // Get the user + var user = await this.GetCurrentUser().ConfigureAwait(false); + + var clientType = ClientType.Other; + + if (!string.IsNullOrEmpty(QueryString["client"])) + { + ClientType type; + + if (Enum.TryParse(QueryString["client"], true, out type)) + { + clientType = type; + } + } + + var device = QueryString["device"]; + + // Get the item + var item = DtoBuilder.GetItemByClientId(QueryString["id"], user.Id); + + // Playback start check-in + if (QueryString["type"].Equals("start", StringComparison.OrdinalIgnoreCase)) + { + Kernel.UserDataManager.OnPlaybackStart(user, item, clientType, device); + } + else + { + long? positionTicks = null; + + if (!string.IsNullOrEmpty(QueryString["positionTicks"])) + { + positionTicks = long.Parse(QueryString["positionTicks"]); + } + + // Progress check-ins require position ticks + if (QueryString["type"].Equals("progress", StringComparison.OrdinalIgnoreCase)) + { + await Kernel.UserDataManager.OnPlaybackProgress(user, item, positionTicks, clientType, device).ConfigureAwait(false); + } + else if (QueryString["type"].Equals("stopped", StringComparison.OrdinalIgnoreCase)) + { + await Kernel.UserDataManager.OnPlaybackStopped(user, item, positionTicks, clientType, device).ConfigureAwait(false); + } + } + + var data = item.GetUserData(user, true); + + return DtoBuilder.GetDtoUserItemData(data); + } + + /// <summary> + /// Gets the current user. + /// </summary> + /// <returns>User.</returns> + /// <exception cref="System.UnauthorizedAccessException"></exception> + public async Task<User> GetCurrentUser() + { + var handler = this; + var id = handler.QueryString["userid"]; + + var user = ApiService.GetUserById(id); + + if (user == null) + { + throw new UnauthorizedAccessException(string.Format("User with Id {0} does not exist", id)); + } + + var clientType = ClientType.Other; + + if (!string.IsNullOrEmpty(handler.QueryString["client"])) + { + ClientType type; + + if (Enum.TryParse(handler.QueryString["client"], true, out type)) + { + clientType = type; + } + } + + var device = handler.QueryString["device"]; + + await Controller.Kernel.Instance.UserManager.LogUserActivity(user, clientType, device).ConfigureAwait(false); + + return user; + } + + } +}
\ No newline at end of file diff --git a/MediaBrowser.Api/HttpHandlers/PlayedStatusHandler.cs b/MediaBrowser.Api/HttpHandlers/PlayedStatusHandler.cs deleted file mode 100644 index c010bcb023..0000000000 --- a/MediaBrowser.Api/HttpHandlers/PlayedStatusHandler.cs +++ /dev/null @@ -1,38 +0,0 @@ -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 deleted file mode 100644 index 47f08c8c32..0000000000 --- a/MediaBrowser.Api/HttpHandlers/PluginAssemblyHandler.cs +++ /dev/null @@ -1,38 +0,0 @@ -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 deleted file mode 100644 index dc363956fd..0000000000 --- a/MediaBrowser.Api/HttpHandlers/PluginConfigurationHandler.cs +++ /dev/null @@ -1,53 +0,0 @@ -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 deleted file mode 100644 index a1b37ecaba..0000000000 --- a/MediaBrowser.Api/HttpHandlers/PluginsHandler.cs +++ /dev/null @@ -1,38 +0,0 @@ -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 deleted file mode 100644 index 48c6761b16..0000000000 --- a/MediaBrowser.Api/HttpHandlers/ServerConfigurationHandler.cs +++ /dev/null @@ -1,37 +0,0 @@ -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 deleted file mode 100644 index 6576e2cfe5..0000000000 --- a/MediaBrowser.Api/HttpHandlers/StudioHandler.cs +++ /dev/null @@ -1,57 +0,0 @@ -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 deleted file mode 100644 index 4377a0f432..0000000000 --- a/MediaBrowser.Api/HttpHandlers/StudiosHandler.cs +++ /dev/null @@ -1,78 +0,0 @@ -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/UpdateMediaLibraryHandler.cs b/MediaBrowser.Api/HttpHandlers/UpdateMediaLibraryHandler.cs new file mode 100644 index 0000000000..e5c42008e1 --- /dev/null +++ b/MediaBrowser.Api/HttpHandlers/UpdateMediaLibraryHandler.cs @@ -0,0 +1,263 @@ +using MediaBrowser.Common.IO; +using MediaBrowser.Common.Net.Handlers; +using MediaBrowser.Controller; +using MediaBrowser.Controller.Entities; +using System; +using System.ComponentModel.Composition; +using System.IO; +using System.Linq; +using System.Threading.Tasks; + +namespace MediaBrowser.Api.HttpHandlers +{ + /// <summary> + /// Makes changes to the user's media library + /// </summary> + [Export(typeof(IHttpServerHandler))] + public class UpdateMediaLibraryHandler : BaseActionHandler<Kernel> + { + /// <summary> + /// Executes the action. + /// </summary> + /// <returns>Task.</returns> + /// <exception cref="System.NotImplementedException"></exception> + protected override Task ExecuteAction() + { + return Task.Run(() => + { + var action = QueryString["action"]; + + if (string.IsNullOrEmpty(action)) + { + throw new ArgumentNullException(); + } + + User user = null; + + if (!string.IsNullOrEmpty(QueryString["userId"])) + { + user = ApiService.GetUserById(QueryString["userId"]); + } + + if (action.Equals("AddVirtualFolder", StringComparison.OrdinalIgnoreCase)) + { + AddVirtualFolder(Uri.UnescapeDataString(QueryString["name"]), user); + } + + if (action.Equals("RemoveVirtualFolder", StringComparison.OrdinalIgnoreCase)) + { + RemoveVirtualFolder(QueryString["name"], user); + } + + if (action.Equals("RenameVirtualFolder", StringComparison.OrdinalIgnoreCase)) + { + RenameVirtualFolder(QueryString["name"], QueryString["newName"], user); + } + + if (action.Equals("RemoveMediaPath", StringComparison.OrdinalIgnoreCase)) + { + RemoveMediaPath(QueryString["virtualFolderName"], QueryString["mediaPath"], user); + } + + if (action.Equals("AddMediaPath", StringComparison.OrdinalIgnoreCase)) + { + AddMediaPath(QueryString["virtualFolderName"], QueryString["mediaPath"], user); + } + + throw new ArgumentOutOfRangeException(); + }); + } + + /// <summary> + /// Adds a virtual folder to either the default view or a user view + /// </summary> + /// <param name="name">The name.</param> + /// <param name="user">The user.</param> + private void AddVirtualFolder(string name, User user) + { + name = FileSystem.GetValidFilename(name); + + var rootFolderPath = user != null ? user.RootFolderPath : Kernel.ApplicationPaths.DefaultUserViewsPath; + var virtualFolderPath = Path.Combine(rootFolderPath, name); + + if (Directory.Exists(virtualFolderPath)) + { + throw new ArgumentException("There is already a media collection with the name " + name + "."); + } + + Directory.CreateDirectory(virtualFolderPath); + } + + /// <summary> + /// Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view + /// </summary> + /// <param name="virtualFolderName">Name of the virtual folder.</param> + /// <param name="path">The path.</param> + /// <param name="user">The user.</param> + private void AddMediaPath(string virtualFolderName, string path, User user) + { + if (!Path.IsPathRooted(path)) + { + throw new ArgumentException("The path is not valid."); + } + + if (!Directory.Exists(path)) + { + throw new DirectoryNotFoundException("The path does not exist."); + } + + // Strip off trailing slash, but not on drives + path = path.TrimEnd(Path.DirectorySeparatorChar); + if (path.EndsWith(":", StringComparison.OrdinalIgnoreCase)) + { + path += Path.DirectorySeparatorChar; + } + + var rootFolderPath = user != null ? user.RootFolderPath : Kernel.ApplicationPaths.DefaultUserViewsPath; + var virtualFolderPath = Path.Combine(rootFolderPath, virtualFolderName); + + ValidateNewMediaPath(rootFolderPath, path); + + var shortcutFilename = Path.GetFileNameWithoutExtension(path); + + var lnk = Path.Combine(virtualFolderPath, shortcutFilename + ".lnk"); + + while (File.Exists(lnk)) + { + shortcutFilename += "1"; + lnk = Path.Combine(virtualFolderPath, shortcutFilename + ".lnk"); + } + + FileSystem.CreateShortcut(lnk, path); + } + + /// <summary> + /// Validates that a new media path can be added + /// </summary> + /// <param name="currentViewRootFolderPath">The current view root folder path.</param> + /// <param name="mediaPath">The media path.</param> + private void ValidateNewMediaPath(string currentViewRootFolderPath, string mediaPath) + { + var duplicate = Directory.EnumerateFiles(Kernel.ApplicationPaths.RootFolderPath, "*.lnk", SearchOption.AllDirectories) + .Select(FileSystem.ResolveShortcut) + .FirstOrDefault(p => !IsNewPathValid(mediaPath, p)); + + if (!string.IsNullOrEmpty(duplicate)) + { + throw new ArgumentException(string.Format("The path cannot be added to the library because {0} already exists.", duplicate)); + } + + // Make sure the current root folder doesn't already have a shortcut to the same path + duplicate = Directory.EnumerateFiles(currentViewRootFolderPath, "*.lnk", SearchOption.AllDirectories) + .Select(FileSystem.ResolveShortcut) + .FirstOrDefault(p => mediaPath.Equals(p, StringComparison.OrdinalIgnoreCase)); + + if (!string.IsNullOrEmpty(duplicate)) + { + throw new ArgumentException(string.Format("The path {0} already exists in the library", mediaPath)); + } + } + + /// <summary> + /// Validates that a new path can be added based on an existing path + /// </summary> + /// <param name="newPath">The new path.</param> + /// <param name="existingPath">The existing path.</param> + /// <returns><c>true</c> if [is new path valid] [the specified new path]; otherwise, <c>false</c>.</returns> + private bool IsNewPathValid(string newPath, string existingPath) + { + // Example: D:\Movies is the existing path + // D:\ cannot be added + // Neither can D:\Movies\Kids + // A D:\Movies duplicate is ok here since that will be caught later + + if (newPath.Equals(existingPath, StringComparison.OrdinalIgnoreCase)) + { + return true; + } + + // Validate the D:\Movies\Kids scenario + if (newPath.StartsWith(existingPath.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase)) + { + return false; + } + + // Validate the D:\ scenario + if (existingPath.StartsWith(newPath.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase)) + { + return false; + } + + return true; + } + + /// <summary> + /// Renames a virtual folder within either the default view or a user view + /// </summary> + /// <param name="name">The name.</param> + /// <param name="newName">The new name.</param> + /// <param name="user">The user.</param> + private void RenameVirtualFolder(string name, string newName, User user) + { + var rootFolderPath = user != null ? user.RootFolderPath : Kernel.ApplicationPaths.DefaultUserViewsPath; + + var currentPath = Path.Combine(rootFolderPath, name); + var newPath = Path.Combine(rootFolderPath, newName); + + if (!Directory.Exists(currentPath)) + { + throw new DirectoryNotFoundException("The media collection does not exist"); + } + + if (Directory.Exists(newPath)) + { + throw new ArgumentException("There is already a media collection with the name " + newPath + "."); + } + + Directory.Move(currentPath, newPath); + } + + /// <summary> + /// Deletes a virtual folder from either the default view or a user view + /// </summary> + /// <param name="name">The name.</param> + /// <param name="user">The user.</param> + private void RemoveVirtualFolder(string name, User user) + { + var rootFolderPath = user != null ? user.RootFolderPath : Kernel.ApplicationPaths.DefaultUserViewsPath; + var path = Path.Combine(rootFolderPath, name); + + if (!Directory.Exists(path)) + { + throw new DirectoryNotFoundException("The media folder does not exist"); + } + + Directory.Delete(path, true); + } + + /// <summary> + /// Deletes a shortcut from within a virtual folder, within either the default view or a user view + /// </summary> + /// <param name="virtualFolderName">Name of the virtual folder.</param> + /// <param name="mediaPath">The media path.</param> + /// <param name="user">The user.</param> + private void RemoveMediaPath(string virtualFolderName, string mediaPath, User user) + { + var rootFolderPath = user != null ? user.RootFolderPath : Kernel.ApplicationPaths.DefaultUserViewsPath; + var path = Path.Combine(rootFolderPath, virtualFolderName); + + if (!Directory.Exists(path)) + { + throw new DirectoryNotFoundException("The media folder does not exist"); + } + + var shortcut = Directory.EnumerateFiles(path, "*.lnk", SearchOption.AllDirectories).FirstOrDefault(f => FileSystem.ResolveShortcut(f).Equals(mediaPath, StringComparison.OrdinalIgnoreCase)); + + if (string.IsNullOrEmpty(shortcut)) + { + throw new DirectoryNotFoundException("The media folder does not exist"); + } + File.Delete(shortcut); + } + } +} diff --git a/MediaBrowser.Api/HttpHandlers/UserAuthenticationHandler.cs b/MediaBrowser.Api/HttpHandlers/UserAuthenticationHandler.cs deleted file mode 100644 index fa9d975983..0000000000 --- a/MediaBrowser.Api/HttpHandlers/UserAuthenticationHandler.cs +++ /dev/null @@ -1,29 +0,0 @@ -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 deleted file mode 100644 index bc92862040..0000000000 --- a/MediaBrowser.Api/HttpHandlers/UserHandler.cs +++ /dev/null @@ -1,29 +0,0 @@ -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 deleted file mode 100644 index aed0804b6d..0000000000 --- a/MediaBrowser.Api/HttpHandlers/UserItemRatingHandler.cs +++ /dev/null @@ -1,46 +0,0 @@ -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 deleted file mode 100644 index 3fc3a7d585..0000000000 --- a/MediaBrowser.Api/HttpHandlers/UsersHandler.cs +++ /dev/null @@ -1,25 +0,0 @@ -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 deleted file mode 100644 index e34a1b41f7..0000000000 --- a/MediaBrowser.Api/HttpHandlers/VideoHandler.cs +++ /dev/null @@ -1,424 +0,0 @@ -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 deleted file mode 100644 index 378e89067d..0000000000 --- a/MediaBrowser.Api/HttpHandlers/WeatherHandler.cs +++ /dev/null @@ -1,43 +0,0 @@ -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 deleted file mode 100644 index dbd1d25be0..0000000000 --- a/MediaBrowser.Api/HttpHandlers/YearHandler.cs +++ /dev/null @@ -1,55 +0,0 @@ -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 deleted file mode 100644 index 7c90768e84..0000000000 --- a/MediaBrowser.Api/HttpHandlers/YearsHandler.cs +++ /dev/null @@ -1,75 +0,0 @@ -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;
- }
- }
-}
|
