using MediaBrowser.Common.IO; using MediaBrowser.Controller; using System; using System.IO; using System.Linq; namespace MediaBrowser.Api.Library { /// /// Class LibraryHelpers /// public static class LibraryHelpers { /// /// The shortcut file extension /// private const string ShortcutFileExtension = ".mblink"; /// /// The shortcut file search /// private const string ShortcutFileSearch = "*" + ShortcutFileExtension; /// /// Deletes a shortcut from within a virtual folder, within either the default view or a user view /// /// The file system. /// Name of the virtual folder. /// The media path. /// The app paths. /// The media folder does not exist public static void RemoveMediaPath(IFileSystem fileSystem, string virtualFolderName, string mediaPath, IServerApplicationPaths appPaths) { var rootFolderPath = appPaths.DefaultUserViewsPath; var path = Path.Combine(rootFolderPath, virtualFolderName); if (!Directory.Exists(path)) { throw new DirectoryNotFoundException(string.Format("The media collection {0} does not exist", virtualFolderName)); } var shortcut = Directory.EnumerateFiles(path, ShortcutFileSearch, SearchOption.AllDirectories).FirstOrDefault(f => fileSystem.ResolveShortcut(f).Equals(mediaPath, StringComparison.OrdinalIgnoreCase)); if (!string.IsNullOrEmpty(shortcut)) { File.Delete(shortcut); } } /// /// Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view /// /// The file system. /// Name of the virtual folder. /// The path. /// The app paths. /// The path does not exist. /// The path is not valid. public static void AddMediaPath(IFileSystem fileSystem, string virtualFolderName, string path, IServerApplicationPaths appPaths) { if (!Directory.Exists(path)) { throw new DirectoryNotFoundException("The path does not exist."); } var rootFolderPath = appPaths.DefaultUserViewsPath; var virtualFolderPath = Path.Combine(rootFolderPath, virtualFolderName); var shortcutFilename = Path.GetFileNameWithoutExtension(path); var lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension); while (File.Exists(lnk)) { shortcutFilename += "1"; lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension); } fileSystem.CreateShortcut(lnk, path); } } }