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

1324 lines
46 KiB
C#
Raw Normal View History

2015-08-17 04:56:22 +00:00
using MediaBrowser.Common.Configuration;
2015-01-21 03:54:45 +00:00
using MediaBrowser.Common.Events;
2014-12-26 17:45:06 +00:00
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.IO;
2015-08-17 04:56:22 +00:00
using MediaBrowser.Common.ScheduledTasks;
2015-03-31 18:50:08 +00:00
using MediaBrowser.Controller;
2014-12-26 17:45:06 +00:00
using MediaBrowser.Controller.Channels;
2014-07-26 17:30:15 +00:00
using MediaBrowser.Controller.Drawing;
2014-12-26 17:45:06 +00:00
using MediaBrowser.Controller.Dto;
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;
2014-12-11 06:20:28 +00:00
using MediaBrowser.Controller.Entities.TV;
2014-07-26 17:30:15 +00:00
using MediaBrowser.Controller.Library;
2014-12-20 06:06:27 +00:00
using MediaBrowser.Controller.LiveTv;
2015-01-02 05:36:27 +00:00
using MediaBrowser.Controller.MediaEncoding;
2015-02-11 16:19:16 +00:00
using MediaBrowser.Controller.Playlists;
2014-07-22 16:36:34 +00:00
using MediaBrowser.Controller.Sync;
2014-12-30 16:36:49 +00:00
using MediaBrowser.Controller.TV;
2015-01-17 04:29:53 +00:00
using MediaBrowser.Model.Dto;
2014-07-22 16:36:34 +00:00
using MediaBrowser.Model.Entities;
2015-01-21 03:54:45 +00:00
using MediaBrowser.Model.Events;
2014-07-26 17:30:15 +00:00
using MediaBrowser.Model.Logging;
2014-07-17 22:21:35 +00:00
using MediaBrowser.Model.Querying;
2015-03-08 04:44:31 +00:00
using MediaBrowser.Model.Serialization;
2014-07-17 22:21:35 +00:00
using MediaBrowser.Model.Sync;
2014-12-26 17:45:06 +00:00
using MediaBrowser.Model.Users;
2014-07-26 17:30:15 +00:00
using MoreLinq;
2014-07-17 22:21:35 +00:00
using System;
2015-03-08 04:44:31 +00:00
using System.Collections.Concurrent;
2014-07-17 22:21:35 +00:00
using System.Collections.Generic;
2014-12-28 06:21:39 +00:00
using System.IO;
2014-07-22 01:29:06 +00:00
using System.Linq;
2015-01-13 05:40:27 +00:00
using System.Threading;
2014-07-17 22:21:35 +00:00
using System.Threading.Tasks;
2015-10-04 04:23:11 +00:00
using CommonIO;
2014-07-17 22:21:35 +00:00
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;
2014-12-13 03:56:30 +00:00
private readonly IUserManager _userManager;
2014-12-27 22:52:41 +00:00
private readonly Func<IDtoService> _dtoService;
2015-03-31 18:50:08 +00:00
private readonly IServerApplicationHost _appHost;
2014-12-30 16:36:49 +00:00
private readonly ITVSeriesManager _tvSeriesManager;
2015-01-03 19:38:22 +00:00
private readonly Func<IMediaEncoder> _mediaEncoder;
private readonly IFileSystem _fileSystem;
2015-01-16 20:54:37 +00:00
private readonly Func<ISubtitleEncoder> _subtitleEncoder;
2015-01-17 04:29:53 +00:00
private readonly IConfigurationManager _config;
2015-03-07 22:43:53 +00:00
private readonly IUserDataManager _userDataManager;
private readonly Func<IMediaSourceManager> _mediaSourceManager;
2015-08-14 14:42:40 +00:00
private readonly IJsonSerializer _json;
private readonly ITaskManager _taskManager;
2014-07-26 17:30:15 +00:00
private ISyncProvider[] _providers = { };
2015-01-28 04:04:26 +00:00
public event EventHandler<GenericEventArgs<SyncJobCreationResult>> SyncJobCreated;
2015-01-21 03:54:45 +00:00
public event EventHandler<GenericEventArgs<SyncJob>> SyncJobCancelled;
2015-02-04 19:13:00 +00:00
public event EventHandler<GenericEventArgs<SyncJob>> SyncJobUpdated;
2015-02-05 03:01:37 +00:00
public event EventHandler<GenericEventArgs<SyncJobItem>> SyncJobItemUpdated;
public event EventHandler<GenericEventArgs<SyncJobItem>> SyncJobItemCreated;
2015-01-21 03:54:45 +00:00
2015-08-14 14:42:40 +00:00
public SyncManager(ILibraryManager libraryManager, ISyncRepository repo, IImageProcessor imageProcessor, ILogger logger, IUserManager userManager, Func<IDtoService> dtoService, IServerApplicationHost appHost, ITVSeriesManager tvSeriesManager, Func<IMediaEncoder> mediaEncoder, IFileSystem fileSystem, Func<ISubtitleEncoder> subtitleEncoder, IConfigurationManager config, IUserDataManager userDataManager, Func<IMediaSourceManager> mediaSourceManager, IJsonSerializer json, ITaskManager taskManager)
2014-07-26 17:30:15 +00:00
{
_libraryManager = libraryManager;
_repo = repo;
_imageProcessor = imageProcessor;
_logger = logger;
2014-12-13 03:56:30 +00:00
_userManager = userManager;
2014-12-26 17:45:06 +00:00
_dtoService = dtoService;
_appHost = appHost;
2014-12-30 16:36:49 +00:00
_tvSeriesManager = tvSeriesManager;
2015-01-03 19:38:22 +00:00
_mediaEncoder = mediaEncoder;
_fileSystem = fileSystem;
2015-01-16 20:54:37 +00:00
_subtitleEncoder = subtitleEncoder;
2015-01-17 04:29:53 +00:00
_config = config;
2015-02-17 18:39:37 +00:00
_userDataManager = userDataManager;
2015-03-07 22:43:53 +00:00
_mediaSourceManager = mediaSourceManager;
2015-03-08 04:44:31 +00:00
_json = json;
2015-08-14 14:42:40 +00:00
_taskManager = taskManager;
2014-07-26 17:30:15 +00:00
}
2014-07-22 01:29:06 +00:00
public void AddParts(IEnumerable<ISyncProvider> providers)
{
_providers = providers.ToArray();
}
2015-03-07 17:19:44 +00:00
public IEnumerable<IServerSyncProvider> ServerSyncProviders
{
get { return _providers.OfType<IServerSyncProvider>(); }
}
2015-03-08 04:44:31 +00:00
private readonly ConcurrentDictionary<string, ISyncDataProvider> _dataProviders =
new ConcurrentDictionary<string, ISyncDataProvider>(StringComparer.OrdinalIgnoreCase);
2015-03-12 04:47:16 +00:00
2015-03-08 04:44:31 +00:00
public ISyncDataProvider GetDataProvider(IServerSyncProvider provider, SyncTarget target)
{
2015-03-31 18:50:08 +00:00
return _dataProviders.GetOrAdd(target.Id, key => new TargetDataProvider(provider, target, _appHost, _logger, _json, _fileSystem, _config.CommonApplicationPaths));
2015-03-08 04:44:31 +00:00
}
2014-07-26 17:30:15 +00:00
public async Task<SyncJobCreationResult> CreateJob(SyncJobRequest request)
2014-07-17 22:21:35 +00:00
{
2015-01-17 04:29:53 +00:00
var processor = GetSyncJobProcessor();
2014-12-13 03:56:30 +00:00
var user = _userManager.GetUserById(request.UserId);
2014-12-16 05:01:57 +00:00
2014-12-30 16:36:49 +00:00
var items = (await processor
.GetItemsForSync(request.Category, request.ParentId, request.ItemIds, user, request.UnwatchedOnly).ConfigureAwait(false))
2014-12-11 06:20:28 +00:00
.ToList();
if (items.Any(i => !SupportsSync(i)))
{
throw new ArgumentException("Item does not support sync.");
}
2014-07-26 17:30:15 +00:00
2014-12-30 16:36:49 +00:00
if (string.IsNullOrWhiteSpace(request.Name))
2014-07-26 17:30:15 +00:00
{
2014-12-31 06:24:49 +00:00
if (request.ItemIds.Count == 1)
2014-12-30 16:36:49 +00:00
{
request.Name = GetDefaultName(_libraryManager.GetItemById(request.ItemIds[0]));
}
2014-07-26 17:30:15 +00:00
}
if (string.IsNullOrWhiteSpace(request.Name))
{
2015-08-14 14:42:40 +00:00
request.Name = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString();
2014-07-26 17:30:15 +00:00
}
var target = GetSyncTargets(request.UserId)
2014-12-24 06:28:40 +00:00
.FirstOrDefault(i => string.Equals(request.TargetId, i.Id));
2015-02-03 18:14:53 +00:00
2014-12-24 06:28:40 +00:00
if (target == null)
{
throw new ArgumentException("Sync target not found.");
}
2014-07-26 17:30:15 +00:00
var jobId = Guid.NewGuid().ToString("N");
2015-02-03 18:14:53 +00:00
2015-03-13 15:54:20 +00:00
if (string.IsNullOrWhiteSpace(request.Quality))
{
request.Quality = GetQualityOptions(request.TargetId)
.Where(i => i.IsDefault)
.Select(i => i.Id)
.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i));
}
2014-07-26 17:30:15 +00:00
var job = new SyncJob
{
Id = jobId,
Name = request.Name,
TargetId = target.Id,
UserId = request.UserId,
UnwatchedOnly = request.UnwatchedOnly,
2014-12-11 06:20:28 +00:00
ItemLimit = request.ItemLimit,
2014-12-30 16:36:49 +00:00
RequestedItemIds = request.ItemIds ?? new List<string> { },
2014-07-26 17:30:15 +00:00
DateCreated = DateTime.UtcNow,
2014-07-27 22:01:29 +00:00
DateLastModified = DateTime.UtcNow,
2014-12-11 06:20:28 +00:00
SyncNewContent = request.SyncNewContent,
ItemCount = items.Count,
2014-12-16 05:01:57 +00:00
Category = request.Category,
2015-03-12 04:03:45 +00:00
ParentId = request.ParentId,
2015-03-15 04:17:35 +00:00
Quality = request.Quality,
2015-03-15 19:10:27 +00:00
Profile = request.Profile,
Bitrate = request.Bitrate
2014-07-26 17:30:15 +00:00
};
2015-02-18 05:01:13 +00:00
if (!request.Category.HasValue && request.ItemIds != null)
2014-12-13 03:56:30 +00:00
{
2015-02-18 05:01:13 +00:00
var requestedItems = request.ItemIds
.Select(_libraryManager.GetItemById)
.Where(i => i != null);
// It's just a static list
if (!requestedItems.Any(i => i.IsFolder || i is IItemByName))
{
job.SyncNewContent = false;
}
2014-12-13 03:56:30 +00:00
}
2014-07-26 17:30:15 +00:00
await _repo.Create(job).ConfigureAwait(false);
2014-12-13 03:56:30 +00:00
await processor.EnsureJobItems(job).ConfigureAwait(false);
2015-01-17 04:29:53 +00:00
2015-01-13 05:40:27 +00:00
// If it already has a converting status then is must have been aborted during conversion
2015-04-04 01:24:49 +00:00
var jobItemsResult = GetJobItems(new SyncJobItemQuery
2015-01-13 05:40:27 +00:00
{
2015-04-04 01:24:49 +00:00
Statuses = new[] { SyncJobItemStatus.Queued, SyncJobItemStatus.Converting },
JobId = jobId,
AddMetadata = false
2015-01-13 05:40:27 +00:00
});
await processor.SyncJobItems(jobItemsResult.Items, false, new Progress<double>(), CancellationToken.None)
.ConfigureAwait(false);
2014-12-13 03:56:30 +00:00
2015-04-04 01:24:49 +00:00
jobItemsResult = GetJobItems(new SyncJobItemQuery
2015-01-27 22:45:59 +00:00
{
2015-04-04 01:24:49 +00:00
Statuses = new[] { SyncJobItemStatus.Queued, SyncJobItemStatus.Converting },
JobId = jobId,
AddMetadata = false
2015-01-27 22:45:59 +00:00
});
2015-01-28 04:04:26 +00:00
var returnResult = new SyncJobCreationResult
2014-07-26 17:30:15 +00:00
{
2015-01-27 22:45:59 +00:00
Job = GetJob(jobId),
JobItems = jobItemsResult.Items.ToList()
2014-07-26 17:30:15 +00:00
};
2015-01-28 04:04:26 +00:00
if (SyncJobCreated != null)
{
EventHelper.FireEventIfNotNull(SyncJobCreated, this, new GenericEventArgs<SyncJobCreationResult>
{
Argument = returnResult
}, _logger);
}
2015-08-14 14:42:40 +00:00
if (returnResult.JobItems.Any(i => i.Status == SyncJobItemStatus.Queued || i.Status == SyncJobItemStatus.Converting))
{
_taskManager.QueueScheduledTask<SyncConvertScheduledTask>();
}
2015-06-07 19:15:27 +00:00
2015-01-28 04:04:26 +00:00
return returnResult;
2014-07-17 22:21:35 +00:00
}
2015-02-04 20:16:23 +00:00
public async Task UpdateJob(SyncJob job)
2014-12-31 06:24:49 +00:00
{
// 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;
2015-03-15 04:39:29 +00:00
instance.Profile = job.Profile;
2014-12-31 06:24:49 +00:00
instance.UnwatchedOnly = job.UnwatchedOnly;
instance.SyncNewContent = job.SyncNewContent;
instance.ItemLimit = job.ItemLimit;
2015-02-04 20:16:23 +00:00
await _repo.Update(instance).ConfigureAwait(false);
OnSyncJobUpdated(instance);
}
internal void OnSyncJobUpdated(SyncJob job)
{
if (SyncJobUpdated != null)
{
EventHelper.FireEventIfNotNull(SyncJobUpdated, this, new GenericEventArgs<SyncJob>
{
Argument = job
}, _logger);
}
2014-12-31 06:24:49 +00:00
}
2015-02-05 03:01:37 +00:00
internal async Task UpdateSyncJobItemInternal(SyncJobItem jobItem)
{
await _repo.Update(jobItem).ConfigureAwait(false);
2015-02-11 16:19:16 +00:00
2015-02-05 03:01:37 +00:00
if (SyncJobUpdated != null)
{
EventHelper.FireEventIfNotNull(SyncJobItemUpdated, this, new GenericEventArgs<SyncJobItem>
{
Argument = jobItem
}, _logger);
}
}
internal void OnSyncJobItemCreated(SyncJobItem job)
{
if (SyncJobUpdated != null)
{
EventHelper.FireEventIfNotNull(SyncJobItemCreated, this, new GenericEventArgs<SyncJobItem>
{
Argument = job
}, _logger);
}
}
2014-12-31 06:24:49 +00:00
public async Task<QueryResult<SyncJob>> GetJobs(SyncJobQuery query)
2014-07-17 22:21:35 +00:00
{
2014-07-26 17:30:15 +00:00
var result = _repo.GetJobs(query);
2014-07-17 22:21:35 +00:00
2014-12-31 06:24:49 +00:00
foreach (var item in result.Items)
{
await FillMetadata(item).ConfigureAwait(false);
}
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-12-31 06:24:49 +00:00
private async Task FillMetadata(SyncJob job)
2014-07-17 22:21:35 +00:00
{
2015-05-19 19:15:40 +00:00
var user = _userManager.GetUserById(job.UserId);
if (user == null)
{
return;
}
2015-02-04 20:16:23 +00:00
var target = GetSyncTargets(job.UserId)
.FirstOrDefault(i => string.Equals(i.Id, job.TargetId, StringComparison.OrdinalIgnoreCase));
if (target != null)
{
job.TargetName = target.Name;
}
2014-12-13 03:56:30 +00:00
var item = job.RequestedItemIds
.Select(_libraryManager.GetItemById)
.FirstOrDefault(i => i != null);
2014-07-26 17:30:15 +00:00
2014-12-31 06:24:49 +00:00
if (item == null)
{
2015-01-17 04:29:53 +00:00
var processor = GetSyncJobProcessor();
2014-12-31 06:24:49 +00:00
item = (await processor
.GetItemsForSync(job.Category, job.ParentId, job.RequestedItemIds, user, job.UnwatchedOnly).ConfigureAwait(false))
.FirstOrDefault();
}
2014-07-26 17:30:15 +00: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 06:24:49 +00: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 17:30:15 +00:00
if (primaryImage != null)
{
try
{
2014-12-31 06:24:49 +00:00
job.PrimaryImageTag = _imageProcessor.GetImageCacheTag(itemWithImage, ImageType.Primary);
job.PrimaryImageItemId = itemWithImage.Id.ToString("N");
2014-07-26 17:30:15 +00:00
}
catch (Exception ex)
{
_logger.ErrorException("Error getting image info", ex);
}
}
}
2014-07-17 22:21:35 +00:00
}
2014-12-31 06:24:49 +00: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 03:54:45 +00:00
public async Task CancelJob(string id)
2014-07-17 22:21:35 +00:00
{
2015-01-21 03:54:45 +00:00
var job = GetJob(id);
if (job == null)
{
throw new ArgumentException("Job not found.");
}
await _repo.DeleteJob(id).ConfigureAwait(false);
var path = GetSyncJobProcessor().GetTemporaryPath(id);
try
{
_fileSystem.DeleteDirectory(path, true);
2015-02-03 21:06:56 +00:00
}
catch (DirectoryNotFoundException)
{
}
catch (Exception ex)
{
_logger.ErrorException("Error deleting directory {0}", ex, path);
}
2015-01-21 03:54:45 +00:00
if (SyncJobCancelled != null)
{
EventHelper.FireEventIfNotNull(SyncJobCancelled, this, new GenericEventArgs<SyncJob>
{
Argument = job
}, _logger);
}
2014-07-17 22:21:35 +00:00
}
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);
}
2015-02-28 14:35:12 +00:00
private IEnumerable<SyncTarget> GetSyncTargets(ISyncProvider provider)
{
return provider.GetAllSyncTargets().Select(i => new SyncTarget
{
Name = i.Name,
Id = GetSyncTargetId(provider, i)
});
}
2014-07-26 17:30:15 +00:00
private IEnumerable<SyncTarget> GetSyncTargets(ISyncProvider provider, string userId)
2014-07-22 16:36:34 +00:00
{
2014-12-31 06:24:49 +00:00
return provider.GetSyncTargets(userId).Select(i => new SyncTarget
2014-07-22 16:36:34 +00:00
{
Name = i.Name,
2014-12-24 06:28:40 +00:00
Id = GetSyncTargetId(provider, i)
2014-07-22 16:36:34 +00:00
});
}
2014-12-24 06:28:40 +00:00
private string GetSyncTargetId(ISyncProvider provider, SyncTarget target)
2014-12-13 03:56:30 +00:00
{
2014-12-24 06:28:40 +00:00
var hasUniqueId = provider as IHasUniqueTargetIds;
if (hasUniqueId != null)
{
return target.Id;
}
2015-03-07 17:19:44 +00:00
return target.Id;
//var providerId = GetSyncProviderId(provider);
//return (providerId + "-" + target.Id).GetMD5().ToString("N");
2014-12-13 03:56:30 +00:00
}
2014-07-22 16:36:34 +00:00
private string GetSyncProviderId(ISyncProvider provider)
{
2014-12-11 06:20:28 +00:00
return (provider.GetType().Name).GetMD5().ToString("N");
2014-07-22 16:36:34 +00:00
}
public bool SupportsSync(BaseItem item)
2014-07-22 01:29:06 +00:00
{
2015-02-11 16:19:16 +00:00
if (item is Playlist)
{
return true;
}
2015-02-12 03:54:31 +00:00
if (item is Person)
{
return false;
}
if (item is Year)
{
return false;
}
2014-12-13 03:56:30 +00: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 16:36:34 +00:00
{
2014-12-11 06:20:28 +00:00
if (item.LocationType == LocationType.Virtual)
{
return false;
}
2014-12-13 03:56:30 +00:00
var video = item as Video;
if (video != null)
{
2015-02-09 20:54:04 +00:00
if (video.IsPlaceHolder)
{
return false;
}
if (video.IsShortcut)
{
return false;
}
2014-12-13 03:56:30 +00:00
}
2014-07-22 16:36:34 +00:00
2015-04-12 01:38:38 +00:00
if (item is LiveTvChannel || item is IChannelItem)
2014-12-26 17:45:06 +00:00
{
return false;
}
2014-12-13 03:56:30 +00:00
return true;
2014-07-22 16:36:34 +00:00
}
2015-04-12 01:38:38 +00:00
return item.LocationType == LocationType.FileSystem || item is Season;
2014-07-26 17:30:15 +00:00
}
private string GetDefaultName(BaseItem item)
{
return item.Name;
}
2014-12-13 03:56:30 +00:00
2014-12-17 05:30:31 +00:00
public async Task ReportSyncJobItemTransferred(string id)
{
var jobItem = _repo.GetJobItem(id);
2014-12-30 16:36:49 +00:00
jobItem.Status = SyncJobItemStatus.Synced;
2014-12-17 05:30:31 +00:00
jobItem.Progress = 100;
2015-01-17 04:29:53 +00:00
if (!string.IsNullOrWhiteSpace(jobItem.TemporaryPath))
2015-01-06 03:25:23 +00:00
{
try
{
2015-01-17 04:29:53 +00:00
_fileSystem.DeleteDirectory(jobItem.TemporaryPath, true);
}
catch (DirectoryNotFoundException)
{
2015-01-06 03:25:23 +00:00
}
catch (Exception ex)
{
_logger.ErrorException("Error deleting temporary job file: {0}", ex, jobItem.OutputPath);
}
}
2015-02-05 03:01:37 +00:00
await UpdateSyncJobItemInternal(jobItem).ConfigureAwait(false);
2014-12-17 05:30:31 +00:00
2015-01-17 04:29:53 +00:00
var processor = GetSyncJobProcessor();
2014-12-17 05:30:31 +00:00
await processor.UpdateJobStatus(jobItem.JobId).ConfigureAwait(false);
}
2015-01-17 04:29:53 +00:00
private SyncJobProcessor GetSyncJobProcessor()
{
2015-03-07 22:43:53 +00:00
return new SyncJobProcessor(_libraryManager, _repo, this, _logger, _userManager, _tvSeriesManager, _mediaEncoder(), _subtitleEncoder(), _config, _fileSystem, _mediaSourceManager());
2015-01-17 04:29:53 +00:00
}
2014-12-17 05:30:31 +00:00
public SyncJobItem GetJobItem(string id)
{
return _repo.GetJobItem(id);
}
2014-12-17 05:52:34 +00:00
public QueryResult<SyncJobItem> GetJobItems(SyncJobItemQuery query)
{
2014-12-31 06:24:49 +00:00
var result = _repo.GetJobItems(query);
if (query.AddMetadata)
{
result.Items.ForEach(FillMetadata);
}
return result;
2014-12-17 05:52:34 +00:00
}
2014-12-26 17:45:06 +00:00
2014-12-27 22:52:41 +00:00
private SyncedItem GetJobItemInfo(SyncJobItem jobItem)
2014-12-26 17:45:06 +00:00
{
var job = _repo.GetJob(jobItem.JobId);
2015-02-03 06:57:45 +00:00
if (job == null)
{
_logger.Error("GetJobItemInfo job id {0} no longer exists", jobItem.JobId);
return null;
}
2014-12-26 17:45:06 +00:00
var libraryItem = _libraryManager.GetItemById(jobItem.ItemId);
2015-02-03 04:54:52 +00:00
if (libraryItem == null)
{
2015-02-03 06:57:45 +00:00
_logger.Error("GetJobItemInfo library item with id {0} no longer exists", jobItem.ItemId);
2015-02-03 04:54:52 +00:00
return null;
}
2014-12-26 17:45:06 +00:00
var syncedItem = new SyncedItem
{
SyncJobId = jobItem.JobId,
SyncJobItemId = jobItem.Id,
ServerId = _appHost.SystemId,
2015-01-17 04:29:53 +00:00
UserId = job.UserId,
2015-03-25 23:13:15 +00:00
SyncJobName = job.Name,
SyncJobDateCreated = job.DateCreated,
2015-01-17 04:29:53 +00:00
AdditionalFiles = jobItem.AdditionalFiles.Select(i => new ItemFileInfo
{
ImageType = i.ImageType,
Name = i.Name,
Type = i.Type,
Index = i.Index
}).ToList()
2014-12-26 17:45:06 +00:00
};
2014-12-28 06:21:39 +00: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);
2015-03-08 05:37:48 +00:00
var mediaSource = jobItem.MediaSource;
2014-12-26 17:45:06 +00:00
2015-01-17 04:29:53 +00:00
syncedItem.Item.MediaSources = new List<MediaSourceInfo>();
2015-01-21 03:54:45 +00:00
2015-03-29 23:47:31 +00:00
syncedItem.OriginalFileName = Path.GetFileName(libraryItem.Path);
if (string.IsNullOrWhiteSpace(syncedItem.OriginalFileName))
2014-12-26 17:45:06 +00:00
{
2015-03-29 23:47:31 +00:00
syncedItem.OriginalFileName = Path.GetFileName(mediaSource.Path);
2014-12-26 17:45:06 +00:00
}
2015-03-29 23:47:31 +00:00
// This will be null for items that are not audio/video
if (mediaSource != null)
2014-12-26 17:45:06 +00:00
{
2015-03-30 19:57:37 +00:00
syncedItem.OriginalFileName = Path.ChangeExtension(syncedItem.OriginalFileName, Path.GetExtension(mediaSource.Path));
2015-01-17 04:29:53 +00:00
syncedItem.Item.MediaSources.Add(mediaSource);
2014-12-26 17:45:06 +00:00
}
2015-06-13 23:56:59 +00:00
if (string.IsNullOrWhiteSpace(syncedItem.OriginalFileName))
{
syncedItem.OriginalFileName = libraryItem.Name;
}
2014-12-26 17:45:06 +00:00
return syncedItem;
}
public Task ReportOfflineAction(UserAction action)
{
2015-02-17 18:39:37 +00:00
switch (action.Type)
{
case UserActionType.PlayedItem:
return ReportOfflinePlayedItem(action);
default:
throw new ArgumentException("Unexpected action type");
}
}
private Task ReportOfflinePlayedItem(UserAction action)
{
var item = _libraryManager.GetItemById(action.ItemId);
var userData = _userDataManager.GetUserData(new Guid(action.UserId), item.GetUserDataKey());
userData.LastPlayedDate = action.Date;
2015-02-17 18:42:46 +00:00
_userDataManager.UpdatePlayState(item, userData, action.PositionTicks);
2015-02-17 18:39:37 +00:00
return _userDataManager.SaveUserData(new Guid(action.UserId), item, userData, UserDataSaveReason.Import, CancellationToken.None);
2014-12-26 17:45:06 +00:00
}
2014-12-27 22:52:41 +00:00
2015-02-19 17:46:18 +00:00
public async Task<List<SyncedItem>> GetReadySyncItems(string targetId)
2014-12-27 22:52:41 +00:00
{
2015-02-19 17:46:18 +00:00
var processor = GetSyncJobProcessor();
await processor.SyncJobItems(targetId, false, new Progress<double>(), CancellationToken.None).ConfigureAwait(false);
2014-12-27 22:52:41 +00:00
var jobItemResult = GetJobItems(new SyncJobItemQuery
{
TargetId = targetId,
2015-03-29 22:38:32 +00:00
Statuses = new[]
2015-02-05 03:01:37 +00:00
{
2015-04-21 15:46:14 +00:00
SyncJobItemStatus.ReadyToTransfer,
SyncJobItemStatus.Transferring
2015-02-05 03:01:37 +00:00
}
2014-12-27 22:52:41 +00:00
});
2015-04-17 17:46:41 +00:00
var readyItems = jobItemResult.Items
2015-02-03 04:54:52 +00:00
.Select(GetJobItemInfo)
.Where(i => i != null)
2014-12-28 06:21:39 +00:00
.ToList();
2015-04-17 17:46:41 +00:00
_logger.Debug("Returning {0} ready sync items for targetId {1}", readyItems.Count, targetId);
return readyItems;
2014-12-27 22:52:41 +00:00
}
2014-12-29 05:56:55 +00:00
public async Task<SyncDataResponse> SyncData(SyncDataRequest request)
{
2015-03-31 18:50:08 +00:00
if (request.SyncJobItemIds != null)
{
return await SyncDataUsingSyncJobItemIds(request).ConfigureAwait(false);
}
2014-12-30 16:36:49 +00:00
var jobItemResult = GetJobItems(new SyncJobItemQuery
{
TargetId = request.TargetId,
2015-03-31 18:50:08 +00:00
Statuses = new[] { SyncJobItemStatus.Synced }
2014-12-30 16:36:49 +00:00
});
2014-12-30 19:16:01 +00:00
var response = new SyncDataResponse();
2014-12-30 16:36:49 +00:00
foreach (var jobItem in jobItemResult.Items)
{
2015-04-12 16:46:29 +00:00
var requiresSaving = false;
var removeFromDevice = false;
2015-04-22 13:28:45 +00:00
var libraryItem = _libraryManager.GetItemById(jobItem.ItemId);
2014-12-30 19:16:01 +00:00
if (request.LocalItemIds.Contains(jobItem.ItemId, StringComparer.OrdinalIgnoreCase))
{
var job = _repo.GetJob(jobItem.JobId);
var user = _userManager.GetUserById(job.UserId);
2015-01-21 03:54:45 +00:00
if (jobItem.IsMarkedForRemoval)
{
// Tell the device to remove it since it has been marked for removal
2015-10-15 02:55:19 +00:00
_logger.Info("Adding ItemIdsToRemove {0} because IsMarkedForRemoval is set.", jobItem.ItemId);
2015-04-12 16:46:29 +00:00
removeFromDevice = true;
2015-01-21 03:54:45 +00:00
}
else if (user == null)
2014-12-30 19:16:01 +00:00
{
// Tell the device to remove it since the user is gone now
2015-10-15 02:55:19 +00:00
_logger.Info("Adding ItemIdsToRemove {0} because the user is no longer valid.", jobItem.ItemId);
2015-04-12 16:46:29 +00:00
removeFromDevice = true;
2014-12-30 19:16:01 +00:00
}
2015-04-22 13:28:45 +00:00
else if (!IsLibraryItemAvailable(libraryItem))
{
// Tell the device to remove it since it's no longer available
2015-10-15 02:55:19 +00:00
_logger.Info("Adding ItemIdsToRemove {0} because it is no longer available.", jobItem.ItemId);
2015-04-22 13:28:45 +00:00
removeFromDevice = true;
}
2014-12-30 19:16:01 +00:00
else if (job.UnwatchedOnly)
{
2015-10-15 02:55:19 +00:00
if (libraryItem is Video && libraryItem.IsPlayed(user))
2014-12-30 19:16:01 +00:00
{
2015-04-22 13:28:45 +00:00
// Tell the device to remove it since it has been played
2015-10-15 02:55:19 +00:00
_logger.Info("Adding ItemIdsToRemove {0} because it has been marked played.", jobItem.ItemId);
2015-04-12 16:46:29 +00:00
removeFromDevice = true;
2014-12-30 19:16:01 +00:00
}
}
}
else
2014-12-30 16:36:49 +00:00
{
2014-12-30 19:16:01 +00:00
// Content is no longer on the device
2015-04-12 16:46:29 +00:00
if (jobItem.IsMarkedForRemoval)
{
jobItem.Status = SyncJobItemStatus.RemovedFromDevice;
}
else
{
2015-10-15 02:55:19 +00:00
_logger.Info("Setting status to Queued for {0} because it is no longer on the device.", jobItem.ItemId);
2015-04-12 16:46:29 +00:00
jobItem.Status = SyncJobItemStatus.Queued;
}
requiresSaving = true;
}
if (removeFromDevice)
{
response.ItemIdsToRemove.Add(jobItem.ItemId);
jobItem.IsMarkedForRemoval = true;
requiresSaving = true;
}
if (requiresSaving)
{
2015-02-05 03:01:37 +00:00
await UpdateSyncJobItemInternal(jobItem).ConfigureAwait(false);
2014-12-30 16:36:49 +00:00
}
}
2014-12-29 05:56:55 +00:00
2015-01-03 19:38:22 +00: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 19:16:01 +00:00
response.ItemIdsToRemove = response.ItemIdsToRemove.Distinct(StringComparer.OrdinalIgnoreCase).ToList();
2015-03-31 18:50:08 +00:00
var itemsOnDevice = request.LocalItemIds
.Except(response.ItemIdsToRemove)
.ToList();
SetUserAccess(request, response, itemsOnDevice);
return response;
}
private async Task<SyncDataResponse> SyncDataUsingSyncJobItemIds(SyncDataRequest request)
{
var jobItemResult = GetJobItems(new SyncJobItemQuery
{
TargetId = request.TargetId,
Statuses = new[] { SyncJobItemStatus.Synced }
});
var response = new SyncDataResponse();
foreach (var jobItem in jobItemResult.Items)
{
2015-04-12 16:46:29 +00:00
var requiresSaving = false;
var removeFromDevice = false;
2015-04-22 13:28:45 +00:00
var libraryItem = _libraryManager.GetItemById(jobItem.ItemId);
2015-03-31 18:50:08 +00:00
if (request.SyncJobItemIds.Contains(jobItem.Id, StringComparer.OrdinalIgnoreCase))
{
var job = _repo.GetJob(jobItem.JobId);
var user = _userManager.GetUserById(job.UserId);
if (jobItem.IsMarkedForRemoval)
{
// Tell the device to remove it since it has been marked for removal
2015-10-15 02:55:19 +00:00
_logger.Info("Adding ItemIdsToRemove {0} because IsMarkedForRemoval is set.", jobItem.Id);
2015-04-12 16:46:29 +00:00
removeFromDevice = true;
2015-03-31 18:50:08 +00:00
}
else if (user == null)
{
// Tell the device to remove it since the user is gone now
2015-10-15 02:55:19 +00:00
_logger.Info("Adding ItemIdsToRemove {0} because the user is no longer valid.", jobItem.Id);
2015-04-12 16:46:29 +00:00
removeFromDevice = true;
2015-03-31 18:50:08 +00:00
}
2015-04-22 13:28:45 +00:00
else if (!IsLibraryItemAvailable(libraryItem))
{
// Tell the device to remove it since it's no longer available
2015-10-15 02:55:19 +00:00
_logger.Info("Adding ItemIdsToRemove {0} because it is no longer available.", jobItem.Id);
2015-04-22 13:28:45 +00:00
removeFromDevice = true;
}
2015-03-31 18:50:08 +00:00
else if (job.UnwatchedOnly)
{
2015-10-15 02:55:19 +00:00
if (libraryItem is Video && libraryItem.IsPlayed(user))
2015-03-31 18:50:08 +00:00
{
2015-04-22 13:28:45 +00:00
// Tell the device to remove it since it has been played
2015-10-15 02:55:19 +00:00
_logger.Info("Adding ItemIdsToRemove {0} because it has been marked played.", jobItem.Id);
2015-04-12 16:46:29 +00:00
removeFromDevice = true;
2015-03-31 18:50:08 +00:00
}
}
}
else
{
// Content is no longer on the device
2015-04-12 16:46:29 +00:00
if (jobItem.IsMarkedForRemoval)
{
jobItem.Status = SyncJobItemStatus.RemovedFromDevice;
}
else
{
2015-10-15 02:55:19 +00:00
_logger.Info("Setting status to Queued for {0} because it is no longer on the device.", jobItem.Id);
2015-04-12 16:46:29 +00:00
jobItem.Status = SyncJobItemStatus.Queued;
}
requiresSaving = true;
}
if (removeFromDevice)
{
response.ItemIdsToRemove.Add(jobItem.Id);
jobItem.IsMarkedForRemoval = true;
requiresSaving = true;
}
if (requiresSaving)
{
2015-03-31 18:50:08 +00:00
await UpdateSyncJobItemInternal(jobItem).ConfigureAwait(false);
}
}
// Now check each item that's on the device
foreach (var syncJobItemId in request.SyncJobItemIds)
{
// See if it's already marked for removal
if (response.ItemIdsToRemove.Contains(syncJobItemId, StringComparer.OrdinalIgnoreCase))
{
continue;
}
// If there isn't a sync job for this item, mark it for removal
if (!jobItemResult.Items.Any(i => string.Equals(syncJobItemId, i.Id, StringComparison.OrdinalIgnoreCase)))
{
response.ItemIdsToRemove.Add(syncJobItemId);
}
}
response.ItemIdsToRemove = response.ItemIdsToRemove.Distinct(StringComparer.OrdinalIgnoreCase).ToList();
2014-12-31 06:24:49 +00:00
2014-12-29 05:56:55 +00:00
return response;
}
2014-12-30 19:16:01 +00:00
2015-02-03 21:06:56 +00:00
private void SetUserAccess(SyncDataRequest request, SyncDataResponse response, List<string> itemIds)
{
var users = request.OfflineUserIds
.Select(_userManager.GetUserById)
.Where(i => i != null)
.ToList();
foreach (var itemId in itemIds)
{
var item = _libraryManager.GetItemById(itemId);
if (item != null)
{
2015-02-03 21:42:13 +00:00
response.ItemUserAccess[itemId] = users
2015-03-22 23:41:58 +00:00
.Where(i => IsUserVisible(item, i))
2015-02-03 21:42:13 +00:00
.Select(i => i.Id.ToString("N"))
.OrderBy(i => i)
.ToList();
2015-02-03 21:06:56 +00:00
}
}
}
private bool IsUserVisible(BaseItem item, User user)
{
return item.IsVisibleStandalone(user);
}
2014-12-30 19:16:01 +00:00
private bool IsLibraryItemAvailable(BaseItem item)
{
if (item == null)
{
return false;
}
return true;
}
2015-01-21 03:54:45 +00: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;
2015-02-05 03:01:37 +00:00
await UpdateSyncJobItemInternal(jobItem).ConfigureAwait(false);
2015-01-21 03:54:45 +00:00
var processor = GetSyncJobProcessor();
await processor.UpdateJobStatus(jobItem.JobId).ConfigureAwait(false);
}
2015-04-04 01:24:49 +00:00
public async Task CancelItems(string targetId, IEnumerable<string> itemIds)
{
foreach (var item in itemIds)
{
var syncJobItemResult = GetJobItems(new SyncJobItemQuery
{
AddMetadata = false,
ItemId = item,
TargetId = targetId,
Statuses = new[] { SyncJobItemStatus.Queued, SyncJobItemStatus.ReadyToTransfer, SyncJobItemStatus.Converting, SyncJobItemStatus.Synced, SyncJobItemStatus.Failed }
});
foreach (var jobItem in syncJobItemResult.Items)
{
await CancelJobItem(jobItem.Id).ConfigureAwait(false);
}
}
}
2015-01-21 03:54:45 +00:00
public async Task CancelJobItem(string id)
{
var jobItem = _repo.GetJobItem(id);
2015-04-04 01:24:49 +00:00
if (jobItem.Status != SyncJobItemStatus.Queued && jobItem.Status != SyncJobItemStatus.ReadyToTransfer && jobItem.Status != SyncJobItemStatus.Converting && jobItem.Status != SyncJobItemStatus.Failed && jobItem.Status != SyncJobItemStatus.Synced)
2015-01-21 03:54:45 +00:00
{
throw new ArgumentException("Operation is not valid for this job item");
}
2015-04-04 01:24:49 +00:00
if (jobItem.Status != SyncJobItemStatus.Synced)
{
jobItem.Status = SyncJobItemStatus.Cancelled;
}
2015-01-21 03:54:45 +00:00
jobItem.Progress = 0;
jobItem.IsMarkedForRemoval = true;
2015-02-05 03:01:37 +00:00
await UpdateSyncJobItemInternal(jobItem).ConfigureAwait(false);
2015-01-21 03:54:45 +00:00
var processor = GetSyncJobProcessor();
await processor.UpdateJobStatus(jobItem.JobId).ConfigureAwait(false);
var path = processor.GetTemporaryPath(jobItem);
try
{
_fileSystem.DeleteDirectory(path, true);
2015-02-03 21:06:56 +00:00
}
catch (DirectoryNotFoundException)
{
}
catch (Exception ex)
{
_logger.ErrorException("Error deleting directory {0}", ex, path);
}
2015-04-04 01:24:49 +00:00
//var jobItemsResult = GetJobItems(new SyncJobItemQuery
//{
// AddMetadata = false,
// JobId = jobItem.JobId,
// Limit = 0,
// Statuses = new[] { SyncJobItemStatus.Converting, SyncJobItemStatus.Failed, SyncJobItemStatus.Queued, SyncJobItemStatus.ReadyToTransfer, SyncJobItemStatus.Synced, SyncJobItemStatus.Transferring }
//});
//if (jobItemsResult.TotalRecordCount == 0)
//{
// await CancelJob(jobItem.JobId).ConfigureAwait(false);
//}
2015-01-21 03:54:45 +00:00
}
2015-01-21 04:56:00 +00:00
2015-04-04 01:24:49 +00:00
public Task MarkJobItemForRemoval(string id)
2015-01-21 04:56:00 +00:00
{
2015-04-04 01:24:49 +00:00
return CancelJobItem(id);
2015-01-21 04:56:00 +00:00
}
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;
2015-02-05 03:01:37 +00:00
await UpdateSyncJobItemInternal(jobItem).ConfigureAwait(false);
2015-01-21 04:56:00 +00:00
var processor = GetSyncJobProcessor();
await processor.UpdateJobStatus(jobItem.JobId).ConfigureAwait(false);
}
2015-01-24 19:03:55 +00:00
2015-02-05 05:29:37 +00:00
public async Task ReportSyncJobItemTransferBeginning(string id)
{
var jobItem = _repo.GetJobItem(id);
jobItem.Status = SyncJobItemStatus.Transferring;
await UpdateSyncJobItemInternal(jobItem).ConfigureAwait(false);
var processor = GetSyncJobProcessor();
await processor.UpdateJobStatus(jobItem.JobId).ConfigureAwait(false);
}
public async Task ReportSyncJobItemTransferFailed(string id)
{
var jobItem = _repo.GetJobItem(id);
jobItem.Status = SyncJobItemStatus.ReadyToTransfer;
await UpdateSyncJobItemInternal(jobItem).ConfigureAwait(false);
var processor = GetSyncJobProcessor();
await processor.UpdateJobStatus(jobItem.JobId).ConfigureAwait(false);
}
2015-06-02 17:46:44 +00:00
public QueryResult<SyncedItemProgress> GetSyncedItemProgresses(SyncJobItemQuery query)
2015-01-24 19:03:55 +00:00
{
2015-06-02 17:46:44 +00:00
return _repo.GetSyncedItemProgresses(query);
2015-01-24 19:03:55 +00:00
}
2015-02-03 18:14:53 +00:00
2015-03-15 19:10:27 +00:00
public SyncJobOptions GetAudioOptions(SyncJobItem jobItem, SyncJob job)
2015-02-03 18:14:53 +00:00
{
2015-03-15 19:10:27 +00:00
var options = GetSyncJobOptions(jobItem.TargetId, null, null);
if (job.Bitrate.HasValue)
2015-02-03 18:14:53 +00:00
{
2015-03-15 19:10:27 +00:00
options.DeviceProfile.MaxStaticBitrate = job.Bitrate.Value;
}
return options;
2015-02-03 18:14:53 +00:00
}
2015-08-18 19:45:41 +00:00
public ISyncProvider GetSyncProvider(SyncJobItem jobItem, SyncJob job)
{
foreach (var provider in _providers)
{
foreach (var target in GetSyncTargets(provider))
{
if (string.Equals(target.Id, jobItem.TargetId, StringComparison.OrdinalIgnoreCase))
{
return provider;
}
}
}
return null;
}
2015-03-15 19:10:27 +00:00
public SyncJobOptions GetVideoOptions(SyncJobItem jobItem, SyncJob job)
2015-02-03 18:14:53 +00:00
{
2015-03-15 19:10:27 +00:00
var options = GetSyncJobOptions(jobItem.TargetId, job.Profile, job.Quality);
if (job.Bitrate.HasValue)
2015-02-03 18:14:53 +00:00
{
2015-03-15 19:10:27 +00:00
options.DeviceProfile.MaxStaticBitrate = job.Bitrate.Value;
}
return options;
2015-02-03 18:14:53 +00:00
}
2015-03-12 04:03:45 +00:00
2015-03-15 19:10:27 +00:00
private SyncJobOptions GetSyncJobOptions(string targetId, string profile, string quality)
2015-03-12 04:03:45 +00:00
{
foreach (var provider in _providers)
{
foreach (var target in GetSyncTargets(provider))
{
if (string.Equals(target.Id, targetId, StringComparison.OrdinalIgnoreCase))
{
2015-03-15 19:10:27 +00:00
return GetSyncJobOptions(provider, target, profile, quality);
2015-03-12 04:03:45 +00:00
}
}
}
2015-03-15 19:10:27 +00:00
return GetDefaultSyncJobOptions(profile, quality);
2015-03-12 04:03:45 +00:00
}
2015-03-15 19:10:27 +00:00
private SyncJobOptions GetSyncJobOptions(ISyncProvider provider, SyncTarget target, string profile, string quality)
2015-03-12 04:03:45 +00:00
{
2015-03-12 04:47:16 +00:00
var hasProfile = provider as IHasSyncQuality;
2015-03-12 04:03:45 +00:00
if (hasProfile != null)
{
2015-03-15 19:10:27 +00:00
return hasProfile.GetSyncJobOptions(target, profile, quality);
2015-03-12 04:03:45 +00:00
}
2015-03-15 19:10:27 +00:00
return GetDefaultSyncJobOptions(profile, quality);
2015-03-12 05:12:02 +00:00
}
2015-03-15 19:10:27 +00:00
private SyncJobOptions GetDefaultSyncJobOptions(string profile, string quality)
2015-03-12 05:12:02 +00:00
{
2015-03-16 02:49:13 +00:00
var supportsAc3 = string.Equals(profile, "general", StringComparison.OrdinalIgnoreCase);
var deviceProfile = new CloudSyncProfile(supportsAc3, false);
2015-03-15 17:50:47 +00:00
deviceProfile.MaxStaticBitrate = SyncHelper.AdjustBitrate(deviceProfile.MaxStaticBitrate, quality);
2015-03-12 05:12:02 +00:00
2015-03-15 19:10:27 +00:00
return new SyncJobOptions
{
DeviceProfile = deviceProfile,
IsConverting = IsConverting(profile, quality)
};
}
private bool IsConverting(string profile, string quality)
{
return !string.Equals(profile, "original", StringComparison.OrdinalIgnoreCase);
2015-03-12 04:03:45 +00:00
}
2015-03-12 04:47:16 +00:00
public IEnumerable<SyncQualityOption> GetQualityOptions(string targetId)
2015-04-05 15:01:57 +00:00
{
return GetQualityOptions(targetId, null);
}
public IEnumerable<SyncQualityOption> GetQualityOptions(string targetId, User user)
2015-03-12 04:47:16 +00:00
{
foreach (var provider in _providers)
{
foreach (var target in GetSyncTargets(provider))
{
if (string.Equals(target.Id, targetId, StringComparison.OrdinalIgnoreCase))
{
2015-04-05 15:01:57 +00:00
return GetQualityOptions(provider, target, user);
2015-03-12 04:47:16 +00:00
}
}
}
return new List<SyncQualityOption>();
}
2015-04-05 15:01:57 +00:00
private IEnumerable<SyncQualityOption> GetQualityOptions(ISyncProvider provider, SyncTarget target, User user)
2015-03-12 04:47:16 +00:00
{
var hasQuality = provider as IHasSyncQuality;
if (hasQuality != null)
{
2015-04-05 15:01:57 +00:00
var options = hasQuality.GetQualityOptions(target);
if (user != null && !user.Policy.EnableSyncTranscoding)
{
options = options.Where(i => i.IsOriginalQuality);
}
return options;
2015-03-12 04:47:16 +00:00
}
// Default options for providers that don't override
return new List<SyncQualityOption>
{
new SyncQualityOption
{
2015-03-15 04:17:35 +00:00
Name = "High",
Id = "high",
IsDefault = true
2015-03-12 04:47:16 +00:00
},
new SyncQualityOption
{
2015-03-15 04:17:35 +00:00
Name = "Medium",
Id = "medium"
2015-03-12 04:47:16 +00:00
},
new SyncQualityOption
{
2015-03-15 04:17:35 +00:00
Name = "Low",
Id = "low"
2015-03-12 04:47:16 +00:00
},
new SyncQualityOption
{
2015-03-15 04:17:35 +00:00
Name = "Custom",
Id = "custom"
2015-03-12 04:47:16 +00:00
}
};
}
2015-03-15 01:42:09 +00:00
2015-04-05 15:01:57 +00:00
public IEnumerable<SyncProfileOption> GetProfileOptions(string targetId, User user)
2015-03-15 01:42:09 +00:00
{
foreach (var provider in _providers)
{
foreach (var target in GetSyncTargets(provider))
{
if (string.Equals(target.Id, targetId, StringComparison.OrdinalIgnoreCase))
{
2015-04-05 15:01:57 +00:00
return GetProfileOptions(provider, target, user);
2015-03-15 01:42:09 +00:00
}
}
}
2015-03-15 04:17:35 +00:00
return new List<SyncProfileOption>();
2015-03-15 01:42:09 +00:00
}
2015-04-05 15:01:57 +00:00
public IEnumerable<SyncProfileOption> GetProfileOptions(string targetId)
{
return GetProfileOptions(targetId, null);
}
private IEnumerable<SyncProfileOption> GetProfileOptions(ISyncProvider provider, SyncTarget target, User user)
2015-03-15 01:42:09 +00:00
{
var hasQuality = provider as IHasSyncQuality;
if (hasQuality != null)
{
return hasQuality.GetProfileOptions(target);
}
2015-03-15 04:17:35 +00:00
var list = new List<SyncProfileOption>();
list.Add(new SyncProfileOption
{
Name = "Original",
Id = "Original",
Description = "Syncs original files as-is.",
EnableQualityOptions = false
});
2015-04-05 15:01:57 +00:00
if (user == null || user.Policy.EnableSyncTranscoding)
2015-03-15 04:17:35 +00:00
{
2015-04-05 15:01:57 +00:00
list.Add(new SyncProfileOption
{
Name = "Baseline",
Id = "baseline",
Description = "Designed for compatibility with all devices, including web browsers. Targets H264/AAC video and MP3 audio."
});
2015-03-15 01:42:09 +00:00
2015-04-05 15:01:57 +00:00
list.Add(new SyncProfileOption
{
Name = "General",
Id = "general",
Description = "Designed for compatibility with Chromecast, Roku, Smart TV's, and other similar devices. Targets H264/AAC/AC3 video and MP3 audio.",
IsDefault = true
});
}
2015-03-15 01:42:09 +00:00
return list;
}
2014-07-17 22:21:35 +00:00
}
}