jellyfin-server/MediaBrowser.Server.Implementations/Sync/SyncManager.cs

238 lines
7.0 KiB
C#
Raw Normal View History

2014-07-22 16:36:34 +00:00
using MediaBrowser.Common.Extensions;
2014-07-26 17:30:15 +00:00
using MediaBrowser.Controller.Drawing;
2014-07-22 16:36:34 +00:00
using MediaBrowser.Controller.Entities;
2014-07-26 17:30:15 +00:00
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
2014-07-22 16:36:34 +00:00
using MediaBrowser.Controller.Sync;
using MediaBrowser.Model.Entities;
2014-07-26 17:30:15 +00:00
using MediaBrowser.Model.Logging;
2014-07-17 22:21:35 +00:00
using MediaBrowser.Model.Querying;
using MediaBrowser.Model.Sync;
2014-07-26 17:30:15 +00:00
using MoreLinq;
2014-07-17 22:21:35 +00:00
using System;
using System.Collections.Generic;
2014-07-22 01:29:06 +00:00
using System.Linq;
2014-07-17 22:21:35 +00:00
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Sync
{
public class SyncManager : ISyncManager
{
2014-07-26 17:30:15 +00:00
private readonly ILibraryManager _libraryManager;
private readonly ISyncRepository _repo;
private readonly IImageProcessor _imageProcessor;
private readonly ILogger _logger;
private ISyncProvider[] _providers = { };
public SyncManager(ILibraryManager libraryManager, ISyncRepository repo, IImageProcessor imageProcessor, ILogger logger)
{
_libraryManager = libraryManager;
_repo = repo;
_imageProcessor = imageProcessor;
_logger = logger;
}
2014-07-22 01:29:06 +00:00
public void AddParts(IEnumerable<ISyncProvider> providers)
{
_providers = providers.ToArray();
}
2014-07-26 17:30:15 +00:00
public async Task<SyncJobCreationResult> CreateJob(SyncJobRequest request)
2014-07-17 22:21:35 +00:00
{
2014-07-26 17:30:15 +00:00
var items = GetItemsForSync(request.ItemIds).ToList();
if (items.Count == 1)
{
request.Name = GetDefaultName(items[0]);
}
if (string.IsNullOrWhiteSpace(request.Name))
{
throw new ArgumentException("Please supply a name for the sync job.");
}
var target = GetSyncTargets(request.UserId)
.First(i => string.Equals(request.TargetId, i.Id));
var jobId = Guid.NewGuid().ToString("N");
var job = new SyncJob
{
Id = jobId,
Name = request.Name,
TargetId = target.Id,
UserId = request.UserId,
UnwatchedOnly = request.UnwatchedOnly,
Limit = request.Limit,
LimitType = request.LimitType,
RequestedItemIds = request.ItemIds,
DateCreated = DateTime.UtcNow,
2014-07-27 22:01:29 +00:00
DateLastModified = DateTime.UtcNow,
ItemCount = 1
2014-07-26 17:30:15 +00:00
};
await _repo.Create(job).ConfigureAwait(false);
return new SyncJobCreationResult
{
Job = GetJob(jobId)
};
2014-07-17 22:21:35 +00:00
}
public QueryResult<SyncJob> GetJobs(SyncJobQuery query)
{
2014-07-26 17:30:15 +00:00
var result = _repo.GetJobs(query);
2014-07-17 22:21:35 +00:00
2014-07-26 17:30:15 +00:00
result.Items.ForEach(FillMetadata);
2014-07-17 22:21:35 +00:00
2014-07-26 17:30:15 +00:00
return result;
2014-07-17 22:21:35 +00:00
}
2014-07-26 17:30:15 +00:00
private void FillMetadata(SyncJob job)
2014-07-17 22:21:35 +00:00
{
2014-07-26 17:30:15 +00:00
var item = GetItemsForSync(job.RequestedItemIds)
.FirstOrDefault();
if (item != null)
{
var hasSeries = item as IHasSeries;
if (hasSeries != null)
{
job.ParentName = hasSeries.SeriesName;
}
var hasAlbumArtist = item as IHasAlbumArtist;
if (hasAlbumArtist != null)
{
job.ParentName = hasAlbumArtist.AlbumArtists.FirstOrDefault();
}
var primaryImage = item.GetImageInfo(ImageType.Primary, 0);
if (primaryImage != null)
{
try
{
job.PrimaryImageTag = _imageProcessor.GetImageCacheTag(item, ImageType.Primary);
job.PrimaryImageItemId = item.Id.ToString("N");
}
catch (Exception ex)
{
_logger.ErrorException("Error getting image info", ex);
}
}
}
2014-07-17 22:21:35 +00:00
}
2014-07-26 17:30:15 +00:00
public Task CancelJob(string id)
2014-07-17 22:21:35 +00:00
{
throw new NotImplementedException();
}
2014-07-26 17:30:15 +00:00
public SyncJob GetJob(string id)
2014-07-17 22:21:35 +00:00
{
2014-07-26 17:30:15 +00:00
return _repo.GetJob(id);
2014-07-17 22:21:35 +00:00
}
2014-07-22 01:29:06 +00:00
2014-07-22 16:36:34 +00:00
public IEnumerable<SyncTarget> GetSyncTargets(string userId)
{
return _providers
2014-07-26 17:30:15 +00:00
.SelectMany(i => GetSyncTargets(i, userId))
2014-07-22 16:36:34 +00:00
.OrderBy(i => i.Name);
}
2014-07-26 17:30:15 +00:00
private IEnumerable<SyncTarget> GetSyncTargets(ISyncProvider provider, string userId)
2014-07-22 16:36:34 +00:00
{
var providerId = GetSyncProviderId(provider);
return provider.GetSyncTargets().Select(i => new SyncTarget
{
Name = i.Name,
Id = providerId + "-" + i.Id
});
}
private ISyncProvider GetSyncProvider(SyncTarget target)
{
var providerId = target.Id.Split(new[] { '-' }, 2).First();
return _providers.First(i => string.Equals(providerId, GetSyncProviderId(i)));
}
private string GetSyncProviderId(ISyncProvider provider)
{
return (provider.GetType().Name + provider.Name).GetMD5().ToString("N");
}
public bool SupportsSync(BaseItem item)
2014-07-22 01:29:06 +00:00
{
2014-07-22 16:36:34 +00:00
if (item.LocationType == LocationType.Virtual)
{
return false;
}
if (string.Equals(item.MediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase) ||
string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
{
if (item.RunTimeTicks.HasValue)
{
var video = item as Video;
if (video != null)
{
if (video.VideoType != VideoType.VideoFile)
{
return false;
}
if (video.IsMultiPart)
{
return false;
}
}
return true;
}
return false;
}
return false;
2014-07-22 01:29:06 +00:00
}
2014-07-26 17:30:15 +00:00
private IEnumerable<BaseItem> GetItemsForSync(IEnumerable<string> itemIds)
{
return itemIds.SelectMany(GetItemsForSync).DistinctBy(i => i.Id);
}
private IEnumerable<BaseItem> GetItemsForSync(string id)
{
var item = _libraryManager.GetItemById(id);
if (item == null)
{
throw new ArgumentException("Item with Id " + id + " not found.");
}
if (!SupportsSync(item))
{
throw new ArgumentException("Item with Id " + id + " does not support sync.");
}
return GetItemsForSync(item);
}
private IEnumerable<BaseItem> GetItemsForSync(BaseItem item)
{
return new[] { item };
}
private string GetDefaultName(BaseItem item)
{
return item.Name;
}
2014-07-17 22:21:35 +00:00
}
}