diff options
| author | LukePulverenti <luke.pulverenti@gmail.com> | 2013-02-24 22:56:00 -0500 |
|---|---|---|
| committer | LukePulverenti <luke.pulverenti@gmail.com> | 2013-02-24 22:56:00 -0500 |
| commit | add43baffef74fcd34cfc6ef02d36777be05b274 (patch) | |
| tree | d099801de1b457e3193f5b29b68337ff50649cd8 /MediaBrowser.Api/Library | |
| parent | 2d342c02ef55e2ba8796d95888274356aaadbe5c (diff) | |
convert media library url's to rest
Diffstat (limited to 'MediaBrowser.Api/Library')
| -rw-r--r-- | MediaBrowser.Api/Library/LibraryHelpers.cs | 221 | ||||
| -rw-r--r-- | MediaBrowser.Api/Library/LibraryService.cs | 246 | ||||
| -rw-r--r-- | MediaBrowser.Api/Library/LibraryStructureService.cs | 278 |
3 files changed, 745 insertions, 0 deletions
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 +{ + /// <summary> + /// Class LibraryHelpers + /// </summary> + public static class LibraryHelpers + { + /// <summary> + /// Adds the virtual folder. + /// </summary> + /// <param name="name">The name.</param> + /// <param name="user">The user.</param> + /// <param name="appPaths">The app paths.</param> + /// <exception cref="System.ArgumentException">There is already a media collection with the name + name + .</exception> + 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); + } + + /// <summary> + /// Removes the virtual folder. + /// </summary> + /// <param name="name">The name.</param> + /// <param name="user">The user.</param> + /// <param name="appPaths">The app paths.</param> + /// <exception cref="System.IO.DirectoryNotFoundException">The media folder does not exist</exception> + 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); + } + + /// <summary> + /// Renames the virtual folder. + /// </summary> + /// <param name="name">The name.</param> + /// <param name="newName">The new name.</param> + /// <param name="user">The user.</param> + /// <param name="appPaths">The app paths.</param> + /// <exception cref="System.IO.DirectoryNotFoundException">The media collection does not exist</exception> + /// <exception cref="System.ArgumentException">There is already a media collection with the name + newPath + .</exception> + 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); + } + + /// <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> + /// <param name="appPaths">The app paths.</param> + /// <exception cref="System.IO.DirectoryNotFoundException">The media folder does not exist</exception> + 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); + } + + /// <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> + /// <param name="appPaths">The app paths.</param> + /// <exception cref="System.ArgumentException">The path is not valid.</exception> + /// <exception cref="System.IO.DirectoryNotFoundException">The path does not exist.</exception> + 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); + } + + /// <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> + /// <param name="appPaths">The app paths.</param> + /// <exception cref="System.ArgumentException"></exception> + 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)); + } + } + + /// <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 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 +{ + /// <summary> + /// Class GetPhyscialPaths + /// </summary> + [Route("/Library/PhysicalPaths", "GET")] + public class GetPhyscialPaths : IReturn<List<string>> + { + } + + /// <summary> + /// Class GetItemTypes + /// </summary> + [Route("/Library/ItemTypes", "GET")] + public class GetItemTypes : IReturn<List<string>> + { + /// <summary> + /// Gets or sets a value indicating whether this instance has internet provider. + /// </summary> + /// <value><c>true</c> if this instance has internet provider; otherwise, <c>false</c>.</value> + public bool HasInternetProvider { get; set; } + } + + /// <summary> + /// Class GetPerson + /// </summary> + [Route("/Library/Persons/{Name}", "GET")] + public class GetPerson : IReturn<BaseItemDto> + { + /// <summary> + /// Gets or sets the name. + /// </summary> + /// <value>The name.</value> + public string Name { get; set; } + } + + /// <summary> + /// Class GetStudio + /// </summary> + [Route("/Library/Studios/{Name}", "GET")] + public class GetStudio : IReturn<BaseItemDto> + { + /// <summary> + /// Gets or sets the name. + /// </summary> + /// <value>The name.</value> + public string Name { get; set; } + } + + /// <summary> + /// Class GetGenre + /// </summary> + [Route("/Library/Genres/{Name}", "GET")] + public class GetGenre : IReturn<BaseItemDto> + { + /// <summary> + /// Gets or sets the name. + /// </summary> + /// <value>The name.</value> + public string Name { get; set; } + } + + /// <summary> + /// Class GetYear + /// </summary> + [Route("/Library/Years/{Year}", "GET")] + public class GetYear : IReturn<BaseItemDto> + { + /// <summary> + /// Gets or sets the year. + /// </summary> + /// <value>The year.</value> + public int Year { get; set; } + } + + /// <summary> + /// Class LibraryService + /// </summary> + public class LibraryService : BaseRestService + { + /// <summary> + /// The _app host + /// </summary> + private readonly IApplicationHost _appHost; + + /// <summary> + /// Initializes a new instance of the <see cref="LibraryService" /> class. + /// </summary> + /// <param name="appHost">The app host.</param> + /// <exception cref="System.ArgumentNullException">appHost</exception> + public LibraryService(IApplicationHost appHost) + { + if (appHost == null) + { + throw new ArgumentNullException("appHost"); + } + + _appHost = appHost; + } + + /// <summary> + /// Gets the specified request. + /// </summary> + /// <param name="request">The request.</param> + /// <returns>System.Object.</returns> + 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); + } + + /// <summary> + /// Gets the specified request. + /// </summary> + /// <param name="request">The request.</param> + /// <returns>System.Object.</returns> + 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); + } + + /// <summary> + /// Gets the specified request. + /// </summary> + /// <param name="request">The request.</param> + /// <returns>System.Object.</returns> + 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); + } + + /// <summary> + /// Gets the specified request. + /// </summary> + /// <param name="request">The request.</param> + /// <returns>System.Object.</returns> + 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); + } + + /// <summary> + /// Gets the specified request. + /// </summary> + /// <param name="request">The request.</param> + /// <returns>System.Object.</returns> + public object Get(GetPhyscialPaths request) + { + var kernel = (Kernel)Kernel; + + var result = kernel.RootFolder.Children.SelectMany(c => c.ResolveArgs.PhysicalLocations).ToList(); + + return ToOptimizedResult(result); + } + + /// <summary> + /// Gets the specified request. + /// </summary> + /// <param name="request">The request.</param> + /// <returns>System.Object.</returns> + 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 +{ + /// <summary> + /// Class GetDefaultVirtualFolders + /// </summary> + [Route("/Library/VirtualFolders", "GET")] + [Route("/Users/{UserId}/VirtualFolders", "GET")] + public class GetVirtualFolders : IReturn<List<VirtualFolderInfo>> + { + /// <summary> + /// Gets or sets the user id. + /// </summary> + /// <value>The user id.</value> + public string UserId { get; set; } + } + + [Route("/Library/VirtualFolders/{Name}", "POST")] + [Route("/Users/{UserId}/VirtualFolders/{Name}", "POST")] + public class AddVirtualFolder : IReturnVoid + { + /// <summary> + /// Gets or sets the user id. + /// </summary> + /// <value>The user id.</value> + public string UserId { get; set; } + + /// <summary> + /// Gets or sets the name. + /// </summary> + /// <value>The name.</value> + public string Name { get; set; } + } + + [Route("/Library/VirtualFolders/{Name}", "DELETE")] + [Route("/Users/{UserId}/VirtualFolders/{Name}", "DELETE")] + public class RemoveVirtualFolder : IReturnVoid + { + /// <summary> + /// Gets or sets the user id. + /// </summary> + /// <value>The user id.</value> + public string UserId { get; set; } + + /// <summary> + /// Gets or sets the name. + /// </summary> + /// <value>The name.</value> + public string Name { get; set; } + } + + [Route("/Library/VirtualFolders/{Name}/Name", "POST")] + [Route("/Users/{UserId}/VirtualFolders/{Name}/Name", "POST")] + public class RenameVirtualFolder : IReturnVoid + { + /// <summary> + /// Gets or sets the user id. + /// </summary> + /// <value>The user id.</value> + public string UserId { get; set; } + + /// <summary> + /// Gets or sets the name. + /// </summary> + /// <value>The name.</value> + public string Name { get; set; } + + /// <summary> + /// Gets or sets the name. + /// </summary> + /// <value>The name.</value> + public string NewName { get; set; } + } + + [Route("/Library/VirtualFolders/{Name}/Paths", "POST")] + [Route("/Users/{UserId}/VirtualFolders/{Name}/Paths", "POST")] + public class AddMediaPath : IReturnVoid + { + /// <summary> + /// Gets or sets the user id. + /// </summary> + /// <value>The user id.</value> + public string UserId { get; set; } + + /// <summary> + /// Gets or sets the name. + /// </summary> + /// <value>The name.</value> + public string Name { get; set; } + + /// <summary> + /// Gets or sets the name. + /// </summary> + /// <value>The name.</value> + public string Path { get; set; } + } + + [Route("/Library/VirtualFolders/{Name}/Paths", "DELETE")] + [Route("/Users/{UserId}/VirtualFolders/{Name}/Paths", "DELETE")] + public class RemoveMediaPath : IReturnVoid + { + /// <summary> + /// Gets or sets the user id. + /// </summary> + /// <value>The user id.</value> + public string UserId { get; set; } + + /// <summary> + /// Gets or sets the name. + /// </summary> + /// <value>The name.</value> + public string Name { get; set; } + + /// <summary> + /// Gets or sets the name. + /// </summary> + /// <value>The name.</value> + public string Path { get; set; } + } + + /// <summary> + /// Class LibraryStructureService + /// </summary> + public class LibraryStructureService : BaseRestService + { + /// <summary> + /// The _app paths + /// </summary> + private readonly IServerApplicationPaths _appPaths; + + /// <summary> + /// Initializes a new instance of the <see cref="LibraryService" /> class. + /// </summary> + /// <param name="appPaths">The app paths.</param> + /// <exception cref="System.ArgumentNullException">appHost</exception> + public LibraryStructureService(IServerApplicationPaths appPaths) + { + if (appPaths == null) + { + throw new ArgumentNullException("appPaths"); + } + + _appPaths = appPaths; + } + + /// <summary> + /// Gets the specified request. + /// </summary> + /// <param name="request">The request.</param> + /// <returns>System.Object.</returns> + 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); + } + } + + /// <summary> + /// Posts the specified request. + /// </summary> + /// <param name="request">The request.</param> + 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); + } + } + + /// <summary> + /// Posts the specified request. + /// </summary> + /// <param name="request">The request.</param> + 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); + } + } + + /// <summary> + /// Deletes the specified request. + /// </summary> + /// <param name="request">The request.</param> + 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); + } + } + + /// <summary> + /// Posts the specified request. + /// </summary> + /// <param name="request">The request.</param> + 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); + } + } + + /// <summary> + /// Deletes the specified request. + /// </summary> + /// <param name="request">The request.</param> + 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); + } + } + } +} |
