From add43baffef74fcd34cfc6ef02d36777be05b274 Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Sun, 24 Feb 2013 22:56:00 -0500 Subject: convert media library url's to rest --- MediaBrowser.Api/ApiService.cs | 34 --- .../HttpHandlers/UpdateMediaLibraryHandler.cs | 261 ------------------- MediaBrowser.Api/Javascript/ApiClient.js | 78 ++---- MediaBrowser.Api/Library/LibraryHelpers.cs | 221 ++++++++++++++++ MediaBrowser.Api/Library/LibraryService.cs | 246 ++++++++++++++++++ .../Library/LibraryStructureService.cs | 278 +++++++++++++++++++++ MediaBrowser.Api/LibraryService.cs | 268 -------------------- MediaBrowser.Api/MediaBrowser.Api.csproj | 5 +- MediaBrowser.Api/UserLibrary/UserLibraryService.cs | 29 --- MediaBrowser.ApiInteraction/ApiClient.cs | 178 +------------ 10 files changed, 783 insertions(+), 815 deletions(-) delete mode 100644 MediaBrowser.Api/HttpHandlers/UpdateMediaLibraryHandler.cs create mode 100644 MediaBrowser.Api/Library/LibraryHelpers.cs create mode 100644 MediaBrowser.Api/Library/LibraryService.cs create mode 100644 MediaBrowser.Api/Library/LibraryStructureService.cs delete mode 100644 MediaBrowser.Api/LibraryService.cs diff --git a/MediaBrowser.Api/ApiService.cs b/MediaBrowser.Api/ApiService.cs index 68ba6d5225..e6e4594c16 100644 --- a/MediaBrowser.Api/ApiService.cs +++ b/MediaBrowser.Api/ApiService.cs @@ -1,10 +1,7 @@ using MediaBrowser.Controller; using MediaBrowser.Controller.Entities; -using MediaBrowser.Model.Connectivity; -using ServiceStack.Common.Web; using System; using System.Net; -using System.Threading.Tasks; namespace MediaBrowser.Api { @@ -43,36 +40,5 @@ namespace MediaBrowser.Api return request.Url.LocalPath.EndsWith(url, StringComparison.OrdinalIgnoreCase); } - - ///// - ///// Gets the current user. - ///// - ///// The request. - ///// Task{User}. - //public static async Task GetCurrentUser(AuthenticatedRequest request) - //{ - // var user = GetUserById(request.UserId); - - // if (user == null) - // { - // throw HttpError.Unauthorized("Invalid user or password entered."); - // } - - // var clientType = ClientType.Other; - - // if (!string.IsNullOrEmpty(request.Client)) - // { - // ClientType type; - - // if (Enum.TryParse(request.Client, true, out type)) - // { - // clientType = type; - // } - // } - - // await Kernel.Instance.UserManager.LogUserActivity(user, clientType, request.Device).ConfigureAwait(false); - - // return user; - //} } } diff --git a/MediaBrowser.Api/HttpHandlers/UpdateMediaLibraryHandler.cs b/MediaBrowser.Api/HttpHandlers/UpdateMediaLibraryHandler.cs deleted file mode 100644 index df6edd6f99..0000000000 --- a/MediaBrowser.Api/HttpHandlers/UpdateMediaLibraryHandler.cs +++ /dev/null @@ -1,261 +0,0 @@ -using MediaBrowser.Common.Net.Handlers; -using MediaBrowser.Controller; -using MediaBrowser.Controller.Entities; -using System; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using MediaBrowser.Controller.IO; - -namespace MediaBrowser.Api.HttpHandlers -{ - /// - /// Makes changes to the user's media library - /// - public class UpdateMediaLibraryHandler : BaseActionHandler - { - /// - /// Executes the action. - /// - /// Task. - /// - 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(); - }); - } - - /// - /// Adds a virtual folder to either the default view or a user view - /// - /// The name. - /// The user. - 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); - } - - /// - /// Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view - /// - /// Name of the virtual folder. - /// The path. - /// The user. - 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); - } - - /// - /// Validates that a new media path can be added - /// - /// The current view root folder path. - /// The media path. - 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)); - } - } - - /// - /// Validates that a new path can be added based on an existing path - /// - /// The new path. - /// The existing path. - /// true if [is new path valid] [the specified new path]; otherwise, false. - 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; - } - - /// - /// Renames a virtual folder within either the default view or a user view - /// - /// The name. - /// The new name. - /// The user. - 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); - } - - /// - /// Deletes a virtual folder from either the default view or a user view - /// - /// The name. - /// The user. - 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); - } - - /// - /// Deletes a shortcut from within a virtual folder, within either the default view or a user view - /// - /// Name of the virtual folder. - /// The media path. - /// The user. - 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/Javascript/ApiClient.js b/MediaBrowser.Api/Javascript/ApiClient.js index df1c8f2c01..e82238d24c 100644 --- a/MediaBrowser.Api/Javascript/ApiClient.js +++ b/MediaBrowser.Api/Javascript/ApiClient.js @@ -297,7 +297,7 @@ var ApiClient = { */ getVirtualFolders: function (userId) { - var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/DefaultVirtualFolders"; + var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders"; url = ApiClient.getUrl(url); @@ -434,18 +434,16 @@ var ApiClient = { throw new Error("null name"); } - var params = { - name: name, - action: "RemoveVirtualFolder" - }; - - if (userId) { - params.userId = userId; - } + var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders"; - var url = ApiClient.getUrl("UpdateMediaLibrary", params); + url += "/" + name; + url = ApiClient.getUrl(url); - return $.post(url); + return $.ajax({ + type: "DELETE", + url: url, + dataType: "json" + }); }, /** @@ -458,16 +456,10 @@ var ApiClient = { throw new Error("null name"); } - var params = { - name: name, - action: "addVirtualFolder" - }; + var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders"; - if (userId) { - params.userId = userId; - } - - var url = ApiClient.getUrl("UpdateMediaLibrary", params); + url += "/" + name; + url = ApiClient.getUrl(url); return $.post(url); }, @@ -482,21 +474,11 @@ var ApiClient = { throw new Error("null name"); } - if (!newName) { - throw new Error("null newName"); - } - - var params = { - name: name, - newName: newName, - action: "RenameVirtualFolder" - }; + var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders"; - if (userId) { - params.userId = userId; - } + url += "/" + name + "/Name"; - var url = ApiClient.getUrl("UpdateMediaLibrary", params); + url = ApiClient.getUrl(url, { newName: newName }); return $.post(url); }, @@ -515,17 +497,11 @@ var ApiClient = { throw new Error("null mediaPath"); } - var params = { - virtualFolderName: virtualFolderName, - mediaPath: mediaPath, - action: "addMediaPath" - }; + var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders"; - if (userId) { - params.userId = userId; - } + url += "/" + virtualFolderName + "/Paths"; - var url = ApiClient.getUrl("UpdateMediaLibrary", params); + url = ApiClient.getUrl(url, { path: mediaPath }); return $.post(url); }, @@ -544,19 +520,17 @@ var ApiClient = { throw new Error("null mediaPath"); } - var params = { - virtualFolderName: virtualFolderName, - mediaPath: mediaPath, - action: "RemoveMediaPath" - }; + var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders"; - if (userId) { - params.userId = userId; - } + url += "/" + virtualFolderName + "/Paths"; - var url = ApiClient.getUrl("UpdateMediaLibrary", params); + url = ApiClient.getUrl(url, { path: mediaPath }); - return $.post(url); + return $.ajax({ + type: "DELETE", + url: url, + dataType: "json" + }); }, /** diff --git a/MediaBrowser.Api/Library/LibraryHelpers.cs b/MediaBrowser.Api/Library/LibraryHelpers.cs new file mode 100644 index 0000000000..2a3a04537e --- /dev/null +++ b/MediaBrowser.Api/Library/LibraryHelpers.cs @@ -0,0 +1,221 @@ +using MediaBrowser.Controller; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.IO; +using System; +using System.IO; +using System.Linq; + +namespace MediaBrowser.Api.Library +{ + /// + /// Class LibraryHelpers + /// + public static class LibraryHelpers + { + /// + /// Adds the virtual folder. + /// + /// The name. + /// The user. + /// The app paths. + /// There is already a media collection with the name + name + . + public static void AddVirtualFolder(string name, User user, IServerApplicationPaths appPaths) + { + name = FileSystem.GetValidFilename(name); + + var rootFolderPath = user != null ? user.RootFolderPath : appPaths.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); + } + + /// + /// Removes the virtual folder. + /// + /// The name. + /// The user. + /// The app paths. + /// The media folder does not exist + public static void RemoveVirtualFolder(string name, User user, IServerApplicationPaths appPaths) + { + var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath; + var path = Path.Combine(rootFolderPath, name); + + if (!Directory.Exists(path)) + { + throw new DirectoryNotFoundException("The media folder does not exist"); + } + + Directory.Delete(path, true); + } + + /// + /// Renames the virtual folder. + /// + /// The name. + /// The new name. + /// The user. + /// The app paths. + /// The media collection does not exist + /// There is already a media collection with the name + newPath + . + public static void RenameVirtualFolder(string name, string newName, User user, IServerApplicationPaths appPaths) + { + var rootFolderPath = user != null ? user.RootFolderPath : appPaths.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); + } + + /// + /// Deletes a shortcut from within a virtual folder, within either the default view or a user view + /// + /// Name of the virtual folder. + /// The media path. + /// The user. + /// The app paths. + /// The media folder does not exist + public static void RemoveMediaPath(string virtualFolderName, string mediaPath, User user, IServerApplicationPaths appPaths) + { + var rootFolderPath = user != null ? user.RootFolderPath : appPaths.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); + } + + /// + /// Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view + /// + /// Name of the virtual folder. + /// The path. + /// The user. + /// The app paths. + /// The path is not valid. + /// The path does not exist. + public static void AddMediaPath(string virtualFolderName, string path, User user, IServerApplicationPaths appPaths) + { + 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 : appPaths.DefaultUserViewsPath; + var virtualFolderPath = Path.Combine(rootFolderPath, virtualFolderName); + + ValidateNewMediaPath(rootFolderPath, path, appPaths); + + 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); + } + + /// + /// Validates that a new media path can be added + /// + /// The current view root folder path. + /// The media path. + /// The app paths. + /// + private static void ValidateNewMediaPath(string currentViewRootFolderPath, string mediaPath, IServerApplicationPaths appPaths) + { + var duplicate = Directory.EnumerateFiles(appPaths.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)); + } + } + + /// + /// Validates that a new path can be added based on an existing path + /// + /// The new path. + /// The existing path. + /// true if [is new path valid] [the specified new path]; otherwise, false. + private static 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; + } + } +} diff --git a/MediaBrowser.Api/Library/LibraryService.cs b/MediaBrowser.Api/Library/LibraryService.cs new file mode 100644 index 0000000000..0729987d9e --- /dev/null +++ b/MediaBrowser.Api/Library/LibraryService.cs @@ -0,0 +1,246 @@ +using MediaBrowser.Common.Kernel; +using MediaBrowser.Common.Net; +using MediaBrowser.Controller; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Model.Dto; +using MediaBrowser.Model.Entities; +using ServiceStack.ServiceHost; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace MediaBrowser.Api.Library +{ + /// + /// Class GetPhyscialPaths + /// + [Route("/Library/PhysicalPaths", "GET")] + public class GetPhyscialPaths : IReturn> + { + } + + /// + /// Class GetItemTypes + /// + [Route("/Library/ItemTypes", "GET")] + public class GetItemTypes : IReturn> + { + /// + /// Gets or sets a value indicating whether this instance has internet provider. + /// + /// true if this instance has internet provider; otherwise, false. + public bool HasInternetProvider { get; set; } + } + + /// + /// Class GetPerson + /// + [Route("/Library/Persons/{Name}", "GET")] + public class GetPerson : IReturn + { + /// + /// Gets or sets the name. + /// + /// The name. + public string Name { get; set; } + } + + /// + /// Class GetStudio + /// + [Route("/Library/Studios/{Name}", "GET")] + public class GetStudio : IReturn + { + /// + /// Gets or sets the name. + /// + /// The name. + public string Name { get; set; } + } + + /// + /// Class GetGenre + /// + [Route("/Library/Genres/{Name}", "GET")] + public class GetGenre : IReturn + { + /// + /// Gets or sets the name. + /// + /// The name. + public string Name { get; set; } + } + + /// + /// Class GetYear + /// + [Route("/Library/Years/{Year}", "GET")] + public class GetYear : IReturn + { + /// + /// Gets or sets the year. + /// + /// The year. + public int Year { get; set; } + } + + /// + /// Class LibraryService + /// + public class LibraryService : BaseRestService + { + /// + /// The _app host + /// + private readonly IApplicationHost _appHost; + + /// + /// Initializes a new instance of the class. + /// + /// The app host. + /// appHost + public LibraryService(IApplicationHost appHost) + { + if (appHost == null) + { + throw new ArgumentNullException("appHost"); + } + + _appHost = appHost; + } + + /// + /// Gets the specified request. + /// + /// The request. + /// System.Object. + public object Get(GetPerson request) + { + var kernel = (Kernel)Kernel; + + var item = kernel.LibraryManager.GetPerson(request.Name).Result; + + // Get everything + var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)); + + var result = new DtoBuilder(Logger).GetDtoBaseItem(item, fields.ToList()).Result; + + return ToOptimizedResult(result); + } + + /// + /// Gets the specified request. + /// + /// The request. + /// System.Object. + public object Get(GetGenre request) + { + var kernel = (Kernel)Kernel; + + var item = kernel.LibraryManager.GetGenre(request.Name).Result; + + // Get everything + var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)); + + var result = new DtoBuilder(Logger).GetDtoBaseItem(item, fields.ToList()).Result; + + return ToOptimizedResult(result); + } + + /// + /// Gets the specified request. + /// + /// The request. + /// System.Object. + public object Get(GetStudio request) + { + var kernel = (Kernel)Kernel; + + var item = kernel.LibraryManager.GetStudio(request.Name).Result; + + // Get everything + var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)); + + var result = new DtoBuilder(Logger).GetDtoBaseItem(item, fields.ToList()).Result; + + return ToOptimizedResult(result); + } + + /// + /// Gets the specified request. + /// + /// The request. + /// System.Object. + public object Get(GetYear request) + { + var kernel = (Kernel)Kernel; + + var item = kernel.LibraryManager.GetYear(request.Year).Result; + + // Get everything + var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)); + + var result = new DtoBuilder(Logger).GetDtoBaseItem(item, fields.ToList()).Result; + + return ToOptimizedResult(result); + } + + /// + /// Gets the specified request. + /// + /// The request. + /// System.Object. + public object Get(GetPhyscialPaths request) + { + var kernel = (Kernel)Kernel; + + var result = kernel.RootFolder.Children.SelectMany(c => c.ResolveArgs.PhysicalLocations).ToList(); + + return ToOptimizedResult(result); + } + + /// + /// Gets the specified request. + /// + /// The request. + /// System.Object. + public object Get(GetItemTypes request) + { + var kernel = (Kernel)Kernel; + + var allTypes = _appHost.AllConcreteTypes.Where(t => t.IsSubclassOf(typeof(BaseItem))); + + if (request.HasInternetProvider) + { + allTypes = allTypes.Where(t => + { + if (t == typeof(UserRootFolder) || t == typeof(AggregateFolder) || t == typeof(Folder) || t == typeof(IndexFolder) || t == typeof(CollectionFolder) || t == typeof(Year)) + { + return false; + } + + if (t == typeof(User)) + { + return false; + } + + // For now it seems internet providers generally only deal with video subclasses + if (t == typeof(Video)) + { + return false; + } + + if (t.IsSubclassOf(typeof(BasePluginFolder))) + { + return false; + } + + return true; + }); + } + + return allTypes.Select(t => t.Name).OrderBy(s => s).ToList(); + } + } +} diff --git a/MediaBrowser.Api/Library/LibraryStructureService.cs b/MediaBrowser.Api/Library/LibraryStructureService.cs new file mode 100644 index 0000000000..35c658f912 --- /dev/null +++ b/MediaBrowser.Api/Library/LibraryStructureService.cs @@ -0,0 +1,278 @@ +using MediaBrowser.Common.Net; +using MediaBrowser.Controller; +using MediaBrowser.Model.Entities; +using ServiceStack.ServiceHost; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace MediaBrowser.Api.Library +{ + /// + /// Class GetDefaultVirtualFolders + /// + [Route("/Library/VirtualFolders", "GET")] + [Route("/Users/{UserId}/VirtualFolders", "GET")] + public class GetVirtualFolders : IReturn> + { + /// + /// Gets or sets the user id. + /// + /// The user id. + public string UserId { get; set; } + } + + [Route("/Library/VirtualFolders/{Name}", "POST")] + [Route("/Users/{UserId}/VirtualFolders/{Name}", "POST")] + public class AddVirtualFolder : IReturnVoid + { + /// + /// Gets or sets the user id. + /// + /// The user id. + public string UserId { get; set; } + + /// + /// Gets or sets the name. + /// + /// The name. + public string Name { get; set; } + } + + [Route("/Library/VirtualFolders/{Name}", "DELETE")] + [Route("/Users/{UserId}/VirtualFolders/{Name}", "DELETE")] + public class RemoveVirtualFolder : IReturnVoid + { + /// + /// Gets or sets the user id. + /// + /// The user id. + public string UserId { get; set; } + + /// + /// Gets or sets the name. + /// + /// The name. + public string Name { get; set; } + } + + [Route("/Library/VirtualFolders/{Name}/Name", "POST")] + [Route("/Users/{UserId}/VirtualFolders/{Name}/Name", "POST")] + public class RenameVirtualFolder : IReturnVoid + { + /// + /// Gets or sets the user id. + /// + /// The user id. + public string UserId { get; set; } + + /// + /// Gets or sets the name. + /// + /// The name. + public string Name { get; set; } + + /// + /// Gets or sets the name. + /// + /// The name. + public string NewName { get; set; } + } + + [Route("/Library/VirtualFolders/{Name}/Paths", "POST")] + [Route("/Users/{UserId}/VirtualFolders/{Name}/Paths", "POST")] + public class AddMediaPath : IReturnVoid + { + /// + /// Gets or sets the user id. + /// + /// The user id. + public string UserId { get; set; } + + /// + /// Gets or sets the name. + /// + /// The name. + public string Name { get; set; } + + /// + /// Gets or sets the name. + /// + /// The name. + public string Path { get; set; } + } + + [Route("/Library/VirtualFolders/{Name}/Paths", "DELETE")] + [Route("/Users/{UserId}/VirtualFolders/{Name}/Paths", "DELETE")] + public class RemoveMediaPath : IReturnVoid + { + /// + /// Gets or sets the user id. + /// + /// The user id. + public string UserId { get; set; } + + /// + /// Gets or sets the name. + /// + /// The name. + public string Name { get; set; } + + /// + /// Gets or sets the name. + /// + /// The name. + public string Path { get; set; } + } + + /// + /// Class LibraryStructureService + /// + public class LibraryStructureService : BaseRestService + { + /// + /// The _app paths + /// + private readonly IServerApplicationPaths _appPaths; + + /// + /// Initializes a new instance of the class. + /// + /// The app paths. + /// appHost + public LibraryStructureService(IServerApplicationPaths appPaths) + { + if (appPaths == null) + { + throw new ArgumentNullException("appPaths"); + } + + _appPaths = appPaths; + } + + /// + /// Gets the specified request. + /// + /// The request. + /// System.Object. + public object Get(GetVirtualFolders request) + { + var kernel = (Kernel)Kernel; + + if (string.IsNullOrEmpty(request.UserId)) + { + var result = kernel.LibraryManager.GetDefaultVirtualFolders().ToList(); + + return ToOptimizedResult(result); + } + else + { + var user = kernel.GetUserById(new Guid(request.UserId)); + + var result = kernel.LibraryManager.GetVirtualFolders(user).ToList(); + + return ToOptimizedResult(result); + } + } + + /// + /// Posts the specified request. + /// + /// The request. + public void Post(AddVirtualFolder request) + { + var kernel = (Kernel)Kernel; + + if (string.IsNullOrEmpty(request.UserId)) + { + LibraryHelpers.AddVirtualFolder(request.Name, null, _appPaths); + } + else + { + var user = kernel.GetUserById(new Guid(request.UserId)); + + LibraryHelpers.AddVirtualFolder(request.Name, user, _appPaths); + } + } + + /// + /// Posts the specified request. + /// + /// The request. + public void Post(RenameVirtualFolder request) + { + var kernel = (Kernel)Kernel; + + if (string.IsNullOrEmpty(request.UserId)) + { + LibraryHelpers.RenameVirtualFolder(request.Name, request.NewName, null, _appPaths); + } + else + { + var user = kernel.GetUserById(new Guid(request.UserId)); + + LibraryHelpers.RenameVirtualFolder(request.Name, request.NewName, user, _appPaths); + } + } + + /// + /// Deletes the specified request. + /// + /// The request. + public void Delete(RemoveVirtualFolder request) + { + var kernel = (Kernel)Kernel; + + if (string.IsNullOrEmpty(request.UserId)) + { + LibraryHelpers.RemoveVirtualFolder(request.Name, null, _appPaths); + } + else + { + var user = kernel.GetUserById(new Guid(request.UserId)); + + LibraryHelpers.RemoveVirtualFolder(request.Name, user, _appPaths); + } + } + + /// + /// Posts the specified request. + /// + /// The request. + public void Post(AddMediaPath request) + { + var kernel = (Kernel)Kernel; + + if (string.IsNullOrEmpty(request.UserId)) + { + LibraryHelpers.AddMediaPath(request.Name, request.Path, null, _appPaths); + } + else + { + var user = kernel.GetUserById(new Guid(request.UserId)); + + LibraryHelpers.AddMediaPath(request.Name, request.Path, user, _appPaths); + } + } + + /// + /// Deletes the specified request. + /// + /// The request. + public void Delete(RemoveMediaPath request) + { + var kernel = (Kernel)Kernel; + + if (string.IsNullOrEmpty(request.UserId)) + { + LibraryHelpers.RemoveMediaPath(request.Name, request.Path, null, _appPaths); + } + else + { + var user = kernel.GetUserById(new Guid(request.UserId)); + + LibraryHelpers.RemoveMediaPath(request.Name, request.Path, user, _appPaths); + } + } + } +} diff --git a/MediaBrowser.Api/LibraryService.cs b/MediaBrowser.Api/LibraryService.cs deleted file mode 100644 index 434b04eff9..0000000000 --- a/MediaBrowser.Api/LibraryService.cs +++ /dev/null @@ -1,268 +0,0 @@ -using MediaBrowser.Common.Kernel; -using MediaBrowser.Common.Net; -using MediaBrowser.Controller; -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Library; -using MediaBrowser.Model.Dto; -using MediaBrowser.Model.Entities; -using ServiceStack.ServiceHost; -using System; -using System.Collections.Generic; -using System.Linq; - -namespace MediaBrowser.Api -{ - /// - /// Class GetPhyscialPaths - /// - [Route("/Library/PhysicalPaths", "GET")] - public class GetPhyscialPaths : IReturn> - { - } - - /// - /// Class GetItemTypes - /// - [Route("/Library/ItemTypes", "GET")] - public class GetItemTypes : IReturn> - { - /// - /// Gets or sets a value indicating whether this instance has internet provider. - /// - /// true if this instance has internet provider; otherwise, false. - public bool HasInternetProvider { get; set; } - } - - /// - /// Class GetPerson - /// - [Route("/Library/Persons/{Name}", "GET")] - public class GetPerson : IReturn - { - /// - /// Gets or sets the name. - /// - /// The name. - public string Name { get; set; } - } - - /// - /// Class GetStudio - /// - [Route("/Library/Studios/{Name}", "GET")] - public class GetStudio : IReturn - { - /// - /// Gets or sets the name. - /// - /// The name. - public string Name { get; set; } - } - - /// - /// Class GetGenre - /// - [Route("/Library/Genres/{Name}", "GET")] - public class GetGenre : IReturn - { - /// - /// Gets or sets the name. - /// - /// The name. - public string Name { get; set; } - } - - /// - /// Class GetYear - /// - [Route("/Library/Years/{Year}", "GET")] - public class GetYear : IReturn - { - /// - /// Gets or sets the year. - /// - /// The year. - public int Year { get; set; } - } - - /// - /// Class GetDefaultVirtualFolders - /// - [Route("/Library/DefaultVirtualFolders", "GET")] - public class GetDefaultVirtualFolders : IReturn> - { - } - - /// - /// Class LibraryService - /// - public class LibraryService : BaseRestService - { - /// - /// The _app host - /// - private readonly IApplicationHost _appHost; - - /// - /// Initializes a new instance of the class. - /// - /// The app host. - /// appHost - public LibraryService(IApplicationHost appHost) - { - if (appHost == null) - { - throw new ArgumentNullException("appHost"); - } - - _appHost = appHost; - } - - /// - /// Gets the specified request. - /// - /// The request. - /// System.Object. - public object Get(GetDefaultVirtualFolders request) - { - var kernel = (Kernel)Kernel; - - var result = kernel.LibraryManager.GetDefaultVirtualFolders().ToList(); - - return ToOptimizedResult(result); - } - - /// - /// Gets the specified request. - /// - /// The request. - /// System.Object. - public object Get(GetPerson request) - { - var kernel = (Kernel)Kernel; - - var item = kernel.LibraryManager.GetPerson(request.Name).Result; - - // Get everything - var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)); - - var result = new DtoBuilder(Logger).GetDtoBaseItem(item, fields.ToList()).Result; - - return ToOptimizedResult(result); - } - - /// - /// Gets the specified request. - /// - /// The request. - /// System.Object. - public object Get(GetGenre request) - { - var kernel = (Kernel)Kernel; - - var item = kernel.LibraryManager.GetGenre(request.Name).Result; - - // Get everything - var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)); - - var result = new DtoBuilder(Logger).GetDtoBaseItem(item, fields.ToList()).Result; - - return ToOptimizedResult(result); - } - - /// - /// Gets the specified request. - /// - /// The request. - /// System.Object. - public object Get(GetStudio request) - { - var kernel = (Kernel)Kernel; - - var item = kernel.LibraryManager.GetStudio(request.Name).Result; - - // Get everything - var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)); - - var result = new DtoBuilder(Logger).GetDtoBaseItem(item, fields.ToList()).Result; - - return ToOptimizedResult(result); - } - - /// - /// Gets the specified request. - /// - /// The request. - /// System.Object. - public object Get(GetYear request) - { - var kernel = (Kernel)Kernel; - - var item = kernel.LibraryManager.GetYear(request.Year).Result; - - // Get everything - var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)); - - var result = new DtoBuilder(Logger).GetDtoBaseItem(item, fields.ToList()).Result; - - return ToOptimizedResult(result); - } - - /// - /// Gets the specified request. - /// - /// The request. - /// System.Object. - public object Get(GetPhyscialPaths request) - { - var kernel = (Kernel)Kernel; - - var result = kernel.RootFolder.Children.SelectMany(c => c.ResolveArgs.PhysicalLocations).ToList(); - - return ToOptimizedResult(result); - } - - /// - /// Gets the specified request. - /// - /// The request. - /// System.Object. - public object Get(GetItemTypes request) - { - var kernel = (Kernel)Kernel; - - var allTypes = _appHost.AllConcreteTypes.Where(t => t.IsSubclassOf(typeof(BaseItem))); - - if (request.HasInternetProvider) - { - allTypes = allTypes.Where(t => - { - if (t == typeof(UserRootFolder) || t == typeof(AggregateFolder) || t == typeof(Folder) || t == typeof(IndexFolder) || t == typeof(CollectionFolder) || t == typeof(Year)) - { - return false; - } - - if (t == typeof(User)) - { - return false; - } - - // For now it seems internet providers generally only deal with video subclasses - if (t == typeof(Video)) - { - return false; - } - - if (t.IsSubclassOf(typeof(BasePluginFolder))) - { - return false; - } - - return true; - }); - } - - return allTypes.Select(t => t.Name).OrderBy(s => s).ToList(); - } - } -} diff --git a/MediaBrowser.Api/MediaBrowser.Api.csproj b/MediaBrowser.Api/MediaBrowser.Api.csproj index fb7fe0b295..81ff1001be 100644 --- a/MediaBrowser.Api/MediaBrowser.Api.csproj +++ b/MediaBrowser.Api/MediaBrowser.Api.csproj @@ -80,7 +80,9 @@ - + + + @@ -102,7 +104,6 @@ - diff --git a/MediaBrowser.Api/UserLibrary/UserLibraryService.cs b/MediaBrowser.Api/UserLibrary/UserLibraryService.cs index 4ec2aca492..51197affb8 100644 --- a/MediaBrowser.Api/UserLibrary/UserLibraryService.cs +++ b/MediaBrowser.Api/UserLibrary/UserLibraryService.cs @@ -83,19 +83,6 @@ namespace MediaBrowser.Api.UserLibrary public Stream RequestStream { get; set; } } - /// - /// Class GetVirtualFolders - /// - [Route("/Users/{UserId}/VirtualFolders", "GET")] - public class GetVirtualFolders : IReturn> - { - /// - /// Gets or sets the user id. - /// - /// The user id. - public Guid UserId { get; set; } - } - /// /// Class MarkFavoriteItem /// @@ -412,22 +399,6 @@ namespace MediaBrowser.Api.UserLibrary return ToOptimizedResult(result); } - /// - /// Gets the specified request. - /// - /// The request. - /// System.Object. - public object Get(GetVirtualFolders request) - { - var kernel = (Kernel)Kernel; - - var user = kernel.GetUserById(request.UserId); - - var result = kernel.LibraryManager.GetVirtualFolders(user).ToList(); - - return ToOptimizedResult(result); - } - /// /// Gets the specified request. /// diff --git a/MediaBrowser.ApiInteraction/ApiClient.cs b/MediaBrowser.ApiInteraction/ApiClient.cs index 4f701532b5..e7d4e6eff1 100644 --- a/MediaBrowser.ApiInteraction/ApiClient.cs +++ b/MediaBrowser.ApiInteraction/ApiClient.cs @@ -658,7 +658,7 @@ namespace MediaBrowser.ApiInteraction /// The user id. /// Task{UserItemDataDto}. /// itemId - public Task ReportPlaybackStartAsync(string itemId, Guid userId) + public Task ReportPlaybackStartAsync(string itemId, Guid userId) { if (string.IsNullOrEmpty(itemId)) { @@ -670,14 +670,9 @@ namespace MediaBrowser.ApiInteraction throw new ArgumentNullException("userId"); } - var dict = new QueryStringDictionary(); - dict.Add("id", itemId); - dict.Add("userId", userId); - dict.Add("type", "start"); - - var url = GetApiUrl("playbackcheckin", dict); + var url = GetApiUrl("Users/" + userId + "/PlayingItems/" + itemId); - return PostAsync(url, new Dictionary()); + return PostAsync(url, new Dictionary()); } /// @@ -688,7 +683,7 @@ namespace MediaBrowser.ApiInteraction /// The position ticks. /// Task{UserItemDataDto}. /// itemId - public Task ReportPlaybackProgressAsync(string itemId, Guid userId, long? positionTicks) + public Task ReportPlaybackProgressAsync(string itemId, Guid userId, long? positionTicks) { if (string.IsNullOrEmpty(itemId)) { @@ -701,15 +696,11 @@ namespace MediaBrowser.ApiInteraction } var dict = new QueryStringDictionary(); - dict.Add("id", itemId); - dict.Add("userId", userId); - dict.Add("type", "progress"); - dict.AddIfNotNull("positionTicks", positionTicks); - var url = GetApiUrl("playbackcheckin", dict); + var url = GetApiUrl("Users/" + userId + "/PlayingItems/" + itemId + "/Progress", dict); - return PostAsync(url, new Dictionary()); + return PostAsync(url, new Dictionary()); } /// @@ -720,7 +711,7 @@ namespace MediaBrowser.ApiInteraction /// The position ticks. /// Task{UserItemDataDto}. /// itemId - public Task ReportPlaybackStoppedAsync(string itemId, Guid userId, long? positionTicks) + public Task ReportPlaybackStoppedAsync(string itemId, Guid userId, long? positionTicks) { if (string.IsNullOrEmpty(itemId)) { @@ -733,15 +724,11 @@ namespace MediaBrowser.ApiInteraction } var dict = new QueryStringDictionary(); - dict.Add("id", itemId); - dict.Add("userId", userId); - dict.Add("type", "stopped"); - dict.AddIfNotNull("positionTicks", positionTicks); - var url = GetApiUrl("playbackcheckin", dict); + var url = GetApiUrl("Users/" + userId + "/PlayingItems/" + itemId, dict); - return PostAsync(url, new Dictionary()); + return HttpClient.DeleteAsync(url, Logger, CancellationToken.None); } /// @@ -878,153 +865,6 @@ namespace MediaBrowser.ApiInteraction return PostAsync(url, triggers); } - /// - /// Adds a virtual folder to either the default view or a user view - /// - /// The name. - /// The user id. - /// Task{RequestResult}. - /// name - public Task AddVirtualFolderAsync(string name, Guid? userId = null) - { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentNullException("name"); - } - - var dict = new QueryStringDictionary(); - dict.Add("name", name); - dict.Add("action", "AddVirtualFolder"); - - dict.AddIfNotNull("userId", userId); - - var url = GetApiUrl("UpdateMediaLibrary", dict); - - return PostAsync(url, new Dictionary()); - } - - /// - /// Removes a virtual folder, within either the default view or a user view - /// - /// The name. - /// The user id. - /// Task. - /// name - public Task RemoveVirtualFolderAsync(string name, Guid? userId = null) - { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentNullException("name"); - } - - var dict = new QueryStringDictionary(); - dict.Add("name", name); - dict.Add("action", "RemoveVirtualFolder"); - - dict.AddIfNotNull("userId", userId); - - var url = GetApiUrl("UpdateMediaLibrary", dict); - - return PostAsync(url, new Dictionary()); - } - - /// - /// Renames a virtual folder, within either the default view or a user view - /// - /// The name. - /// The new name. - /// The user id. - /// Task. - /// name - public Task RenameVirtualFolderAsync(string name, string newName, Guid? userId = null) - { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentNullException("name"); - } - - if (string.IsNullOrEmpty(newName)) - { - throw new ArgumentNullException("newName"); - } - - var dict = new QueryStringDictionary(); - dict.Add("name", name); - dict.Add("newName", newName); - dict.Add("action", "RenameVirtualFolder"); - - dict.AddIfNotNull("userId", userId); - - var url = GetApiUrl("UpdateMediaLibrary", dict); - - return PostAsync(url, new Dictionary()); - } - - /// - /// Adds a media path to a virtual folder, within either the default view or a user view - /// - /// Name of the virtual folder. - /// The media path. - /// The user id. - /// Task. - /// virtualFolderName - public Task AddMediaPathAsync(string virtualFolderName, string mediaPath, Guid? userId = null) - { - if (string.IsNullOrEmpty(virtualFolderName)) - { - throw new ArgumentNullException("virtualFolderName"); - } - - if (string.IsNullOrEmpty(mediaPath)) - { - throw new ArgumentNullException("mediaPath"); - } - - var dict = new QueryStringDictionary(); - dict.Add("virtualFolderName", virtualFolderName); - dict.Add("mediaPath", mediaPath); - dict.Add("action", "AddMediaPath"); - - dict.AddIfNotNull("userId", userId); - - var url = GetApiUrl("UpdateMediaLibrary", dict); - - return PostAsync(url, new Dictionary()); - } - - /// - /// Removes a media path from a virtual folder, within either the default view or a user view - /// - /// Name of the virtual folder. - /// The media path. - /// The user id. - /// Task. - /// virtualFolderName - public Task RemoveMediaPathAsync(string virtualFolderName, string mediaPath, Guid? userId = null) - { - if (string.IsNullOrEmpty(virtualFolderName)) - { - throw new ArgumentNullException("virtualFolderName"); - } - - if (string.IsNullOrEmpty(mediaPath)) - { - throw new ArgumentNullException("mediaPath"); - } - - var dict = new QueryStringDictionary(); - - dict.Add("virtualFolderName", virtualFolderName); - dict.Add("mediaPath", mediaPath); - dict.Add("action", "RemoveMediaPath"); - - dict.AddIfNotNull("userId", userId); - - var url = GetApiUrl("UpdateMediaLibrary", dict); - - return PostAsync(url, new Dictionary()); - } - /// /// Updates display preferences for a user /// -- cgit v1.2.3