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

294 lines
9.2 KiB
C#
Raw Normal View History

2014-12-10 23:20:28 -07:00
using MediaBrowser.Common.Extensions;
2014-07-26 10:30:15 -07:00
using MediaBrowser.Controller.Drawing;
2014-07-22 09:36:34 -07:00
using MediaBrowser.Controller.Entities;
2014-07-26 10:30:15 -07:00
using MediaBrowser.Controller.Entities.Audio;
2014-12-10 23:20:28 -07:00
using MediaBrowser.Controller.Entities.TV;
2014-07-26 10:30:15 -07:00
using MediaBrowser.Controller.Library;
2014-07-22 09:36:34 -07:00
using MediaBrowser.Controller.Sync;
2014-12-12 20:56:30 -07:00
using MediaBrowser.Model.Dlna;
2014-07-22 09:36:34 -07:00
using MediaBrowser.Model.Entities;
2014-07-26 10:30:15 -07:00
using MediaBrowser.Model.Logging;
2014-07-17 15:21:35 -07:00
using MediaBrowser.Model.Querying;
using MediaBrowser.Model.Sync;
2014-07-26 10:30:15 -07:00
using MoreLinq;
2014-07-17 15:21:35 -07:00
using System;
using System.Collections.Generic;
2014-07-21 18:29:06 -07:00
using System.Linq;
2014-07-17 15:21:35 -07:00
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Sync
{
public class SyncManager : ISyncManager
{
2014-07-26 10:30:15 -07:00
private readonly ILibraryManager _libraryManager;
private readonly ISyncRepository _repo;
private readonly IImageProcessor _imageProcessor;
private readonly ILogger _logger;
2014-12-12 20:56:30 -07:00
private readonly IUserManager _userManager;
2014-07-26 10:30:15 -07:00
private ISyncProvider[] _providers = { };
2014-12-12 20:56:30 -07:00
public SyncManager(ILibraryManager libraryManager, ISyncRepository repo, IImageProcessor imageProcessor, ILogger logger, IUserManager userManager)
2014-07-26 10:30:15 -07:00
{
_libraryManager = libraryManager;
_repo = repo;
_imageProcessor = imageProcessor;
_logger = logger;
2014-12-12 20:56:30 -07:00
_userManager = userManager;
2014-07-26 10:30:15 -07:00
}
2014-07-21 18:29:06 -07:00
public void AddParts(IEnumerable<ISyncProvider> providers)
{
_providers = providers.ToArray();
}
2014-07-26 10:30:15 -07:00
public async Task<SyncJobCreationResult> CreateJob(SyncJobRequest request)
2014-07-17 15:21:35 -07:00
{
2014-12-12 20:56:30 -07:00
var processor = new SyncJobProcessor(_libraryManager, _repo, this, _logger, _userManager);
var user = _userManager.GetUserById(request.UserId);
2014-12-15 22:01:57 -07:00
2014-12-12 20:56:30 -07:00
var items = processor
2014-12-17 15:39:17 -07:00
.GetItemsForSync(request.ItemIds, user, request.UnwatchedOnly)
2014-12-10 23:20:28 -07:00
.ToList();
if (items.Any(i => !SupportsSync(i)))
{
throw new ArgumentException("Item does not support sync.");
}
2014-07-26 10:30:15 -07:00
2014-12-12 20:56:30 -07:00
if (string.IsNullOrWhiteSpace(request.Name) && request.ItemIds.Count == 1)
2014-07-26 10:30:15 -07:00
{
2014-12-12 20:56:30 -07:00
request.Name = GetDefaultName(_libraryManager.GetItemById(request.ItemIds[0]));
2014-07-26 10:30:15 -07:00
}
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,
2014-12-10 23:20:28 -07:00
ItemLimit = request.ItemLimit,
2014-07-26 10:30:15 -07:00
RequestedItemIds = request.ItemIds,
DateCreated = DateTime.UtcNow,
2014-07-27 15:01:29 -07:00
DateLastModified = DateTime.UtcNow,
2014-12-10 23:20:28 -07:00
SyncNewContent = request.SyncNewContent,
ItemCount = items.Count,
2014-12-15 22:01:57 -07:00
Quality = request.Quality,
Category = request.Category,
ParentId = request.ParentId
2014-07-26 10:30:15 -07:00
};
2014-12-12 20:56:30 -07:00
// It's just a static list
if (!items.Any(i => i.IsFolder || i is IItemByName))
{
job.SyncNewContent = false;
}
2014-07-26 10:30:15 -07:00
await _repo.Create(job).ConfigureAwait(false);
2014-12-12 20:56:30 -07:00
await processor.EnsureJobItems(job).ConfigureAwait(false);
2014-07-26 10:30:15 -07:00
return new SyncJobCreationResult
{
Job = GetJob(jobId)
};
2014-07-17 15:21:35 -07:00
}
public QueryResult<SyncJob> GetJobs(SyncJobQuery query)
{
2014-07-26 10:30:15 -07:00
var result = _repo.GetJobs(query);
2014-07-17 15:21:35 -07:00
2014-07-26 10:30:15 -07:00
result.Items.ForEach(FillMetadata);
2014-07-17 15:21:35 -07:00
2014-07-26 10:30:15 -07:00
return result;
2014-07-17 15:21:35 -07:00
}
2014-07-26 10:30:15 -07:00
private void FillMetadata(SyncJob job)
2014-07-17 15:21:35 -07:00
{
2014-12-12 20:56:30 -07:00
var item = job.RequestedItemIds
.Select(_libraryManager.GetItemById)
.FirstOrDefault(i => i != null);
2014-07-26 10:30:15 -07:00
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 15:21:35 -07:00
}
2014-07-26 10:30:15 -07:00
public Task CancelJob(string id)
2014-07-17 15:21:35 -07:00
{
2014-12-10 23:20:28 -07:00
return _repo.DeleteJob(id);
2014-07-17 15:21:35 -07:00
}
2014-07-26 10:30:15 -07:00
public SyncJob GetJob(string id)
2014-07-17 15:21:35 -07:00
{
2014-07-26 10:30:15 -07:00
return _repo.GetJob(id);
2014-07-17 15:21:35 -07:00
}
2014-07-21 18:29:06 -07:00
2014-07-22 09:36:34 -07:00
public IEnumerable<SyncTarget> GetSyncTargets(string userId)
{
return _providers
2014-07-26 10:30:15 -07:00
.SelectMany(i => GetSyncTargets(i, userId))
2014-07-22 09:36:34 -07:00
.OrderBy(i => i.Name);
}
2014-07-26 10:30:15 -07:00
private IEnumerable<SyncTarget> GetSyncTargets(ISyncProvider provider, string userId)
2014-07-22 09:36:34 -07:00
{
var providerId = GetSyncProviderId(provider);
return provider.GetSyncTargets().Select(i => new SyncTarget
{
Name = i.Name,
2014-12-12 20:56:30 -07:00
Id = GetSyncTargetId(providerId, i)
2014-07-22 09:36:34 -07:00
});
}
2014-12-12 20:56:30 -07:00
private string GetSyncTargetId(string providerId, SyncTarget target)
{
return (providerId + "-" + target.Id).GetMD5().ToString("N");
}
2014-07-22 09:36:34 -07:00
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)
{
2014-12-10 23:20:28 -07:00
return (provider.GetType().Name).GetMD5().ToString("N");
2014-07-22 09:36:34 -07:00
}
public bool SupportsSync(BaseItem item)
2014-07-21 18:29:06 -07:00
{
2014-12-12 20:56:30 -07:00
if (string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase) ||
string.Equals(item.MediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase) ||
string.Equals(item.MediaType, MediaType.Photo, StringComparison.OrdinalIgnoreCase) ||
string.Equals(item.MediaType, MediaType.Game, StringComparison.OrdinalIgnoreCase) ||
string.Equals(item.MediaType, MediaType.Book, StringComparison.OrdinalIgnoreCase))
2014-07-22 09:36:34 -07:00
{
2014-12-10 23:20:28 -07:00
if (item.LocationType == LocationType.Virtual)
{
return false;
}
2014-12-12 20:56:30 -07:00
if (!item.RunTimeTicks.HasValue)
2014-07-22 09:36:34 -07:00
{
2014-12-12 20:56:30 -07:00
return false;
}
var video = item as Video;
if (video != null)
{
if (video.VideoType == VideoType.Iso)
{
return false;
}
2014-07-22 09:36:34 -07:00
2014-12-12 20:56:30 -07:00
if (video.IsStacked)
2014-07-22 09:36:34 -07:00
{
2014-12-12 20:56:30 -07:00
return false;
2014-07-22 09:36:34 -07:00
}
2014-12-12 20:56:30 -07:00
}
2014-07-22 09:36:34 -07:00
2014-12-12 20:56:30 -07:00
var game = item as Game;
if (game != null)
{
if (game.IsMultiPart)
{
return false;
}
2014-07-22 09:36:34 -07:00
}
2014-12-12 20:56:30 -07:00
return true;
2014-07-22 09:36:34 -07:00
}
2014-12-10 23:20:28 -07:00
return item.LocationType == LocationType.FileSystem || item is Season;
2014-07-26 10:30:15 -07:00
}
private string GetDefaultName(BaseItem item)
{
return item.Name;
}
2014-12-12 20:56:30 -07:00
public DeviceProfile GetDeviceProfile(string targetId)
{
foreach (var provider in _providers)
{
foreach (var target in GetSyncTargets(provider, null))
{
if (string.Equals(target.Id, targetId, StringComparison.OrdinalIgnoreCase))
{
return provider.GetDeviceProfile(target);
}
}
}
return null;
}
2014-12-16 22:30:31 -07:00
public async Task ReportSyncJobItemTransferred(string id)
{
var jobItem = _repo.GetJobItem(id);
jobItem.Status = SyncJobItemStatus.Completed;
jobItem.Progress = 100;
await _repo.Update(jobItem).ConfigureAwait(false);
var processor = new SyncJobProcessor(_libraryManager, _repo, this, _logger, _userManager);
await processor.UpdateJobStatus(jobItem.JobId).ConfigureAwait(false);
}
public SyncJobItem GetJobItem(string id)
{
return _repo.GetJobItem(id);
}
2014-12-16 22:52:34 -07:00
public QueryResult<SyncJobItem> GetJobItems(SyncJobItemQuery query)
{
return _repo.GetJobItems(query);
}
2014-07-17 15:21:35 -07:00
}
}