CollectionManager.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // Need to use the [boxset] suffix
  30. // If internet metadata is not found, or if xml saving is off there will be no collection.xml
  31. // This could cause it to get re-resolved as a plain folder
  32. var folderName = _fileSystem.GetValidFilename(name) + " [boxset]";
  33. var parentFolder = GetParentFolder(options.ParentId);
  34. if (parentFolder == null)
  35. {
  36. throw new ArgumentException();
  37. }
  38. var path = Path.Combine(parentFolder.Path, folderName);
  39. _iLibraryMonitor.ReportFileSystemChangeBeginning(path);
  40. try
  41. {
  42. Directory.CreateDirectory(path);
  43. var collection = new BoxSet
  44. {
  45. Name = name,
  46. Parent = parentFolder,
  47. DisplayMediaType = "Collection",
  48. Path = path,
  49. DontFetchMeta = options.IsLocked,
  50. ProviderIds = options.ProviderIds
  51. };
  52. await parentFolder.AddChild(collection, CancellationToken.None).ConfigureAwait(false);
  53. await collection.RefreshMetadata(new MetadataRefreshOptions(), CancellationToken.None)
  54. .ConfigureAwait(false);
  55. }
  56. finally
  57. {
  58. // Refresh handled internally
  59. _iLibraryMonitor.ReportFileSystemChangeComplete(path, false);
  60. }
  61. }
  62. private Folder GetParentFolder(Guid? parentId)
  63. {
  64. if (parentId.HasValue)
  65. {
  66. if (parentId.Value == Guid.Empty)
  67. {
  68. throw new ArgumentNullException("parentId");
  69. }
  70. return _libraryManager.GetItemById(parentId.Value) as Folder;
  71. }
  72. return _libraryManager.RootFolder.Children.OfType<ManualCollectionsFolder>().FirstOrDefault() ??
  73. _libraryManager.RootFolder.GetHiddenChildren().OfType<ManualCollectionsFolder>().FirstOrDefault();
  74. }
  75. public async Task AddToCollection(Guid collectionId, IEnumerable<Guid> ids)
  76. {
  77. var collection = _libraryManager.GetItemById(collectionId) as BoxSet;
  78. if (collection == null)
  79. {
  80. throw new ArgumentException("No collection exists with the supplied Id");
  81. }
  82. var list = new List<LinkedChild>();
  83. foreach (var itemId in ids)
  84. {
  85. var item = _libraryManager.GetItemById(itemId);
  86. if (item == null)
  87. {
  88. throw new ArgumentException("No item exists with the supplied Id");
  89. }
  90. if (collection.LinkedChildren.Any(i => i.ItemId.HasValue && i.ItemId == itemId))
  91. {
  92. throw new ArgumentException("Item already exists in collection");
  93. }
  94. list.Add(new LinkedChild
  95. {
  96. ItemName = item.Name,
  97. ItemYear = item.ProductionYear,
  98. ItemType = item.GetType().Name,
  99. Type = LinkedChildType.Manual
  100. });
  101. }
  102. collection.LinkedChildren.AddRange(list);
  103. await collection.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
  104. await collection.RefreshMetadata(CancellationToken.None).ConfigureAwait(false);
  105. }
  106. public async Task RemoveFromCollection(Guid collectionId, IEnumerable<Guid> itemIds)
  107. {
  108. var collection = _libraryManager.GetItemById(collectionId) as BoxSet;
  109. if (collection == null)
  110. {
  111. throw new ArgumentException("No collection exists with the supplied Id");
  112. }
  113. var list = new List<LinkedChild>();
  114. foreach (var itemId in itemIds)
  115. {
  116. var child = collection.LinkedChildren.FirstOrDefault(i => i.ItemId.HasValue && i.ItemId.Value == itemId);
  117. if (child == null)
  118. {
  119. throw new ArgumentException("No collection title exists with the supplied Id");
  120. }
  121. list.Add(child);
  122. }
  123. var shortcutFiles = Directory
  124. .EnumerateFiles(collection.Path, "*", SearchOption.TopDirectoryOnly)
  125. .Where(i => _fileSystem.IsShortcut(i))
  126. .ToList();
  127. var shortcutFilesToDelete = list.Where(child => !string.IsNullOrWhiteSpace(child.Path) && child.Type == LinkedChildType.Shortcut)
  128. .Select(child => shortcutFiles.FirstOrDefault(i => string.Equals(child.Path, _fileSystem.ResolveShortcut(i), StringComparison.OrdinalIgnoreCase)))
  129. .Where(i => !string.IsNullOrWhiteSpace(i))
  130. .ToList();
  131. foreach (var file in shortcutFilesToDelete)
  132. {
  133. _iLibraryMonitor.ReportFileSystemChangeBeginning(file);
  134. }
  135. try
  136. {
  137. foreach (var file in shortcutFilesToDelete)
  138. {
  139. File.Delete(file);
  140. }
  141. foreach (var child in list)
  142. {
  143. collection.LinkedChildren.Remove(child);
  144. }
  145. }
  146. finally
  147. {
  148. foreach (var file in shortcutFilesToDelete)
  149. {
  150. _iLibraryMonitor.ReportFileSystemChangeComplete(file, false);
  151. }
  152. }
  153. await collection.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
  154. await collection.RefreshMetadata(CancellationToken.None).ConfigureAwait(false);
  155. }
  156. }
  157. }