diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2013-06-12 17:46:50 -0400 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2013-06-12 17:46:50 -0400 |
| commit | def34281994508213e5200fb150cdbcc817c55ab (patch) | |
| tree | c9233f3dce58987a3db389c90750ea5fa6d38096 /MediaBrowser.Controller/Entities/Video.cs | |
| parent | 455de48a65ddb9ac1fb5ff989b72f9735749a47e (diff) | |
Added poor man's multi-file movie support
Diffstat (limited to 'MediaBrowser.Controller/Entities/Video.cs')
| -rw-r--r-- | MediaBrowser.Controller/Entities/Video.cs | 111 |
1 files changed, 109 insertions, 2 deletions
diff --git a/MediaBrowser.Controller/Entities/Video.cs b/MediaBrowser.Controller/Entities/Video.cs index bac29f0f5..a15362037 100644 --- a/MediaBrowser.Controller/Entities/Video.cs +++ b/MediaBrowser.Controller/Entities/Video.cs @@ -1,8 +1,13 @@ -using MediaBrowser.Model.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Resolvers; +using MediaBrowser.Model.Entities; +using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization; +using System.Threading; +using System.Threading.Tasks; namespace MediaBrowser.Controller.Entities { @@ -11,11 +16,16 @@ namespace MediaBrowser.Controller.Entities /// </summary> public class Video : BaseItem, IHasMediaStreams { + public bool IsMultiPart { get; set; } + + public List<Guid> AdditionalPartIds { get; set; } + public Video() { MediaStreams = new List<MediaStream>(); Chapters = new List<ChapterInfo>(); PlayableStreamFileNames = new List<string>(); + AdditionalPartIds = new List<Guid>(); } /// <summary> @@ -61,7 +71,7 @@ namespace MediaBrowser.Controller.Entities { return GetPlayableStreamFiles(Path); } - + /// <summary> /// Gets the playable stream files. /// </summary> @@ -112,5 +122,102 @@ namespace MediaBrowser.Controller.Entities return Model.Entities.MediaType.Video; } } + + /// <summary> + /// Overrides the base implementation to refresh metadata for local trailers + /// </summary> + /// <param name="cancellationToken">The cancellation token.</param> + /// <param name="forceSave">if set to <c>true</c> [is new item].</param> + /// <param name="forceRefresh">if set to <c>true</c> [force].</param> + /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param> + /// <returns>true if a provider reports we changed</returns> + public override async Task<bool> RefreshMetadata(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true) + { + // Kick off a task to refresh the main item + var result = await base.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders).ConfigureAwait(false); + + var additionalPartsChanged = await RefreshAdditionalParts(cancellationToken, forceSave, forceRefresh, allowSlowProviders).ConfigureAwait(false); + + return additionalPartsChanged || result; + } + + /// <summary> + /// Refreshes the additional parts. + /// </summary> + /// <param name="cancellationToken">The cancellation token.</param> + /// <param name="forceSave">if set to <c>true</c> [force save].</param> + /// <param name="forceRefresh">if set to <c>true</c> [force refresh].</param> + /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param> + /// <returns>Task{System.Boolean}.</returns> + private async Task<bool> RefreshAdditionalParts(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true) + { + var newItems = LoadAdditionalParts().ToList(); + var newItemIds = newItems.Select(i => i.Id).ToList(); + + var itemsChanged = !AdditionalPartIds.SequenceEqual(newItemIds); + + var tasks = newItems.Select(i => i.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders)); + + var results = await Task.WhenAll(tasks).ConfigureAwait(false); + + AdditionalPartIds = newItemIds; + + return itemsChanged || results.Contains(true); + } + + /// <summary> + /// Loads the additional parts. + /// </summary> + /// <returns>IEnumerable{Video}.</returns> + private IEnumerable<Video> LoadAdditionalParts() + { + if (!IsMultiPart || LocationType != LocationType.FileSystem) + { + return new List<Video>(); + } + + ItemResolveArgs resolveArgs; + + try + { + resolveArgs = ResolveArgs; + } + catch (IOException ex) + { + Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path); + return new List<Video>(); + } + + if (!resolveArgs.IsDirectory) + { + return new List<Video>(); + } + + var files = resolveArgs.FileSystemChildren.Where(i => + { + if ((i.Attributes & FileAttributes.Directory) == FileAttributes.Directory) + { + return false; + } + + return !string.Equals(i.FullName, Path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsVideoFile(i.FullName) && EntityResolutionHelper.IsMultiPartFile(i.FullName); + }); + + return LibraryManager.ResolvePaths<Video>(files, null).Select(video => + { + // Try to retrieve it from the db. If we don't find it, use the resolved version + var dbItem = LibraryManager.RetrieveItem(video.Id) as Video; + + if (dbItem != null) + { + dbItem.ResolveArgs = video.ResolveArgs; + video = dbItem; + } + + return video; + + }).ToList(); + } + } } |
