2014-12-17 06:30:31 +01:00
|
|
|
|
using MediaBrowser.Controller.Dto;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Net;
|
2014-07-18 00:21:35 +02:00
|
|
|
|
using MediaBrowser.Controller.Sync;
|
2014-12-17 06:30:31 +01:00
|
|
|
|
using MediaBrowser.Model.Dto;
|
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-18 00:21:35 +02:00
|
|
|
|
using ServiceStack;
|
2014-12-17 06:30:31 +01:00
|
|
|
|
using System;
|
2014-07-22 18:36:34 +02:00
|
|
|
|
using System.Collections.Generic;
|
2014-12-17 06:30:31 +01:00
|
|
|
|
using System.Linq;
|
2014-07-18 00:21:35 +02: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-17 06:52:34 +01:00
|
|
|
|
[Route("/Sync/JobItems", "GET", Summary = "Gets sync job items.")]
|
|
|
|
|
public class GetSyncJobItems : SyncJobItemQuery, IReturn<QueryResult<SyncJobItem>>
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-18 00:21:35 +02:00
|
|
|
|
[Route("/Sync/Jobs", "GET", Summary = "Gets sync jobs.")]
|
2014-12-17 06:52:34 +01:00
|
|
|
|
public class GetSyncJobs : SyncJobQuery, IReturn<QueryResult<SyncJob>>
|
2014-07-18 00:21:35 +02:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-18 21:07:28 +02:00
|
|
|
|
[Route("/Sync/Jobs", "POST", Summary = "Gets sync jobs.")]
|
2014-07-26 19:30:15 +02:00
|
|
|
|
public class CreateSyncJob : SyncJobRequest, IReturn<SyncJobCreationResult>
|
2014-07-18 21:07:28 +02:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-22 18:36:34 +02:00
|
|
|
|
[Route("/Sync/Targets", "GET", Summary = "Gets a list of available sync targets.")]
|
2014-12-16 06:01:57 +01:00
|
|
|
|
public class GetSyncTargets : IReturn<List<SyncTarget>>
|
2014-07-18 21:07:28 +02:00
|
|
|
|
{
|
2014-07-22 18:36:34 +02:00
|
|
|
|
[ApiMember(Name = "UserId", Description = "UserId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
|
|
|
|
|
public string UserId { get; set; }
|
2014-07-18 21:07:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-17 06:30:31 +01: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-19 05:20:07 +01:00
|
|
|
|
[ApiMember(Name = "ItemIds", Description = "ItemIds", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
|
2014-12-17 06:30:31 +01:00
|
|
|
|
public string ItemIds { get; set; }
|
2014-12-19 05:20:07 +01:00
|
|
|
|
|
|
|
|
|
[ApiMember(Name = "ParentId", Description = "ParentId", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
|
|
|
|
|
public string ParentId { get; set; }
|
2014-12-26 18:45:06 +01:00
|
|
|
|
|
2014-12-19 05:20:07 +01:00
|
|
|
|
[ApiMember(Name = "Category", Description = "Category", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
|
|
|
|
|
public SyncCategory? Category { get; set; }
|
2014-12-17 06:30:31 +01: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; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-26 18:45:06 +01:00
|
|
|
|
[Route("/Sync/OfflineActions", "POST", Summary = "Reports an action that occurred while offline.")]
|
|
|
|
|
public class ReportOfflineActions : List<UserAction>, IReturnVoid
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-18 00:21:35 +02:00
|
|
|
|
[Authenticated]
|
|
|
|
|
public class SyncService : BaseApiService
|
|
|
|
|
{
|
|
|
|
|
private readonly ISyncManager _syncManager;
|
2014-12-17 06:30:31 +01:00
|
|
|
|
private readonly IDtoService _dtoService;
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2014-07-18 00:21:35 +02:00
|
|
|
|
|
2014-12-17 06:30:31 +01:00
|
|
|
|
public SyncService(ISyncManager syncManager, IDtoService dtoService, ILibraryManager libraryManager)
|
2014-07-18 00:21:35 +02:00
|
|
|
|
{
|
|
|
|
|
_syncManager = syncManager;
|
2014-12-17 06:30:31 +01:00
|
|
|
|
_dtoService = dtoService;
|
|
|
|
|
_libraryManager = libraryManager;
|
2014-07-18 00:21:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-16 06:01:57 +01:00
|
|
|
|
public object Get(GetSyncTargets request)
|
2014-07-22 18:36:34 +02:00
|
|
|
|
{
|
|
|
|
|
var result = _syncManager.GetSyncTargets(request.UserId);
|
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-18 00:21:35 +02:00
|
|
|
|
public object Get(GetSyncJobs request)
|
|
|
|
|
{
|
2014-12-17 06:52:34 +01:00
|
|
|
|
var result = _syncManager.GetJobs(request);
|
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object Get(GetSyncJobItems request)
|
|
|
|
|
{
|
|
|
|
|
var result = _syncManager.GetJobItems(request);
|
2014-07-18 00:21:35 +02:00
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object Get(GetSyncJob request)
|
|
|
|
|
{
|
|
|
|
|
var result = _syncManager.GetJob(request.Id);
|
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-18 16:20:02 +02:00
|
|
|
|
public void Delete(CancelSyncJob request)
|
2014-07-18 00:21:35 +02:00
|
|
|
|
{
|
2014-08-18 16:20:02 +02:00
|
|
|
|
var task = _syncManager.CancelJob(request.Id);
|
|
|
|
|
|
|
|
|
|
Task.WaitAll(task);
|
2014-07-18 00:21:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-17 07:38:13 +02:00
|
|
|
|
public async Task<object> Post(CreateSyncJob request)
|
2014-07-18 00:21:35 +02:00
|
|
|
|
{
|
2014-08-17 07:38:13 +02:00
|
|
|
|
var result = await _syncManager.CreateJob(request).ConfigureAwait(false);
|
2014-07-18 00:21:35 +02:00
|
|
|
|
|
2014-07-26 19:30:15 +02:00
|
|
|
|
return ToOptimizedResult(result);
|
2014-07-18 21:07:28 +02:00
|
|
|
|
}
|
2014-12-17 06:30:31 +01: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);
|
|
|
|
|
|
|
|
|
|
if (jobItem.Status != SyncJobItemStatus.Transferring)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("The job item is not yet ready for transfer.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ToStaticFileResult(jobItem.OutputPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object Get(GetSyncDialogOptions request)
|
|
|
|
|
{
|
|
|
|
|
var result = new SyncDialogOptions();
|
|
|
|
|
|
|
|
|
|
result.Targets = _syncManager.GetSyncTargets(request.UserId)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2014-12-19 05:20:07 +01:00
|
|
|
|
if (request.Category.HasValue)
|
|
|
|
|
{
|
|
|
|
|
result.Options = SyncHelper.GetSyncOptions(request.Category.Value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var dtos = request.ItemIds.Split(',')
|
|
|
|
|
.Select(_libraryManager.GetItemById)
|
|
|
|
|
.Where(i => i != null)
|
|
|
|
|
.Select(i => _dtoService.GetBaseItemDto(i, new DtoOptions
|
|
|
|
|
{
|
|
|
|
|
Fields = new List<ItemFields>
|
2014-12-26 18:45:06 +01:00
|
|
|
|
{
|
|
|
|
|
ItemFields.SyncInfo
|
|
|
|
|
}
|
2014-12-19 05:20:07 +01:00
|
|
|
|
}))
|
|
|
|
|
.ToList();
|
2014-12-17 06:30:31 +01:00
|
|
|
|
|
2014-12-19 05:20:07 +01:00
|
|
|
|
result.Options = SyncHelper.GetSyncOptions(dtos);
|
|
|
|
|
}
|
2014-12-17 06:30:31 +01:00
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
|
}
|
2014-12-26 18:45:06 +01: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-07-18 00:21:35 +02:00
|
|
|
|
}
|
|
|
|
|
}
|