jellyfin/MediaBrowser.Api/Sync/SyncService.cs

394 lines
14 KiB
C#
Raw Normal View History

2015-04-28 06:56:57 -07:00
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Dto;
2014-12-16 22:30:31 -07:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
2014-07-17 15:21:35 -07:00
using MediaBrowser.Controller.Sync;
using MediaBrowser.Model.Querying;
using MediaBrowser.Model.Sync;
2014-12-26 10:45:06 -07:00
using MediaBrowser.Model.Users;
2014-07-17 15:21:35 -07:00
using ServiceStack;
2014-12-16 22:30:31 -07:00
using System;
2014-07-22 09:36:34 -07:00
using System.Collections.Generic;
2014-12-16 22:30:31 -07:00
using System.Linq;
2014-07-17 15:21:35 -07:00
using System.Threading.Tasks;
namespace MediaBrowser.Api.Sync
{
[Route("/Sync/Jobs/{Id}", "DELETE", Summary = "Cancels a sync job.")]
public class CancelSyncJob : IReturnVoid
{
[ApiMember(Name = "Id", Description = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get; set; }
}
[Route("/Sync/Jobs/{Id}", "GET", Summary = "Gets a sync job.")]
public class GetSyncJob : IReturn<SyncJob>
{
[ApiMember(Name = "Id", Description = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get; set; }
}
2014-12-30 23:24:49 -07:00
[Route("/Sync/Jobs/{Id}", "POST", Summary = "Updates a sync job.")]
public class UpdateSyncJob : SyncJob, IReturnVoid
{
}
2014-12-16 22:52:34 -07:00
[Route("/Sync/JobItems", "GET", Summary = "Gets sync job items.")]
public class GetSyncJobItems : SyncJobItemQuery, IReturn<QueryResult<SyncJobItem>>
{
}
2015-01-20 20:54:45 -07:00
[Route("/Sync/JobItems/{Id}/Enable", "POST", Summary = "Enables a cancelled or queued sync job item")]
public class EnableSyncJobItem : IReturnVoid
{
[ApiMember(Name = "Id", Description = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Id { get; set; }
}
2015-01-20 21:56:00 -07:00
[Route("/Sync/JobItems/{Id}/MarkForRemoval", "POST", Summary = "Marks a job item for removal")]
public class MarkJobItemForRemoval : IReturnVoid
{
[ApiMember(Name = "Id", Description = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Id { get; set; }
}
[Route("/Sync/JobItems/{Id}/UnmarkForRemoval", "POST", Summary = "Unmarks a job item for removal")]
public class UnmarkJobItemForRemoval : IReturnVoid
{
[ApiMember(Name = "Id", Description = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Id { get; set; }
}
2015-01-20 20:54:45 -07:00
[Route("/Sync/JobItems/{Id}", "DELETE", Summary = "Cancels a sync job item")]
public class CancelSyncJobItem : IReturnVoid
{
[ApiMember(Name = "Id", Description = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public string Id { get; set; }
}
2015-04-03 18:24:49 -07:00
[Route("/Sync/{TargetId}/Items", "DELETE", Summary = "Cancels items from a sync target")]
public class CancelItems : IReturnVoid
{
[ApiMember(Name = "TargetId", Description = "TargetId", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "Items")]
public string TargetId { get; set; }
[ApiMember(Name = "ItemIds", Description = "ItemIds", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "Items")]
public string ItemIds { get; set; }
}
2014-07-17 15:21:35 -07:00
[Route("/Sync/Jobs", "GET", Summary = "Gets sync jobs.")]
2014-12-16 22:52:34 -07:00
public class GetSyncJobs : SyncJobQuery, IReturn<QueryResult<SyncJob>>
2014-07-17 15:21:35 -07:00
{
}
[Route("/Sync/Jobs", "POST", Summary = "Gets sync jobs.")]
2014-07-26 10:30:15 -07:00
public class CreateSyncJob : SyncJobRequest, IReturn<SyncJobCreationResult>
{
}
2014-07-22 09:36:34 -07:00
[Route("/Sync/Targets", "GET", Summary = "Gets a list of available sync targets.")]
2014-12-15 22:01:57 -07:00
public class GetSyncTargets : IReturn<List<SyncTarget>>
{
2014-07-22 09:36:34 -07:00
[ApiMember(Name = "UserId", Description = "UserId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
public string UserId { get; set; }
}
2014-12-16 22:30:31 -07:00
[Route("/Sync/Options", "GET", Summary = "Gets a list of available sync targets.")]
public class GetSyncDialogOptions : IReturn<SyncDialogOptions>
{
[ApiMember(Name = "UserId", Description = "UserId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
public string UserId { get; set; }
2014-12-18 21:20:07 -07:00
[ApiMember(Name = "ItemIds", Description = "ItemIds", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
2014-12-16 22:30:31 -07:00
public string ItemIds { get; set; }
2014-12-18 21:20:07 -07:00
[ApiMember(Name = "ParentId", Description = "ParentId", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public string ParentId { get; set; }
2014-12-26 10:45:06 -07:00
2015-03-07 21:44:31 -07:00
[ApiMember(Name = "TargetId", Description = "TargetId", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public string TargetId { get; set; }
2014-12-18 21:20:07 -07:00
[ApiMember(Name = "Category", Description = "Category", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public SyncCategory? Category { get; set; }
2014-12-16 22:30:31 -07:00
}
[Route("/Sync/JobItems/{Id}/Transferred", "POST", Summary = "Reports that a sync job item has successfully been transferred.")]
public class ReportSyncJobItemTransferred : IReturnVoid
{
[ApiMember(Name = "Id", Description = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Id { get; set; }
}
[Route("/Sync/JobItems/{Id}/File", "GET", Summary = "Gets a sync job item file")]
public class GetSyncJobItemFile
{
[ApiMember(Name = "Id", Description = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get; set; }
}
2015-01-16 21:29:53 -07:00
[Route("/Sync/JobItems/{Id}/AdditionalFiles", "GET", Summary = "Gets a sync job item file")]
public class GetSyncJobItemAdditionalFile
{
[ApiMember(Name = "Id", Description = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get; set; }
[ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
public string Name { get; set; }
}
2014-12-26 10:45:06 -07:00
[Route("/Sync/OfflineActions", "POST", Summary = "Reports an action that occurred while offline.")]
public class ReportOfflineActions : List<UserAction>, IReturnVoid
{
}
2014-12-27 15:52:41 -07:00
[Route("/Sync/Items/Ready", "GET", Summary = "Gets ready to download sync items.")]
public class GetReadySyncItems : IReturn<List<SyncedItem>>
{
[ApiMember(Name = "TargetId", Description = "TargetId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
public string TargetId { get; set; }
}
2014-12-28 22:56:55 -07:00
[Route("/Sync/Data", "POST", Summary = "Syncs data between device and server")]
public class SyncData : SyncDataRequest, IReturn<SyncDataResponse>
{
}
2014-07-17 15:21:35 -07:00
[Authenticated]
public class SyncService : BaseApiService
{
private readonly ISyncManager _syncManager;
2014-12-16 22:30:31 -07:00
private readonly IDtoService _dtoService;
private readonly ILibraryManager _libraryManager;
2015-02-16 20:49:47 -07:00
private readonly IUserManager _userManager;
2014-07-17 15:21:35 -07:00
2015-02-16 20:49:47 -07:00
public SyncService(ISyncManager syncManager, IDtoService dtoService, ILibraryManager libraryManager, IUserManager userManager)
2014-07-17 15:21:35 -07:00
{
_syncManager = syncManager;
2014-12-16 22:30:31 -07:00
_dtoService = dtoService;
_libraryManager = libraryManager;
2015-02-16 20:49:47 -07:00
_userManager = userManager;
2014-07-17 15:21:35 -07:00
}
2014-12-15 22:01:57 -07:00
public object Get(GetSyncTargets request)
2014-07-22 09:36:34 -07:00
{
var result = _syncManager.GetSyncTargets(request.UserId);
return ToOptimizedResult(result);
}
2014-12-30 23:24:49 -07:00
public async Task<object> Get(GetSyncJobs request)
2014-07-17 15:21:35 -07:00
{
2014-12-30 23:24:49 -07:00
var result = await _syncManager.GetJobs(request).ConfigureAwait(false);
2014-12-16 22:52:34 -07:00
return ToOptimizedResult(result);
}
public object Get(GetSyncJobItems request)
{
var result = _syncManager.GetJobItems(request);
2014-07-17 15:21:35 -07:00
return ToOptimizedResult(result);
}
public object Get(GetSyncJob request)
{
var result = _syncManager.GetJob(request.Id);
return ToOptimizedResult(result);
}
2014-08-18 07:20:02 -07:00
public void Delete(CancelSyncJob request)
2014-07-17 15:21:35 -07:00
{
2014-08-18 07:20:02 -07:00
var task = _syncManager.CancelJob(request.Id);
Task.WaitAll(task);
2014-07-17 15:21:35 -07:00
}
2014-08-16 22:38:13 -07:00
public async Task<object> Post(CreateSyncJob request)
2014-07-17 15:21:35 -07:00
{
2014-08-16 22:38:13 -07:00
var result = await _syncManager.CreateJob(request).ConfigureAwait(false);
2014-07-17 15:21:35 -07:00
2014-07-26 10:30:15 -07:00
return ToOptimizedResult(result);
}
2014-12-16 22:30:31 -07:00
2015-04-03 18:24:49 -07:00
public void Delete(CancelItems request)
{
var itemIds = request.ItemIds.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var task = _syncManager.CancelItems(request.TargetId, itemIds);
Task.WaitAll(task);
}
2014-12-16 22:30:31 -07:00
public void Post(ReportSyncJobItemTransferred request)
{
var task = _syncManager.ReportSyncJobItemTransferred(request.Id);
Task.WaitAll(task);
}
public object Get(GetSyncJobItemFile request)
{
var jobItem = _syncManager.GetJobItem(request.Id);
2015-04-28 06:56:57 -07:00
if (jobItem == null)
{
throw new ResourceNotFoundException();
}
2015-02-04 20:01:37 -07:00
if (jobItem.Status < SyncJobItemStatus.ReadyToTransfer)
2014-12-16 22:30:31 -07:00
{
throw new ArgumentException("The job item is not yet ready for transfer.");
}
2015-02-04 22:29:37 -07:00
var task = _syncManager.ReportSyncJobItemTransferBeginning(request.Id);
Task.WaitAll(task);
2015-10-02 11:30:27 -07:00
return ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
{
Path = jobItem.OutputPath,
OnError = () =>
{
var failedTask = _syncManager.ReportSyncJobItemTransferFailed(request.Id);
Task.WaitAll(failedTask);
}
});
2014-12-16 22:30:31 -07:00
}
2015-03-11 21:47:16 -07:00
2014-12-16 22:30:31 -07:00
public object Get(GetSyncDialogOptions request)
{
var result = new SyncDialogOptions();
result.Targets = _syncManager.GetSyncTargets(request.UserId)
.ToList();
2015-04-05 08:01:57 -07:00
var auth = AuthorizationContext.GetAuthorizationInfo(Request);
var authenticatedUser = _userManager.GetUserById(auth.UserId);
2015-03-07 21:44:31 -07:00
if (!string.IsNullOrWhiteSpace(request.TargetId))
{
result.Targets = result.Targets
.Where(i => string.Equals(i.Id, request.TargetId, StringComparison.OrdinalIgnoreCase))
.ToList();
2015-03-11 21:47:16 -07:00
result.QualityOptions = _syncManager
2015-04-05 08:01:57 -07:00
.GetQualityOptions(request.TargetId, authenticatedUser)
2015-03-11 21:47:16 -07:00
.ToList();
2015-03-14 18:42:09 -07:00
result.ProfileOptions = _syncManager
2015-04-05 08:01:57 -07:00
.GetProfileOptions(request.TargetId, authenticatedUser)
2015-03-14 18:42:09 -07:00
.ToList();
2015-03-07 21:44:31 -07:00
}
2014-12-18 21:20:07 -07:00
if (request.Category.HasValue)
{
result.Options = SyncHelper.GetSyncOptions(request.Category.Value);
}
else
{
2014-12-26 22:08:39 -07:00
var dtoOptions = new DtoOptions
{
Fields = new List<ItemFields>
{
ItemFields.SyncInfo
}
};
2015-02-04 22:29:37 -07:00
var items = request.ItemIds.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
2014-12-18 21:20:07 -07:00
.Select(_libraryManager.GetItemById)
2015-01-24 12:03:55 -07:00
.Where(i => i != null);
2015-02-16 20:49:47 -07:00
var dtos = _dtoService.GetBaseItemDtos(items, dtoOptions, authenticatedUser)
2014-12-18 21:20:07 -07:00
.ToList();
2014-12-16 22:30:31 -07:00
2014-12-18 21:20:07 -07:00
result.Options = SyncHelper.GetSyncOptions(dtos);
}
2014-12-16 22:30:31 -07:00
return ToOptimizedResult(result);
}
2014-12-26 10:45:06 -07:00
public void Post(ReportOfflineActions request)
{
var task = PostAsync(request);
Task.WaitAll(task);
}
public async Task PostAsync(ReportOfflineActions request)
{
foreach (var action in request)
{
await _syncManager.ReportOfflineAction(action).ConfigureAwait(false);
}
}
2014-12-27 15:52:41 -07:00
2015-02-19 10:46:18 -07:00
public async Task<object> Get(GetReadySyncItems request)
2014-12-27 15:52:41 -07:00
{
2015-02-19 10:46:18 -07:00
var result = await _syncManager.GetReadySyncItems(request.TargetId).ConfigureAwait(false);
return ToOptimizedResult(result);
2014-12-27 15:52:41 -07:00
}
2014-12-28 22:56:55 -07:00
public async Task<object> Post(SyncData request)
{
var response = await _syncManager.SyncData(request).ConfigureAwait(false);
return ToOptimizedResult(response);
}
2014-12-30 23:24:49 -07:00
public void Post(UpdateSyncJob request)
{
var task = _syncManager.UpdateJob(request);
Task.WaitAll(task);
}
2015-01-16 21:29:53 -07:00
public object Get(GetSyncJobItemAdditionalFile request)
{
var jobItem = _syncManager.GetJobItem(request.Id);
2015-02-04 20:01:37 -07:00
if (jobItem.Status < SyncJobItemStatus.ReadyToTransfer)
2015-01-16 21:29:53 -07:00
{
throw new ArgumentException("The job item is not yet ready for transfer.");
}
var file = jobItem.AdditionalFiles.FirstOrDefault(i => string.Equals(i.Name, request.Name, StringComparison.OrdinalIgnoreCase));
if (file == null)
{
throw new ArgumentException("Sync job additional file not found.");
}
return ToStaticFileResult(file.Path);
}
2015-01-20 20:54:45 -07:00
public void Post(EnableSyncJobItem request)
{
var task = _syncManager.ReEnableJobItem(request.Id);
Task.WaitAll(task);
}
public void Delete(CancelSyncJobItem request)
{
var task = _syncManager.CancelJobItem(request.Id);
Task.WaitAll(task);
}
2015-01-20 21:56:00 -07:00
public void Post(MarkJobItemForRemoval request)
{
var task = _syncManager.MarkJobItemForRemoval(request.Id);
Task.WaitAll(task);
}
public void Post(UnmarkJobItemForRemoval request)
{
var task = _syncManager.UnmarkJobItemForRemoval(request.Id);
Task.WaitAll(task);
}
2014-07-17 15:21:35 -07:00
}
}