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

194 lines
7.0 KiB
C#
Raw Normal View History

2015-03-08 04:44:31 +00:00
using MediaBrowser.Common.Configuration;
2015-03-31 18:50:08 +00:00
using MediaBrowser.Controller;
2015-03-08 04:44:31 +00:00
using MediaBrowser.Controller.Sync;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Sync;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.IO;
2015-04-15 03:41:29 +00:00
using Interfaces.IO;
2016-10-06 18:55:01 +00:00
using MediaBrowser.Common.IO;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.IO;
2015-03-08 04:44:31 +00:00
namespace MediaBrowser.Server.Implementations.Sync
{
public class TargetDataProvider : ISyncDataProvider
{
private readonly SyncTarget _target;
private readonly IServerSyncProvider _provider;
private readonly SemaphoreSlim _dataLock = new SemaphoreSlim(1, 1);
private List<LocalItem> _items;
private readonly ILogger _logger;
private readonly IJsonSerializer _json;
private readonly IFileSystem _fileSystem;
private readonly IApplicationPaths _appPaths;
2015-03-31 18:50:08 +00:00
private readonly IServerApplicationHost _appHost;
2016-10-06 18:55:01 +00:00
private readonly IMemoryStreamProvider _memoryStreamProvider;
2015-03-08 04:44:31 +00:00
2016-10-06 18:55:01 +00:00
public TargetDataProvider(IServerSyncProvider provider, SyncTarget target, IServerApplicationHost appHost, ILogger logger, IJsonSerializer json, IFileSystem fileSystem, IApplicationPaths appPaths, IMemoryStreamProvider memoryStreamProvider)
2015-03-08 04:44:31 +00:00
{
_logger = logger;
_json = json;
_provider = provider;
_target = target;
_fileSystem = fileSystem;
_appPaths = appPaths;
2016-10-06 18:55:01 +00:00
_memoryStreamProvider = memoryStreamProvider;
2015-03-31 18:50:08 +00:00
_appHost = appHost;
2015-03-08 04:44:31 +00:00
}
2015-04-13 05:12:02 +00:00
private string[] GetRemotePath()
2015-03-08 04:44:31 +00:00
{
var parts = new List<string>
{
2015-03-31 18:50:08 +00:00
_appHost.FriendlyName,
2015-03-08 04:44:31 +00:00
"data.json"
};
2015-03-31 18:50:08 +00:00
parts = parts.Select(i => GetValidFilename(_provider, i)).ToList();
2015-04-13 05:12:02 +00:00
return parts.ToArray();
2015-03-08 04:44:31 +00:00
}
2015-03-31 18:50:08 +00:00
private string GetValidFilename(IServerSyncProvider provider, string filename)
{
// We can always add this method to the sync provider if it's really needed
return _fileSystem.GetValidFilename(filename);
}
2015-11-06 16:46:51 +00:00
private async Task<List<LocalItem>> RetrieveItems(CancellationToken cancellationToken)
2015-03-08 04:44:31 +00:00
{
2015-11-06 16:46:51 +00:00
_logger.Debug("Getting {0} from {1}", string.Join(MediaSync.PathSeparatorString, GetRemotePath().ToArray()), _provider.Name);
2015-04-13 05:12:02 +00:00
2016-10-29 20:02:21 +00:00
var fileResult = await _provider.GetFiles(GetRemotePath().ToArray(), _target, cancellationToken).ConfigureAwait(false);
2015-04-04 01:24:49 +00:00
2015-11-06 16:46:51 +00:00
if (fileResult.Items.Length > 0)
{
2016-10-29 20:02:21 +00:00
using (var stream = await _provider.GetFile(fileResult.Items[0].FullName, _target, new Progress<double>(), cancellationToken))
2015-03-08 04:44:31 +00:00
{
2015-11-06 16:46:51 +00:00
return _json.DeserializeFromStream<List<LocalItem>>(stream);
2015-03-08 04:44:31 +00:00
}
}
2015-11-06 16:46:51 +00:00
return new List<LocalItem>();
}
private async Task EnsureData(CancellationToken cancellationToken)
{
if (_items == null)
{
_items = await RetrieveItems(cancellationToken).ConfigureAwait(false);
}
2015-03-08 04:44:31 +00:00
}
2015-11-06 16:46:51 +00:00
private async Task SaveData(List<LocalItem> items, CancellationToken cancellationToken)
2015-03-08 04:44:31 +00:00
{
2016-10-06 18:55:01 +00:00
using (var stream = _memoryStreamProvider.CreateNew())
2015-03-08 04:44:31 +00:00
{
2015-11-06 16:46:51 +00:00
_json.SerializeToStream(items, stream);
2015-03-08 04:44:31 +00:00
// Save to sync provider
stream.Position = 0;
2015-11-06 16:46:51 +00:00
var remotePath = GetRemotePath();
_logger.Debug("Saving data.json to {0}. Remote path: {1}", _provider.Name, string.Join("/", remotePath));
await _provider.SendFile(stream, remotePath, _target, new Progress<double>(), cancellationToken).ConfigureAwait(false);
2015-03-08 04:44:31 +00:00
}
}
2015-11-06 16:46:51 +00:00
private async Task<T> GetData<T>(bool enableCache, Func<List<LocalItem>, T> dataFactory)
2015-03-08 04:44:31 +00:00
{
2015-11-06 16:46:51 +00:00
if (!enableCache)
{
var items = await RetrieveItems(CancellationToken.None).ConfigureAwait(false);
var newCache = items.ToList();
var result = dataFactory(items);
await UpdateCache(newCache).ConfigureAwait(false);
return result;
}
2015-03-08 04:44:31 +00:00
await _dataLock.WaitAsync().ConfigureAwait(false);
try
{
await EnsureData(CancellationToken.None).ConfigureAwait(false);
return dataFactory(_items);
}
finally
{
_dataLock.Release();
}
}
private async Task UpdateData(Func<List<LocalItem>, List<LocalItem>> action)
2015-11-06 16:46:51 +00:00
{
var items = await RetrieveItems(CancellationToken.None).ConfigureAwait(false);
items = action(items);
await SaveData(items.ToList(), CancellationToken.None).ConfigureAwait(false);
2015-11-21 03:07:44 +00:00
2015-11-06 16:46:51 +00:00
await UpdateCache(null).ConfigureAwait(false);
}
private async Task UpdateCache(List<LocalItem> list)
2015-03-08 04:44:31 +00:00
{
await _dataLock.WaitAsync().ConfigureAwait(false);
try
{
2015-11-06 16:46:51 +00:00
_items = list;
2015-03-08 04:44:31 +00:00
}
finally
{
_dataLock.Release();
}
}
2015-04-13 05:12:02 +00:00
public Task<List<LocalItem>> GetLocalItems(SyncTarget target, string serverId)
2015-03-31 18:50:08 +00:00
{
2015-11-06 16:46:51 +00:00
return GetData(false, items => items.Where(i => string.Equals(i.ServerId, serverId, StringComparison.OrdinalIgnoreCase)).ToList());
2015-03-31 18:50:08 +00:00
}
2015-03-08 04:44:31 +00:00
public Task AddOrUpdate(SyncTarget target, LocalItem item)
{
return UpdateData(items =>
{
var list = items.Where(i => !string.Equals(i.Id, item.Id, StringComparison.OrdinalIgnoreCase))
.ToList();
list.Add(item);
return list;
});
}
public Task Delete(SyncTarget target, string id)
{
return UpdateData(items => items.Where(i => !string.Equals(i.Id, id, StringComparison.OrdinalIgnoreCase)).ToList());
}
public Task<LocalItem> Get(SyncTarget target, string id)
{
2015-11-06 16:46:51 +00:00
return GetData(true, items => items.FirstOrDefault(i => string.Equals(i.Id, id, StringComparison.OrdinalIgnoreCase)));
2015-03-08 04:44:31 +00:00
}
2015-04-04 01:24:49 +00:00
public Task<List<LocalItem>> GetItems(SyncTarget target, string serverId, string itemId)
2015-03-08 04:44:31 +00:00
{
2015-11-06 16:46:51 +00:00
return GetData(true, items => items.Where(i => string.Equals(i.ServerId, serverId, StringComparison.OrdinalIgnoreCase) && string.Equals(i.ItemId, itemId, StringComparison.OrdinalIgnoreCase)).ToList());
2015-03-08 04:44:31 +00:00
}
2015-04-04 01:24:49 +00:00
public Task<List<LocalItem>> GetItemsBySyncJobItemId(SyncTarget target, string serverId, string syncJobItemId)
2015-03-08 04:44:31 +00:00
{
2015-11-06 16:46:51 +00:00
return GetData(false, items => items.Where(i => string.Equals(i.ServerId, serverId, StringComparison.OrdinalIgnoreCase) && string.Equals(i.SyncJobItemId, syncJobItemId, StringComparison.OrdinalIgnoreCase)).ToList());
2015-03-31 18:50:08 +00:00
}
2015-03-08 04:44:31 +00:00
}
}