jellyfin-server/MediaBrowser.Controller/Entities/Movies/Movie.cs

153 lines
5.5 KiB
C#
Raw Normal View History

using MediaBrowser.Model.Entities;
using System;
2013-02-21 01:33:05 +00:00
using System.Collections.Generic;
2013-02-21 17:51:17 +00:00
using System.IO;
2013-02-21 01:33:05 +00:00
using System.Linq;
using System.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Controller.Entities.Movies
{
/// <summary>
/// Class Movie
/// </summary>
2013-02-21 17:51:17 +00:00
public class Movie : Video
2013-02-21 01:33:05 +00:00
{
public List<Guid> SpecialFeatureIds { get; set; }
public Movie()
{
SpecialFeatureIds = new List<Guid>();
}
2013-06-16 19:02:57 +00:00
2013-02-21 01:33:05 +00:00
/// <summary>
/// Should be overridden to return the proper folder where metadata lives
/// </summary>
/// <value>The meta location.</value>
[IgnoreDataMember]
public override string MetaLocation
{
get
{
2013-06-16 19:02:57 +00:00
return VideoType == VideoType.VideoFile || VideoType == VideoType.Iso || IsMultiPart ? System.IO.Path.GetDirectoryName(Path) : Path;
2013-02-21 01:33:05 +00:00
}
}
/// <summary>
/// Gets the user data key.
2013-02-21 01:33:05 +00:00
/// </summary>
/// <returns>System.String.</returns>
public override string GetUserDataKey()
2013-02-21 01:33:05 +00:00
{
return this.GetProviderId(MetadataProviders.Tmdb) ?? this.GetProviderId(MetadataProviders.Imdb) ?? base.GetUserDataKey();
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Needed because the resolver stops at the movie folder and we find the video inside.
/// </summary>
/// <value><c>true</c> if [use parent path to create resolve args]; otherwise, <c>false</c>.</value>
protected override bool UseParentPathToCreateResolveArgs
{
get
{
2013-06-16 19:02:57 +00:00
return VideoType == VideoType.VideoFile || VideoType == VideoType.Iso || IsMultiPart;
2013-02-21 01:33:05 +00:00
}
}
/// <summary>
/// Overrides the base implementation to refresh metadata for special features
/// </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>
/// <param name="resetResolveArgs">The reset resolve args.</param>
2013-02-21 01:33:05 +00:00
/// <returns>Task{System.Boolean}.</returns>
public override async Task<bool> RefreshMetadata(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true, bool resetResolveArgs = true)
2013-02-21 01:33:05 +00:00
{
// Kick off a task to refresh the main item
var result = await base.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders, resetResolveArgs).ConfigureAwait(false);
2013-02-21 01:33:05 +00:00
2013-06-17 20:35:43 +00:00
var specialFeaturesChanged = false;
// Must have a parent to have special features
// In other words, it must be part of the Parent/Child tree
if (LocationType == LocationType.FileSystem && Parent != null)
{
specialFeaturesChanged = await RefreshSpecialFeatures(cancellationToken, forceSave, forceRefresh, allowSlowProviders).ConfigureAwait(false);
}
2013-02-21 01:33:05 +00:00
return specialFeaturesChanged || result;
}
private async Task<bool> RefreshSpecialFeatures(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true)
{
var newItems = LoadSpecialFeatures().ToList();
var newItemIds = newItems.Select(i => i.Id).ToList();
2013-02-21 01:33:05 +00:00
var itemsChanged = !SpecialFeatureIds.SequenceEqual(newItemIds);
2013-02-21 01:33:05 +00:00
var tasks = newItems.Select(i => i.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders));
var results = await Task.WhenAll(tasks).ConfigureAwait(false);
SpecialFeatureIds = newItemIds;
return itemsChanged || results.Contains(true);
}
2013-06-16 19:02:57 +00:00
2013-02-21 17:51:17 +00:00
/// <summary>
/// Loads the special features.
2013-02-21 17:51:17 +00:00
/// </summary>
/// <returns>IEnumerable{Video}.</returns>
2013-02-21 17:51:17 +00:00
private IEnumerable<Video> LoadSpecialFeatures()
{
FileSystemInfo folder;
2013-02-21 17:51:17 +00:00
try
{
folder = ResolveArgs.GetFileSystemEntryByName("specials");
}
catch (IOException ex)
{
2013-02-21 20:26:35 +00:00
Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path);
return new List<Video>();
2013-02-21 17:51:17 +00:00
}
// Path doesn't exist. No biggie
if (folder == null)
{
return new List<Video>();
2013-02-21 17:51:17 +00:00
}
IEnumerable<FileSystemInfo> files;
2013-02-21 17:51:17 +00:00
try
{
files = new DirectoryInfo(folder.FullName).EnumerateFiles();
2013-02-21 17:51:17 +00:00
}
catch (IOException ex)
{
Logger.ErrorException("Error loading special features for {0}", ex, Name);
return new List<Video>();
2013-02-21 17:51:17 +00:00
}
return LibraryManager.ResolvePaths<Video>(files, null).Select(video =>
2013-02-21 17:51:17 +00:00
{
// Try to retrieve it from the db. If we don't find it, use the resolved version
2013-06-17 20:35:43 +00:00
var dbItem = LibraryManager.RetrieveItem(video.Id, typeof(Video)) as Video;
2013-02-21 17:51:17 +00:00
if (dbItem != null)
{
dbItem.ResolveArgs = video.ResolveArgs;
video = dbItem;
}
return video;
});
2013-02-21 01:33:05 +00:00
}
2013-02-21 17:51:17 +00:00
2013-02-21 01:33:05 +00:00
}
}