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/Library/LibraryHelpers.cs | 221 ++++++++++++++++ MediaBrowser.Api/Library/LibraryService.cs | 246 ++++++++++++++++++ .../Library/LibraryStructureService.cs | 278 +++++++++++++++++++++ 3 files changed, 745 insertions(+) create mode 100644 MediaBrowser.Api/Library/LibraryHelpers.cs create mode 100644 MediaBrowser.Api/Library/LibraryService.cs create mode 100644 MediaBrowser.Api/Library/LibraryStructureService.cs (limited to 'MediaBrowser.Api/Library') 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); + } + } + } +} -- cgit v1.2.3