2014-12-28 07:21:39 +01:00
|
|
|
|
using MediaBrowser.Common;
|
2015-01-17 05:29:53 +01:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2015-01-21 04:54:45 +01:00
|
|
|
|
using MediaBrowser.Common.Events;
|
2014-12-26 18:45:06 +01:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2015-01-13 04:46:44 +01:00
|
|
|
|
using MediaBrowser.Common.IO;
|
2014-12-26 18:45:06 +01:00
|
|
|
|
using MediaBrowser.Controller.Channels;
|
2014-07-26 19:30:15 +02:00
|
|
|
|
using MediaBrowser.Controller.Drawing;
|
2014-12-26 18:45:06 +01:00
|
|
|
|
using MediaBrowser.Controller.Dto;
|
2014-07-22 18:36:34 +02:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2014-07-26 19:30:15 +02:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2014-12-11 07:20:28 +01:00
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2014-07-26 19:30:15 +02:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-12-20 07:06:27 +01:00
|
|
|
|
using MediaBrowser.Controller.LiveTv;
|
2015-01-02 06:36:27 +01:00
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2014-07-22 18:36:34 +02:00
|
|
|
|
using MediaBrowser.Controller.Sync;
|
2014-12-30 17:36:49 +01:00
|
|
|
|
using MediaBrowser.Controller.TV;
|
2014-12-13 04:56:30 +01:00
|
|
|
|
using MediaBrowser.Model.Dlna;
|
2015-01-17 05:29:53 +01:00
|
|
|
|
using MediaBrowser.Model.Dto;
|
2014-07-22 18:36:34 +02:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2015-01-21 04:54:45 +01:00
|
|
|
|
using MediaBrowser.Model.Events;
|
2014-07-26 19:30:15 +02:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2014-07-18 00:21:35 +02:00
|
|
|
|
using MediaBrowser.Model.Querying;
|
|
|
|
|
using MediaBrowser.Model.Sync;
|
2014-12-26 18:45:06 +01:00
|
|
|
|
using MediaBrowser.Model.Users;
|
2014-07-26 19:30:15 +02:00
|
|
|
|
using MoreLinq;
|
2014-07-18 00:21:35 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-12-28 07:21:39 +01:00
|
|
|
|
using System.IO;
|
2014-07-22 03:29:06 +02:00
|
|
|
|
using System.Linq;
|
2015-01-13 06:40:27 +01:00
|
|
|
|
using System.Threading;
|
2014-07-18 00:21:35 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Sync
|
|
|
|
|
{
|
|
|
|
|
public class SyncManager : ISyncManager
|
|
|
|
|
{
|
2014-07-26 19:30:15 +02:00
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly ISyncRepository _repo;
|
|
|
|
|
private readonly IImageProcessor _imageProcessor;
|
|
|
|
|
private readonly ILogger _logger;
|
2014-12-13 04:56:30 +01:00
|
|
|
|
private readonly IUserManager _userManager;
|
2014-12-27 23:52:41 +01:00
|
|
|
|
private readonly Func<IDtoService> _dtoService;
|
2014-12-26 18:45:06 +01:00
|
|
|
|
private readonly IApplicationHost _appHost;
|
2014-12-30 17:36:49 +01:00
|
|
|
|
private readonly ITVSeriesManager _tvSeriesManager;
|
2015-01-03 20:38:22 +01:00
|
|
|
|
private readonly Func<IMediaEncoder> _mediaEncoder;
|
2015-01-13 04:46:44 +01:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2015-01-16 21:54:37 +01:00
|
|
|
|
private readonly Func<ISubtitleEncoder> _subtitleEncoder;
|
2015-01-17 05:29:53 +01:00
|
|
|
|
private readonly IConfigurationManager _config;
|
2014-07-26 19:30:15 +02:00
|
|
|
|
|
|
|
|
|
private ISyncProvider[] _providers = { };
|
|
|
|
|
|
2015-01-28 05:04:26 +01:00
|
|
|
|
public event EventHandler<GenericEventArgs<SyncJobCreationResult>> SyncJobCreated;
|
2015-01-21 04:54:45 +01:00
|
|
|
|
public event EventHandler<GenericEventArgs<SyncJob>> SyncJobCancelled;
|
|
|
|
|
|
2015-01-17 05:29:53 +01:00
|
|
|
|
public SyncManager(ILibraryManager libraryManager, ISyncRepository repo, IImageProcessor imageProcessor, ILogger logger, IUserManager userManager, Func<IDtoService> dtoService, IApplicationHost appHost, ITVSeriesManager tvSeriesManager, Func<IMediaEncoder> mediaEncoder, IFileSystem fileSystem, Func<ISubtitleEncoder> subtitleEncoder, IConfigurationManager config)
|
2014-07-26 19:30:15 +02:00
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_repo = repo;
|
|
|
|
|
_imageProcessor = imageProcessor;
|
|
|
|
|
_logger = logger;
|
2014-12-13 04:56:30 +01:00
|
|
|
|
_userManager = userManager;
|
2014-12-26 18:45:06 +01:00
|
|
|
|
_dtoService = dtoService;
|
|
|
|
|
_appHost = appHost;
|
2014-12-30 17:36:49 +01:00
|
|
|
|
_tvSeriesManager = tvSeriesManager;
|
2015-01-03 20:38:22 +01:00
|
|
|
|
_mediaEncoder = mediaEncoder;
|
2015-01-13 04:46:44 +01:00
|
|
|
|
_fileSystem = fileSystem;
|
2015-01-16 21:54:37 +01:00
|
|
|
|
_subtitleEncoder = subtitleEncoder;
|
2015-01-17 05:29:53 +01:00
|
|
|
|
_config = config;
|
2014-07-26 19:30:15 +02:00
|
|
|
|
}
|
2014-07-22 03:29:06 +02:00
|
|
|
|
|
|
|
|
|
public void AddParts(IEnumerable<ISyncProvider> providers)
|
|
|
|
|
{
|
|
|
|
|
_providers = providers.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-26 19:30:15 +02:00
|
|
|
|
public async Task<SyncJobCreationResult> CreateJob(SyncJobRequest request)
|
2014-07-18 00:21:35 +02:00
|
|
|
|
{
|
2015-01-17 05:29:53 +01:00
|
|
|
|
var processor = GetSyncJobProcessor();
|
2014-12-13 04:56:30 +01:00
|
|
|
|
|
|
|
|
|
var user = _userManager.GetUserById(request.UserId);
|
2014-12-16 06:01:57 +01:00
|
|
|
|
|
2014-12-30 17:36:49 +01:00
|
|
|
|
var items = (await processor
|
|
|
|
|
.GetItemsForSync(request.Category, request.ParentId, request.ItemIds, user, request.UnwatchedOnly).ConfigureAwait(false))
|
2014-12-11 07:20:28 +01:00
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
if (items.Any(i => !SupportsSync(i)))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Item does not support sync.");
|
|
|
|
|
}
|
2014-07-26 19:30:15 +02:00
|
|
|
|
|
2014-12-30 17:36:49 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(request.Name))
|
2014-07-26 19:30:15 +02:00
|
|
|
|
{
|
2014-12-31 07:24:49 +01:00
|
|
|
|
if (request.ItemIds.Count == 1)
|
2014-12-30 17:36:49 +01:00
|
|
|
|
{
|
|
|
|
|
request.Name = GetDefaultName(_libraryManager.GetItemById(request.ItemIds[0]));
|
|
|
|
|
}
|
2014-07-26 19:30:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(request.Name))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Please supply a name for the sync job.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var target = GetSyncTargets(request.UserId)
|
2014-12-24 07:28:40 +01:00
|
|
|
|
.FirstOrDefault(i => string.Equals(request.TargetId, i.Id));
|
2015-01-28 05:04:26 +01:00
|
|
|
|
|
2014-12-24 07:28:40 +01:00
|
|
|
|
if (target == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Sync target not found.");
|
|
|
|
|
}
|
2014-07-26 19:30:15 +02:00
|
|
|
|
|
|
|
|
|
var jobId = Guid.NewGuid().ToString("N");
|
2015-01-28 05:04:26 +01:00
|
|
|
|
|
2014-07-26 19:30:15 +02:00
|
|
|
|
var job = new SyncJob
|
|
|
|
|
{
|
|
|
|
|
Id = jobId,
|
|
|
|
|
Name = request.Name,
|
|
|
|
|
TargetId = target.Id,
|
|
|
|
|
UserId = request.UserId,
|
|
|
|
|
UnwatchedOnly = request.UnwatchedOnly,
|
2014-12-11 07:20:28 +01:00
|
|
|
|
ItemLimit = request.ItemLimit,
|
2014-12-30 17:36:49 +01:00
|
|
|
|
RequestedItemIds = request.ItemIds ?? new List<string> { },
|
2014-07-26 19:30:15 +02:00
|
|
|
|
DateCreated = DateTime.UtcNow,
|
2014-07-28 00:01:29 +02:00
|
|
|
|
DateLastModified = DateTime.UtcNow,
|
2014-12-11 07:20:28 +01:00
|
|
|
|
SyncNewContent = request.SyncNewContent,
|
|
|
|
|
ItemCount = items.Count,
|
2014-12-16 06:01:57 +01:00
|
|
|
|
Quality = request.Quality,
|
|
|
|
|
Category = request.Category,
|
|
|
|
|
ParentId = request.ParentId
|
2014-07-26 19:30:15 +02:00
|
|
|
|
};
|
|
|
|
|
|
2014-12-13 04:56:30 +01:00
|
|
|
|
// It's just a static list
|
|
|
|
|
if (!items.Any(i => i.IsFolder || i is IItemByName))
|
|
|
|
|
{
|
|
|
|
|
job.SyncNewContent = false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-26 19:30:15 +02:00
|
|
|
|
await _repo.Create(job).ConfigureAwait(false);
|
|
|
|
|
|
2014-12-13 04:56:30 +01:00
|
|
|
|
await processor.EnsureJobItems(job).ConfigureAwait(false);
|
2015-01-17 05:29:53 +01:00
|
|
|
|
|
2015-01-13 06:40:27 +01:00
|
|
|
|
// If it already has a converting status then is must have been aborted during conversion
|
|
|
|
|
var jobItemsResult = _repo.GetJobItems(new SyncJobItemQuery
|
|
|
|
|
{
|
|
|
|
|
Statuses = new List<SyncJobItemStatus> { SyncJobItemStatus.Queued, SyncJobItemStatus.Converting },
|
|
|
|
|
JobId = jobId
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await processor.SyncJobItems(jobItemsResult.Items, false, new Progress<double>(), CancellationToken.None)
|
|
|
|
|
.ConfigureAwait(false);
|
2014-12-13 04:56:30 +01:00
|
|
|
|
|
2015-01-27 23:45:59 +01:00
|
|
|
|
jobItemsResult = _repo.GetJobItems(new SyncJobItemQuery
|
|
|
|
|
{
|
|
|
|
|
Statuses = new List<SyncJobItemStatus> { SyncJobItemStatus.Queued, SyncJobItemStatus.Converting },
|
|
|
|
|
JobId = jobId
|
|
|
|
|
});
|
|
|
|
|
|
2015-01-28 05:04:26 +01:00
|
|
|
|
var returnResult = new SyncJobCreationResult
|
2014-07-26 19:30:15 +02:00
|
|
|
|
{
|
2015-01-27 23:45:59 +01:00
|
|
|
|
Job = GetJob(jobId),
|
|
|
|
|
JobItems = jobItemsResult.Items.ToList()
|
2014-07-26 19:30:15 +02:00
|
|
|
|
};
|
2015-01-28 05:04:26 +01:00
|
|
|
|
|
|
|
|
|
if (SyncJobCreated != null)
|
|
|
|
|
{
|
|
|
|
|
EventHelper.FireEventIfNotNull(SyncJobCreated, this, new GenericEventArgs<SyncJobCreationResult>
|
|
|
|
|
{
|
|
|
|
|
Argument = returnResult
|
|
|
|
|
|
|
|
|
|
}, _logger);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return returnResult;
|
2014-07-18 00:21:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-31 07:24:49 +01:00
|
|
|
|
public Task UpdateJob(SyncJob job)
|
|
|
|
|
{
|
|
|
|
|
// Get fresh from the db and only update the fields that are supported to be changed.
|
|
|
|
|
var instance = _repo.GetJob(job.Id);
|
|
|
|
|
|
|
|
|
|
instance.Name = job.Name;
|
|
|
|
|
instance.Quality = job.Quality;
|
|
|
|
|
instance.UnwatchedOnly = job.UnwatchedOnly;
|
|
|
|
|
instance.SyncNewContent = job.SyncNewContent;
|
|
|
|
|
instance.ItemLimit = job.ItemLimit;
|
|
|
|
|
|
|
|
|
|
return _repo.Update(instance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<QueryResult<SyncJob>> GetJobs(SyncJobQuery query)
|
2014-07-18 00:21:35 +02:00
|
|
|
|
{
|
2014-07-26 19:30:15 +02:00
|
|
|
|
var result = _repo.GetJobs(query);
|
2014-07-18 00:21:35 +02:00
|
|
|
|
|
2014-12-31 07:24:49 +01:00
|
|
|
|
foreach (var item in result.Items)
|
|
|
|
|
{
|
|
|
|
|
await FillMetadata(item).ConfigureAwait(false);
|
|
|
|
|
}
|
2014-07-18 00:21:35 +02:00
|
|
|
|
|
2014-07-26 19:30:15 +02:00
|
|
|
|
return result;
|
2014-07-18 00:21:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-31 07:24:49 +01:00
|
|
|
|
private async Task FillMetadata(SyncJob job)
|
2014-07-18 00:21:35 +02:00
|
|
|
|
{
|
2014-12-13 04:56:30 +01:00
|
|
|
|
var item = job.RequestedItemIds
|
|
|
|
|
.Select(_libraryManager.GetItemById)
|
|
|
|
|
.FirstOrDefault(i => i != null);
|
2014-07-26 19:30:15 +02:00
|
|
|
|
|
2014-12-31 07:24:49 +01:00
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
2015-01-17 05:29:53 +01:00
|
|
|
|
var processor = GetSyncJobProcessor();
|
2014-12-31 07:24:49 +01:00
|
|
|
|
|
|
|
|
|
var user = _userManager.GetUserById(job.UserId);
|
|
|
|
|
|
|
|
|
|
item = (await processor
|
|
|
|
|
.GetItemsForSync(job.Category, job.ParentId, job.RequestedItemIds, user, job.UnwatchedOnly).ConfigureAwait(false))
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-26 19:30:15 +02: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);
|
2014-12-31 07:24:49 +01:00
|
|
|
|
var itemWithImage = item;
|
|
|
|
|
|
|
|
|
|
if (primaryImage == null)
|
|
|
|
|
{
|
|
|
|
|
var parentWithImage = item.Parents.FirstOrDefault(i => i.HasImage(ImageType.Primary));
|
|
|
|
|
|
|
|
|
|
if (parentWithImage != null)
|
|
|
|
|
{
|
|
|
|
|
itemWithImage = parentWithImage;
|
|
|
|
|
primaryImage = parentWithImage.GetImageInfo(ImageType.Primary, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-26 19:30:15 +02:00
|
|
|
|
|
|
|
|
|
if (primaryImage != null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-12-31 07:24:49 +01:00
|
|
|
|
job.PrimaryImageTag = _imageProcessor.GetImageCacheTag(itemWithImage, ImageType.Primary);
|
|
|
|
|
job.PrimaryImageItemId = itemWithImage.Id.ToString("N");
|
2014-07-26 19:30:15 +02:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting image info", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-18 00:21:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-31 07:24:49 +01:00
|
|
|
|
private void FillMetadata(SyncJobItem jobItem)
|
|
|
|
|
{
|
|
|
|
|
var item = _libraryManager.GetItemById(jobItem.ItemId);
|
|
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var primaryImage = item.GetImageInfo(ImageType.Primary, 0);
|
|
|
|
|
var itemWithImage = item;
|
|
|
|
|
|
|
|
|
|
if (primaryImage == null)
|
|
|
|
|
{
|
|
|
|
|
var parentWithImage = item.Parents.FirstOrDefault(i => i.HasImage(ImageType.Primary));
|
|
|
|
|
|
|
|
|
|
if (parentWithImage != null)
|
|
|
|
|
{
|
|
|
|
|
itemWithImage = parentWithImage;
|
|
|
|
|
primaryImage = parentWithImage.GetImageInfo(ImageType.Primary, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (primaryImage != null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
jobItem.PrimaryImageTag = _imageProcessor.GetImageCacheTag(itemWithImage, ImageType.Primary);
|
|
|
|
|
jobItem.PrimaryImageItemId = itemWithImage.Id.ToString("N");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting image info", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-21 04:54:45 +01:00
|
|
|
|
public async Task CancelJob(string id)
|
2014-07-18 00:21:35 +02:00
|
|
|
|
{
|
2015-01-21 04:54:45 +01:00
|
|
|
|
var job = GetJob(id);
|
|
|
|
|
|
|
|
|
|
if (job == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Job not found.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await _repo.DeleteJob(id).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (SyncJobCancelled != null)
|
|
|
|
|
{
|
|
|
|
|
EventHelper.FireEventIfNotNull(SyncJobCancelled, this, new GenericEventArgs<SyncJob>
|
|
|
|
|
{
|
|
|
|
|
Argument = job
|
|
|
|
|
|
|
|
|
|
}, _logger);
|
|
|
|
|
}
|
2014-07-18 00:21:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-26 19:30:15 +02:00
|
|
|
|
public SyncJob GetJob(string id)
|
2014-07-18 00:21:35 +02:00
|
|
|
|
{
|
2014-07-26 19:30:15 +02:00
|
|
|
|
return _repo.GetJob(id);
|
2014-07-18 00:21:35 +02:00
|
|
|
|
}
|
2014-07-22 03:29:06 +02:00
|
|
|
|
|
2014-07-22 18:36:34 +02:00
|
|
|
|
public IEnumerable<SyncTarget> GetSyncTargets(string userId)
|
|
|
|
|
{
|
|
|
|
|
return _providers
|
2014-07-26 19:30:15 +02:00
|
|
|
|
.SelectMany(i => GetSyncTargets(i, userId))
|
2014-07-22 18:36:34 +02:00
|
|
|
|
.OrderBy(i => i.Name);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-26 19:30:15 +02:00
|
|
|
|
private IEnumerable<SyncTarget> GetSyncTargets(ISyncProvider provider, string userId)
|
2014-07-22 18:36:34 +02:00
|
|
|
|
{
|
2014-12-31 07:24:49 +01:00
|
|
|
|
return provider.GetSyncTargets(userId).Select(i => new SyncTarget
|
2014-07-22 18:36:34 +02:00
|
|
|
|
{
|
|
|
|
|
Name = i.Name,
|
2014-12-24 07:28:40 +01:00
|
|
|
|
Id = GetSyncTargetId(provider, i)
|
2014-07-22 18:36:34 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-24 07:28:40 +01:00
|
|
|
|
private string GetSyncTargetId(ISyncProvider provider, SyncTarget target)
|
2014-12-13 04:56:30 +01:00
|
|
|
|
{
|
2014-12-24 07:28:40 +01:00
|
|
|
|
var hasUniqueId = provider as IHasUniqueTargetIds;
|
|
|
|
|
|
|
|
|
|
if (hasUniqueId != null)
|
|
|
|
|
{
|
|
|
|
|
return target.Id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var providerId = GetSyncProviderId(provider);
|
2014-12-13 04:56:30 +01:00
|
|
|
|
return (providerId + "-" + target.Id).GetMD5().ToString("N");
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-22 18:36:34 +02: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-11 07:20:28 +01:00
|
|
|
|
return (provider.GetType().Name).GetMD5().ToString("N");
|
2014-07-22 18:36:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SupportsSync(BaseItem item)
|
2014-07-22 03:29:06 +02:00
|
|
|
|
{
|
2014-12-13 04:56:30 +01: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 18:36:34 +02:00
|
|
|
|
{
|
2014-12-11 07:20:28 +01:00
|
|
|
|
if (item.LocationType == LocationType.Virtual)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-13 04:56:30 +01:00
|
|
|
|
if (!item.RunTimeTicks.HasValue)
|
2014-07-22 18:36:34 +02:00
|
|
|
|
{
|
2014-12-13 04:56:30 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var video = item as Video;
|
|
|
|
|
if (video != null)
|
|
|
|
|
{
|
|
|
|
|
if (video.VideoType == VideoType.Iso)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-07-22 18:36:34 +02:00
|
|
|
|
|
2014-12-13 04:56:30 +01:00
|
|
|
|
if (video.IsStacked)
|
2014-07-22 18:36:34 +02:00
|
|
|
|
{
|
2014-12-13 04:56:30 +01:00
|
|
|
|
return false;
|
2014-07-22 18:36:34 +02:00
|
|
|
|
}
|
2014-12-13 04:56:30 +01:00
|
|
|
|
}
|
2014-07-22 18:36:34 +02:00
|
|
|
|
|
2014-12-13 04:56:30 +01:00
|
|
|
|
var game = item as Game;
|
|
|
|
|
if (game != null)
|
|
|
|
|
{
|
|
|
|
|
if (game.IsMultiPart)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-07-22 18:36:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-26 18:45:06 +01:00
|
|
|
|
if (item is LiveTvChannel || item is IChannelItem || item is ILiveTvRecording)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-27 07:24:46 +01:00
|
|
|
|
// It would be nice to support these later
|
|
|
|
|
if (item is Game || item is Book)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-13 04:56:30 +01:00
|
|
|
|
return true;
|
2014-07-22 18:36:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-20 07:06:27 +01:00
|
|
|
|
return item.LocationType == LocationType.FileSystem || item is Season || item is ILiveTvRecording;
|
2014-07-26 19:30:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetDefaultName(BaseItem item)
|
|
|
|
|
{
|
|
|
|
|
return item.Name;
|
|
|
|
|
}
|
2014-12-13 04:56:30 +01: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-17 06:30:31 +01:00
|
|
|
|
|
|
|
|
|
public async Task ReportSyncJobItemTransferred(string id)
|
|
|
|
|
{
|
|
|
|
|
var jobItem = _repo.GetJobItem(id);
|
|
|
|
|
|
2014-12-30 17:36:49 +01:00
|
|
|
|
jobItem.Status = SyncJobItemStatus.Synced;
|
2014-12-17 06:30:31 +01:00
|
|
|
|
jobItem.Progress = 100;
|
|
|
|
|
|
2015-01-17 05:29:53 +01:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(jobItem.TemporaryPath))
|
2015-01-06 04:25:23 +01:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-01-17 05:29:53 +01:00
|
|
|
|
_fileSystem.DeleteDirectory(jobItem.TemporaryPath, true);
|
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
|
{
|
2015-01-06 04:25:23 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error deleting temporary job file: {0}", ex, jobItem.OutputPath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-17 06:30:31 +01:00
|
|
|
|
await _repo.Update(jobItem).ConfigureAwait(false);
|
|
|
|
|
|
2015-01-17 05:29:53 +01:00
|
|
|
|
var processor = GetSyncJobProcessor();
|
2014-12-17 06:30:31 +01:00
|
|
|
|
|
|
|
|
|
await processor.UpdateJobStatus(jobItem.JobId).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-17 05:29:53 +01:00
|
|
|
|
private SyncJobProcessor GetSyncJobProcessor()
|
|
|
|
|
{
|
|
|
|
|
return new SyncJobProcessor(_libraryManager, _repo, this, _logger, _userManager, _tvSeriesManager, _mediaEncoder(), _subtitleEncoder(), _config, _fileSystem);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-17 06:30:31 +01:00
|
|
|
|
public SyncJobItem GetJobItem(string id)
|
|
|
|
|
{
|
|
|
|
|
return _repo.GetJobItem(id);
|
|
|
|
|
}
|
2014-12-17 06:52:34 +01:00
|
|
|
|
|
|
|
|
|
public QueryResult<SyncJobItem> GetJobItems(SyncJobItemQuery query)
|
|
|
|
|
{
|
2014-12-31 07:24:49 +01:00
|
|
|
|
var result = _repo.GetJobItems(query);
|
|
|
|
|
|
|
|
|
|
if (query.AddMetadata)
|
|
|
|
|
{
|
|
|
|
|
result.Items.ForEach(FillMetadata);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2014-12-17 06:52:34 +01:00
|
|
|
|
}
|
2014-12-26 18:45:06 +01:00
|
|
|
|
|
2014-12-27 23:52:41 +01:00
|
|
|
|
private SyncedItem GetJobItemInfo(SyncJobItem jobItem)
|
2014-12-26 18:45:06 +01:00
|
|
|
|
{
|
|
|
|
|
var job = _repo.GetJob(jobItem.JobId);
|
|
|
|
|
|
|
|
|
|
var libraryItem = _libraryManager.GetItemById(jobItem.ItemId);
|
|
|
|
|
|
2015-02-03 05:54:52 +01:00
|
|
|
|
if (libraryItem == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-26 18:45:06 +01:00
|
|
|
|
var syncedItem = new SyncedItem
|
|
|
|
|
{
|
|
|
|
|
SyncJobId = jobItem.JobId,
|
|
|
|
|
SyncJobItemId = jobItem.Id,
|
|
|
|
|
ServerId = _appHost.SystemId,
|
2015-01-17 05:29:53 +01:00
|
|
|
|
UserId = job.UserId,
|
|
|
|
|
AdditionalFiles = jobItem.AdditionalFiles.Select(i => new ItemFileInfo
|
|
|
|
|
{
|
|
|
|
|
ImageType = i.ImageType,
|
|
|
|
|
Name = i.Name,
|
|
|
|
|
Type = i.Type,
|
|
|
|
|
Index = i.Index
|
|
|
|
|
|
|
|
|
|
}).ToList()
|
2014-12-26 18:45:06 +01:00
|
|
|
|
};
|
|
|
|
|
|
2014-12-28 07:21:39 +01:00
|
|
|
|
var dtoOptions = new DtoOptions();
|
|
|
|
|
|
|
|
|
|
// Remove some bloat
|
|
|
|
|
dtoOptions.Fields.Remove(ItemFields.MediaStreams);
|
|
|
|
|
dtoOptions.Fields.Remove(ItemFields.IndexOptions);
|
|
|
|
|
dtoOptions.Fields.Remove(ItemFields.MediaSourceCount);
|
|
|
|
|
dtoOptions.Fields.Remove(ItemFields.OriginalPrimaryImageAspectRatio);
|
|
|
|
|
dtoOptions.Fields.Remove(ItemFields.Path);
|
|
|
|
|
dtoOptions.Fields.Remove(ItemFields.SeriesGenres);
|
|
|
|
|
dtoOptions.Fields.Remove(ItemFields.Settings);
|
|
|
|
|
dtoOptions.Fields.Remove(ItemFields.SyncInfo);
|
|
|
|
|
|
|
|
|
|
syncedItem.Item = _dtoService().GetBaseItemDto(libraryItem, dtoOptions);
|
2014-12-26 18:45:06 +01:00
|
|
|
|
|
|
|
|
|
var mediaSource = syncedItem.Item.MediaSources
|
|
|
|
|
.FirstOrDefault(i => string.Equals(i.Id, jobItem.MediaSourceId));
|
|
|
|
|
|
2015-01-17 05:29:53 +01:00
|
|
|
|
syncedItem.Item.MediaSources = new List<MediaSourceInfo>();
|
2015-01-21 04:54:45 +01:00
|
|
|
|
|
2014-12-26 18:45:06 +01:00
|
|
|
|
// This will be null for items that are not audio/video
|
|
|
|
|
if (mediaSource == null)
|
|
|
|
|
{
|
|
|
|
|
syncedItem.OriginalFileName = Path.GetFileName(libraryItem.Path);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
syncedItem.OriginalFileName = Path.GetFileName(mediaSource.Path);
|
2015-01-17 05:29:53 +01:00
|
|
|
|
syncedItem.Item.MediaSources.Add(mediaSource);
|
2014-12-26 18:45:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return syncedItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task ReportOfflineAction(UserAction action)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
}
|
2014-12-27 23:52:41 +01:00
|
|
|
|
|
|
|
|
|
public List<SyncedItem> GetReadySyncItems(string targetId)
|
|
|
|
|
{
|
|
|
|
|
var jobItemResult = GetJobItems(new SyncJobItemQuery
|
|
|
|
|
{
|
|
|
|
|
TargetId = targetId,
|
2014-12-30 20:16:01 +01:00
|
|
|
|
Statuses = new List<SyncJobItemStatus> { SyncJobItemStatus.Transferring }
|
2014-12-27 23:52:41 +01:00
|
|
|
|
});
|
|
|
|
|
|
2015-02-03 05:54:52 +01:00
|
|
|
|
return jobItemResult.Items
|
|
|
|
|
.Select(GetJobItemInfo)
|
|
|
|
|
.Where(i => i != null)
|
2014-12-28 07:21:39 +01:00
|
|
|
|
.ToList();
|
2014-12-27 23:52:41 +01:00
|
|
|
|
}
|
2014-12-29 06:56:55 +01:00
|
|
|
|
|
|
|
|
|
public async Task<SyncDataResponse> SyncData(SyncDataRequest request)
|
|
|
|
|
{
|
2014-12-30 17:36:49 +01:00
|
|
|
|
var jobItemResult = GetJobItems(new SyncJobItemQuery
|
|
|
|
|
{
|
|
|
|
|
TargetId = request.TargetId,
|
2014-12-30 20:16:01 +01:00
|
|
|
|
Statuses = new List<SyncJobItemStatus> { SyncJobItemStatus.Synced }
|
2014-12-30 17:36:49 +01:00
|
|
|
|
});
|
|
|
|
|
|
2014-12-30 20:16:01 +01:00
|
|
|
|
var response = new SyncDataResponse();
|
|
|
|
|
|
2014-12-30 17:36:49 +01:00
|
|
|
|
foreach (var jobItem in jobItemResult.Items)
|
|
|
|
|
{
|
2014-12-30 20:16:01 +01:00
|
|
|
|
if (request.LocalItemIds.Contains(jobItem.ItemId, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
var job = _repo.GetJob(jobItem.JobId);
|
|
|
|
|
var user = _userManager.GetUserById(job.UserId);
|
|
|
|
|
|
2015-01-21 04:54:45 +01:00
|
|
|
|
if (jobItem.IsMarkedForRemoval)
|
|
|
|
|
{
|
|
|
|
|
// Tell the device to remove it since it has been marked for removal
|
|
|
|
|
response.ItemIdsToRemove.Add(jobItem.ItemId);
|
|
|
|
|
}
|
|
|
|
|
else if (user == null)
|
2014-12-30 20:16:01 +01:00
|
|
|
|
{
|
|
|
|
|
// Tell the device to remove it since the user is gone now
|
|
|
|
|
response.ItemIdsToRemove.Add(jobItem.ItemId);
|
|
|
|
|
}
|
|
|
|
|
else if (job.UnwatchedOnly)
|
|
|
|
|
{
|
|
|
|
|
var libraryItem = _libraryManager.GetItemById(jobItem.ItemId);
|
|
|
|
|
|
|
|
|
|
if (IsLibraryItemAvailable(libraryItem))
|
|
|
|
|
{
|
|
|
|
|
if (libraryItem.IsPlayed(user) && libraryItem is Video)
|
|
|
|
|
{
|
|
|
|
|
// Tell the device to remove it since it has been played
|
|
|
|
|
response.ItemIdsToRemove.Add(jobItem.ItemId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Tell the device to remove it since it's no longer available
|
|
|
|
|
response.ItemIdsToRemove.Add(jobItem.ItemId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2014-12-30 17:36:49 +01:00
|
|
|
|
{
|
2014-12-30 20:16:01 +01:00
|
|
|
|
// Content is no longer on the device
|
2014-12-30 17:36:49 +01:00
|
|
|
|
jobItem.Status = SyncJobItemStatus.RemovedFromDevice;
|
|
|
|
|
await _repo.Update(jobItem).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-29 06:56:55 +01:00
|
|
|
|
|
2015-01-03 20:38:22 +01:00
|
|
|
|
// Now check each item that's on the device
|
|
|
|
|
foreach (var itemId in request.LocalItemIds)
|
|
|
|
|
{
|
|
|
|
|
// See if it's already marked for removal
|
|
|
|
|
if (response.ItemIdsToRemove.Contains(itemId, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If there isn't a sync job for this item, mark it for removal
|
|
|
|
|
if (!jobItemResult.Items.Any(i => string.Equals(itemId, i.ItemId, StringComparison.OrdinalIgnoreCase)))
|
|
|
|
|
{
|
|
|
|
|
response.ItemIdsToRemove.Add(itemId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-30 20:16:01 +01:00
|
|
|
|
response.ItemIdsToRemove = response.ItemIdsToRemove.Distinct(StringComparer.OrdinalIgnoreCase).ToList();
|
2014-12-31 07:24:49 +01:00
|
|
|
|
|
2014-12-29 06:56:55 +01:00
|
|
|
|
return response;
|
|
|
|
|
}
|
2014-12-30 20:16:01 +01:00
|
|
|
|
|
|
|
|
|
private bool IsLibraryItemAvailable(BaseItem item)
|
|
|
|
|
{
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Make sure it hasn't been deleted
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-01-21 04:54:45 +01:00
|
|
|
|
|
|
|
|
|
public async Task ReEnableJobItem(string id)
|
|
|
|
|
{
|
|
|
|
|
var jobItem = _repo.GetJobItem(id);
|
|
|
|
|
|
|
|
|
|
if (jobItem.Status != SyncJobItemStatus.Failed && jobItem.Status != SyncJobItemStatus.Cancelled)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Operation is not valid for this job item");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jobItem.Status = SyncJobItemStatus.Queued;
|
|
|
|
|
jobItem.Progress = 0;
|
|
|
|
|
jobItem.IsMarkedForRemoval = false;
|
|
|
|
|
|
|
|
|
|
await _repo.Update(jobItem).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var processor = GetSyncJobProcessor();
|
|
|
|
|
|
|
|
|
|
await processor.UpdateJobStatus(jobItem.JobId).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task CancelJobItem(string id)
|
|
|
|
|
{
|
|
|
|
|
var jobItem = _repo.GetJobItem(id);
|
|
|
|
|
|
|
|
|
|
if (jobItem.Status != SyncJobItemStatus.Queued && jobItem.Status != SyncJobItemStatus.Transferring && jobItem.Status != SyncJobItemStatus.Converting)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Operation is not valid for this job item");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jobItem.Status = SyncJobItemStatus.Cancelled;
|
|
|
|
|
jobItem.Progress = 0;
|
|
|
|
|
jobItem.IsMarkedForRemoval = true;
|
|
|
|
|
|
|
|
|
|
await _repo.Update(jobItem).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var processor = GetSyncJobProcessor();
|
|
|
|
|
|
|
|
|
|
await processor.UpdateJobStatus(jobItem.JobId).ConfigureAwait(false);
|
|
|
|
|
}
|
2015-01-21 05:56:00 +01:00
|
|
|
|
|
|
|
|
|
public async Task MarkJobItemForRemoval(string id)
|
|
|
|
|
{
|
|
|
|
|
var jobItem = _repo.GetJobItem(id);
|
|
|
|
|
|
|
|
|
|
if (jobItem.Status != SyncJobItemStatus.Synced)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Operation is not valid for this job item");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jobItem.IsMarkedForRemoval = true;
|
|
|
|
|
|
|
|
|
|
await _repo.Update(jobItem).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var processor = GetSyncJobProcessor();
|
|
|
|
|
|
|
|
|
|
await processor.UpdateJobStatus(jobItem.JobId).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task UnmarkJobItemForRemoval(string id)
|
|
|
|
|
{
|
|
|
|
|
var jobItem = _repo.GetJobItem(id);
|
|
|
|
|
|
|
|
|
|
if (jobItem.Status != SyncJobItemStatus.Synced)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Operation is not valid for this job item");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jobItem.IsMarkedForRemoval = false;
|
|
|
|
|
|
|
|
|
|
await _repo.Update(jobItem).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var processor = GetSyncJobProcessor();
|
|
|
|
|
|
|
|
|
|
await processor.UpdateJobStatus(jobItem.JobId).ConfigureAwait(false);
|
|
|
|
|
}
|
2015-01-24 20:03:55 +01:00
|
|
|
|
|
|
|
|
|
public QueryResult<string> GetLibraryItemIds(SyncJobItemQuery query)
|
|
|
|
|
{
|
|
|
|
|
return _repo.GetLibraryItemIds(query);
|
|
|
|
|
}
|
2014-07-18 00:21:35 +02:00
|
|
|
|
}
|
|
|
|
|
}
|