CollectionManager.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller.Collections;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Entities.Movies;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Providers;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Server.Implementations.Collections
  14. {
  15. public class CollectionManager : ICollectionManager
  16. {
  17. private readonly ILibraryManager _libraryManager;
  18. private readonly IFileSystem _fileSystem;
  19. private readonly ILibraryMonitor _iLibraryMonitor;
  20. public CollectionManager(ILibraryManager libraryManager, IFileSystem fileSystem, ILibraryMonitor iLibraryMonitor)
  21. {
  22. _libraryManager = libraryManager;
  23. _fileSystem = fileSystem;
  24. _iLibraryMonitor = iLibraryMonitor;
  25. }
  26. public async Task CreateCollection(CollectionCreationOptions options)
  27. {
  28. var name = options.Name;
  29. var folderName = _fileSystem.GetValidFilename(name);
  30. var parentFolder = GetParentFolder(options.ParentId);
  31. if (parentFolder == null)
  32. {
  33. throw new ArgumentException();
  34. }
  35. var path = Path.Combine(parentFolder.Path, folderName);
  36. _iLibraryMonitor.ReportFileSystemChangeBeginning(path);
  37. try
  38. {
  39. Directory.CreateDirectory(path);
  40. var collection = new BoxSet
  41. {
  42. Name = name,
  43. Parent = parentFolder,
  44. DisplayMediaType = "Collection",
  45. Path = path,
  46. DontFetchMeta = options.IsLocked
  47. };
  48. await parentFolder.AddChild(collection, CancellationToken.None).ConfigureAwait(false);
  49. await collection.RefreshMetadata(new MetadataRefreshOptions(), CancellationToken.None)
  50. .ConfigureAwait(false);
  51. }
  52. finally
  53. {
  54. // Refresh handled internally
  55. _iLibraryMonitor.ReportFileSystemChangeComplete(path, false);
  56. }
  57. }
  58. private Folder GetParentFolder(Guid? parentId)
  59. {
  60. if (parentId.HasValue)
  61. {
  62. if (parentId.Value == Guid.Empty)
  63. {
  64. throw new ArgumentNullException("parentId");
  65. }
  66. return _libraryManager.GetItemById(parentId.Value) as Folder;
  67. }
  68. return _libraryManager.RootFolder.Children.OfType<ManualCollectionsFolder>().FirstOrDefault() ??
  69. _libraryManager.RootFolder.GetHiddenChildren().OfType<ManualCollectionsFolder>().FirstOrDefault();
  70. }
  71. public async Task AddToCollection(Guid collectionId, IEnumerable<Guid> ids)
  72. {
  73. var collection = _libraryManager.GetItemById(collectionId) as BoxSet;
  74. if (collection == null)
  75. {
  76. throw new ArgumentException("No collection exists with the supplied Id");
  77. }
  78. var list = new List<LinkedChild>();
  79. foreach (var itemId in ids)
  80. {
  81. var item = _libraryManager.GetItemById(itemId);
  82. if (item == null)
  83. {
  84. throw new ArgumentException("No item exists with the supplied Id");
  85. }
  86. if (collection.LinkedChildren.Any(i => i.ItemId.HasValue && i.ItemId == itemId))
  87. {
  88. throw new ArgumentException("Item already exists in collection");
  89. }
  90. list.Add(new LinkedChild
  91. {
  92. ItemName = item.Name,
  93. ItemYear = item.ProductionYear,
  94. ItemType = item.GetType().Name,
  95. Type = LinkedChildType.Manual
  96. });
  97. }
  98. collection.LinkedChildren.AddRange(list);
  99. await collection.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
  100. await collection.RefreshMetadata(CancellationToken.None).ConfigureAwait(false);
  101. }
  102. public async Task RemoveFromCollection(Guid collectionId, IEnumerable<Guid> itemIds)
  103. {
  104. var collection = _libraryManager.GetItemById(collectionId) as BoxSet;
  105. if (collection == null)
  106. {
  107. throw new ArgumentException("No collection exists with the supplied Id");
  108. }
  109. var list = new List<LinkedChild>();
  110. foreach (var itemId in itemIds)
  111. {
  112. var child = collection.LinkedChildren.FirstOrDefault(i => i.ItemId.HasValue && i.ItemId.Value == itemId);
  113. if (child == null)
  114. {
  115. throw new ArgumentException("No collection title exists with the supplied Id");
  116. }
  117. list.Add(child);
  118. }
  119. var shortcutFiles = Directory
  120. .EnumerateFiles(collection.Path, "*", SearchOption.TopDirectoryOnly)
  121. .Where(i => _fileSystem.IsShortcut(i))
  122. .ToList();
  123. var shortcutFilesToDelete = list.Where(child => !string.IsNullOrWhiteSpace(child.Path) && child.Type == LinkedChildType.Shortcut)
  124. .Select(child => shortcutFiles.FirstOrDefault(i => string.Equals(child.Path, _fileSystem.ResolveShortcut(i), StringComparison.OrdinalIgnoreCase)))
  125. .Where(i => !string.IsNullOrWhiteSpace(i))
  126. .ToList();
  127. foreach (var file in shortcutFilesToDelete)
  128. {
  129. _iLibraryMonitor.ReportFileSystemChangeBeginning(file);
  130. }
  131. try
  132. {
  133. foreach (var file in shortcutFilesToDelete)
  134. {
  135. File.Delete(file);
  136. }
  137. foreach (var child in list)
  138. {
  139. collection.LinkedChildren.Remove(child);
  140. }
  141. }
  142. finally
  143. {
  144. foreach (var file in shortcutFilesToDelete)
  145. {
  146. _iLibraryMonitor.ReportFileSystemChangeComplete(file, false);
  147. }
  148. }
  149. await collection.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
  150. await collection.RefreshMetadata(CancellationToken.None).ConfigureAwait(false);
  151. }
  152. }
  153. }